Mybatis使用@Select注解sql中使用in問題
更新時間:2023年05月26日 10:14:29 作者:wuzi_uzi
這篇文章主要介紹了Mybatis使用@Select注解sql中使用in問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
Mybatis使用@Select注解sql中使用in
mapper
@Select("SELECT u.* , ur.role_id , r.role_name from sys_user_role ur , sys_role r , sys_user u ,sys_user_depart ud " +
"where ur.role_id = r.id and ur.user_id = u.id and u.id = ud.user_id " +
"and ud.create_user_id in (${createUserId})")
public Page<SysRoleDeptVO> getUserByCreateUserIds(Page page, @Param("createUserId") String createUserId);service
@Override
public Page<SysRoleDeptVO> getByUserIds(Page<SysRoleDeptVO> page, List<String> userIds) {
/** 如果當前部門下沒用戶的話,就傳個 "" 過去 由于 mybatis 解析后空字符串
* 后是什么都沒有, 這里用 in () 如果這樣,就會異常 ,所以當用戶為空的話,
* 傳 "''" 這樣解析后 變成了 in ('')
* */
String userids = "''" ;
if(userIds != null && userIds.size() != 0){
StringBuilder stringBuilder = new StringBuilder("");
for (int i = 0; i < userIds.size(); i++) {
stringBuilder.append("'");
stringBuilder.append(userIds.get(i));
stringBuilder.append("'");
stringBuilder.append(",");
}
userids = stringBuilder.substring(0, stringBuilder.length() - 1);
}
return userMapper.getByUserIds(page,userids);mapper防止轉(zhuǎn)義字符
<![CDATA[ ? ? ?]]>?
Mybatis在@Select寫IN SQL
簡單介紹在 MyBatis 的注解方式中,寫包含 in 語法的 SQL
直接了斷看下面的代碼,SQL 是獲取某幾個 ID 的文章
@Select("<script>" +
? ? ? ? ? ? "select * from article where id in " +
? ? ? ? ? ? "<foreach item='item' index='index' collection='articleIds' open='(' separator=', ' close=')'>" +
? ? ? ? ? ? ? ? "#{item}" +
? ? ? ? ? ? "</foreach>" +
? ? ? ? "</script>")
List<Article> getArticlesByIds(@Param("articleIds") List<Long> articleIds);總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
springmvc不進入Controller導致404的問題
這篇文章主要介紹了springmvc不進入Controller導致404的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02
spring?boot?動態(tài)生成接口實現(xiàn)類的場景分析
本文不具體介紹動態(tài)代理,主要看一下它在springboot項目中的實際應用,下面我們模仿feign來實現(xiàn)一個調(diào)用三方接口的?httpclient,感謝的朋友跟隨小編一起看看吧2021-11-11
Spring+SpringMVC配置事務管理無效原因及解決辦法詳解
這篇文章主要介紹了Spring+SpringMVC配置事務管理無效原因及解決辦法詳解,具有一定借鑒價值,需要的朋友可以參考下2017-12-12
關于JSONObject.toJSONString出現(xiàn)地址引用問題
這篇文章主要介紹了關于JSONObject.toJSONString出現(xiàn)地址引用問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03

