Mybatis动态sql传入int类型值为0时存在的问题.

下午开发时遇到了一个问题,解决了很久,最后经过同事帮助得以解决.

存在的问题:

同一个方法,传入不同的type值按类别来查找, 传入1可以准确找出, 传入0时却是查找出了全部数据.

int left = checkoutConsoleService.queryConsoleCount(0); //查出全部数据
	    
int right = checkoutConsoleService.queryConsoleCount(1); //准确查找出
<select id="queryConsoleCount" parameterType="int" resultType="int">
     select count(id) from t_d_checkout_console a
     where 1=1
     <if test=" _parameter !=null and _parameter !=''">
         AND a.type = #{_parameter}
     </if>
</select>

问题解决:

动态sql判断有问题,因为大多数字段都是String,属于引用类型,所以都是用not in {null, ''}来判断传入的参数是否为空。当传入的是int类型的数字0时,mybatis似乎会把它当成空字符串,所以动态sql没有拼接上。

修改成!= null之后就能正常传入0了

<select id="queryConsoleCount" parameterType="int" resultType="int">
    select count(id) from t_d_checkout_console a
    where 1=1
    <if test=" _parameter !=null">
         AND a.type = #{_parameter}
    </if>
</select>

学到的东西:

1.mapper配置文件中 当只传入一个值时, 需要使用_parameter 来当作传入的字段.

否则就会出现这个问题:

There is no getter for property named 'subjectId' in 'class java.lang.Intege

2.上述问题的解决

3.学会在debug过程中看日志信息来精准的判断问题

PS:

今天也是第一次在CSDN写博客, 希望自己能一直坚持写下去,慢慢成长吧.

猜你喜欢

转载自blog.csdn.net/weixin_43226645/article/details/86015651