第一种方法:in 条件为拼接好的字符串

直接传入拼接好的where in 条件,如(’111’,’222’,’333’)则需要使用${idlist}传参,即绝对引用,而不能使用#, 如果使用#传参会被mybatis当成字符串再添加一层’’引号,导致错误。
优点:简单方便,高效,缺点:不能防止SQL注入
list转换为Sql条件字符串代码如下:

1
2
3
4
5
6
7
8
9
StringBuilder sqlString = new StringBuilder();

for (int i = 0; i < exhCollectForm.getSelectedCloId().size(); i++) {
if (i > 0) {
sqlString.append(",");
}
sqlString.append("'").append(exhCollectForm.getSelectedCloId().get(i)).append("'");
}
String result = sqlString.toString();

第二种方法:in 条件为List对象

in条件直接传入List对象,让mybatis再去拼接生成in条件,这个很麻烦,但是可以防止SQL注入

第三种方法:in 条件为String[] 数组

in条件直接传入[]数组对象,让mybatis再去拼接生成in条件,这个很麻烦,但是可以防止SQL注入
Service:

1
2
3
int deleteMenuByIdList(String idlist,int delcount,int lastsort);
int deleteMenuByIdList(List<String> idlist, int delcount,int lastsort);
int deleteMenuByIdList(String[] idlist, int delcount,int lastsort);

Dao:

1
2
3
4
5
6
//用这种写法方便,idlist直接拼接好,xml中用 in ${idlist}接受参数
int deleteMenuByIdList(@Param("idlist")String idlist, @Param("delcount")int delcount, @Param("lastsort")int lastsort);
//用这种写法直接传List对象,xml中再写循环拼接,麻烦
int deleteMenuByIdList2(@Param("idlist")List<String> idlist, @Param("delcount")int delcount, @Param("lastsort")int lastsort);
//用这种写法直接传String[]数组,xml中再写循环拼接,麻烦
int deleteMenuByIdList3(@Param("idlist")String[] idlist, @Param("delcount")int delcount, @Param("lastsort")int lastsort);

mappper.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
1,
<delete id="deleteMenuByIdList" >
delete from s_menu where menu_id in ${idlist};
update s_menu set sort=sort-#{delcount} where sort >= #{lastsort} and menu_id not in ${idlist};
</delete>

2,
<delete id="deleteMenuByIdList2" >
delete from s_menu where menu_id in
<foreach collection="idlist" item="menu_id" separator="," open="(" close=")">
#{menu_id}
</foreach>
;update s_menu set sort=sort-#{delcount} where sort >= #{lastsort} and menu_id not in
<foreach collection="idlist" item="menu_id" separator="," open="(" close=")">
#{menu_id}
</foreach>;
</delete>

3,
<delete id="deleteMenuByIdList3" >
delete from s_menu where menu_id in
<foreach collection="idlist" item="menu_id" separator="," open="(" close=")">
#{menu_id}
</foreach>
;update s_menu set sort=sort-#{delcount} where sort >= #{lastsort} and menu_id not in
<foreach collection="idlist" item="menu_id" separator="," open="(" close=")">
#{menu_id}
</foreach>;
</delete>

原文链接:https://blog.csdn.net/wh445306/article/details/111056331