直接传入拼接好的where in 条件,如(’111’,’222’,’333’)则需要使用${idlist}传参,即绝对引用,而不能使用#, 如果使用#传参会被mybatis当成字符串再添加一层’’引号,导致错误。 优点:简单方便,高效,缺点:不能防止SQL注入 list转换为Sql条件字符串代码如下:
1 2 3 4 5 6 7 8 9
StringBuildersqlString=newStringBuilder();
for (inti=0; i < exhCollectForm.getSelectedCloId().size(); i++) { if (i > 0) { sqlString.append(","); } sqlString.append("'").append(exhCollectForm.getSelectedCloId().get(i)).append("'"); } Stringresult= sqlString.toString();
1, <deleteid="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, <deleteid="deleteMenuByIdList2" > delete from s_menu where menu_id in <foreachcollection="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 <foreachcollection="idlist"item="menu_id"separator=","open="("close=")"> #{menu_id} </foreach>; </delete> 3, <deleteid="deleteMenuByIdList3" > delete from s_menu where menu_id in <foreachcollection="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 <foreachcollection="idlist"item="menu_id"separator=","open="("close=")"> #{menu_id} </foreach>; </delete>