欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Mybatis使用@Select注解sql中使用in問題

 更新時(shí)間:2023年05月26日 10:14:29   作者:wuzi_uzi  
這篇文章主要介紹了Mybatis使用@Select注解sql中使用in問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

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) {
        /** 如果當(dāng)前部門下沒用戶的話,就傳個(gè) ""  過去   由于 mybatis 解析后空字符串
         *  后是什么都沒有, 這里用  in ()  如果這樣,就會(huì)異常 ,所以當(dāng)用戶為空的話,
         *  傳 "''"  這樣解析后 變成了 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 是獲取某幾個(gè) 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é)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論