mybatis之if判断

今天使用mybatis开发公司中台项目踩的一个坑,分享并记录一下

踩坑前因:因项目中比较多状态字段,用了大量的Integer 0和1进行判断

在功能做完后只是粗略的点了下觉得没多大问题(来自程序员强大的自信),便提交了代码,很不巧的是刚好领导在做功能测试,发现了功能缺陷,主角来了:

在做牧户查询时所有的0判断均无效,而1有效。查阅资料得知在if语句做如下判断时intger类型0也视为false

<if test="status != null and status !=''">and status = #{status}</if> 

解决方案有二:

1、<if test="status != null ">and status = #{status}</if> 直接判断!=null即可,只有字符串才需要判断!=""。

2、或者这样写 <if test="status != null and status !='' or status==0 ">and status = #{status}</if

附上大神详细解析链接:https://www.jianshu.com/p/91ed365c0fdd

猜你喜欢

转载自www.cnblogs.com/speree/p/13394267.html