Mybatis 中 Integer 类型参数问题

前两天在项目开发遇到了Mybatis 中 Integer 类型参数传递问题.项目中代码如下

<if test="req.type != null and req.type !=''">
      AND type = #{req.type}
</if>
使用实体类传递参数时,若属性为 varchar 类型,则一般情况下使用上面的写法;若有些属性类型设置为 Integer 类型,如果依旧沿用上面写法,会有以下问题:
1、不传入 state 值(则state为null),则无法进入 if 条件内;
2、传入 state 为 0 以外的数字,则成功进入 if 条件内;
3、传入 state 为 0 ,则无法进入进入 if 条件内,原因:mybatis 在解析Integer类型数据时,如果数据值为0,会将0解析为空字符串。

因此,传递 Integer 类型参数时,正确写法如下:
<if test="req.type != null ">
    AND type = #{req.type}
</if>
 
转载 链接:https://www.jianshu.com/p/491a94c8f740
posted @ 2022-06-15 09:55  蜗牛学编程  阅读(664)  评论(0编辑  收藏  举报