运行结果如下:
数据库数据如下 应该查询出11条数据,从上图看输出的sql动态sql语句,if条件没执行
代码如下:
测试类
@Test
public void testStorageCount() {
Map<String, Object> storageMap = new HashMap<>();
storageMap.put("status", 0);
int count = wmsWarehouseService.findCountStorageNum(storageMap);
System.out.println("数量" + count);
}
service 实现类
public int findCountStorageNum(Map<String, Object> params) {
return dao.selectCountStorageNum(params);
}
dao 和mapper
int selectCountStorageNum(Map<String, Object> params);
<select id="selectCountStorageNum" resultType="java.lang.Integer">
SELECT count(*) FROM wms_warehouse WHERE del_flag = 0
<if test="subordinate != '' and subordinate != null">
AND subordinate = #{subordinate}
</if>
<if test="status != '' and status != null">
AND status = #{status}
</if>
</select>
后来发现是传参数的问题:
这不应该传入0,应该把0加上引号改为字符串。改后执行测试类,效果如下:
OK
总结:mybatis传入参数,如果是0或1时要加上引号,mybatis会默认数值0为false,1为true。