运行结果以下:
java
数据库数据以下 应该查询出11条数据,从上图看输出的sql动态sql语句,if条件没执行sql
代码以下:
测试类数据库
@Test public void testStorageCount() { Map<String, Object> storageMap = new HashMap<>(); storageMap.put("status", 0); int count = wmsWarehouseService.findCountStorageNum(storageMap); System.out.println("数量" + count); }
service 实现类mybatis
public int findCountStorageNum(Map<String, Object> params) { return dao.selectCountStorageNum(params); }
dao 和mapperapp
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。测试