1、对list判null和判空来处理
使用mybatis进行in()判断的时候传入参数为List,这时候如果list有内容还好,如果List为空的话只用foreach会报错,那么就要判断List是否为空了,当然可以在java代码中进行判断,但是我不想每次调用该方法都要进行判断,所有最好还是在mybatis的sql配置文件中判断,配置如下:
<if test="list != null and list.size() > 0">
_claimreview.id in
<foreach collection="list" index="index" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</if>
2、对list判null和判空来处理(非必填list推荐)
<foreach collection="list" index="index" item="item" open="AND _claimreview.id IN (" separator="," close=")">
#{item}
</foreach>
这种方法在传入的collection值为空时,不会生成open内的sql语句,比较简洁
3、对list判null和判空来处理(必填list推荐)
select * from aaa_table
where
<foreach collection="list" index="index" item="item" open=" _claimreview.id IN (" separator="," close=")">
#{item}
</foreach>
这个方法对于 需要必填的list 例如 delete from table_a where <foreach ...></foreach>
就非常有用,因为没有就会报错,不会误删
千万不要出现如下错误示例:
delete from aaa_table
where 1=1
<foreach collection="list" index="index" item="item" open=" and _claimreview.id IN (" separator="," close=")">
#{item}
</foreach>
这样的话,可能连整张表都删没了
总结一下:最好还是在service层写个 if标签 做下判断,如果是必填list,如果size<=0,则不要调用DAO层,这样最安全,至于还有什么拦截器的配置方法也转载一下,大家需要的话看着用也行。
https://blog.csdn.net/weixin_34068198/article/details/88009410
ok~