mybatis处理查询条件缺失的问题,原因是对象是int型

今天碰到一个很诡异的问题,后台管理系统输入了搜索条件,查出来的数据不对,前端查出来37条,我用条件去数据库查是12条。

然后打印执行的sql发现少了后面的条件。只带了时间,没有带上城市id和小区id。下面是sql的where 条件的mybatis配置文件:

<sql id="param">
		where 1 = 1
		<if test="res.start_date != null">
			and c.open_time > #{res.start_date}
		</if>
		<if test="res.end_date != null">
			<![CDATA[ and c.open_time < #{res.end_date} ]]>
		</if>
		<if test="res.city_id>0 and res.com_id=0">
			and com.city_id = #{res.city_id}
		</if>
		<if test="res.city_id>0 and res.com_id>0">
			and com.city_id = #{res.city_id}
			and com.id = #{res.com_id}
		</if>
</sql>

Dao层相关代码片段是:

 public List<CardRecord> paramList(@Param("res") Resident resident, @Param("limit") Limit limit);

然后我就怀疑取Resident对象中的city_id和com_id是空,不然怎么执行的sql的where条件没带上。

调试的时候查看执行查询之前的resident对象,是有值的。

接着看执行之后的结果:控制台打印的sql还是没有city_id和com_id的条件:

这就很奇怪了,为啥没带上。 这就要找问题所在了。

我首先先把时间条件注释掉,看看结果:

那就是没有时间条件,还是没有city_id、com_id,那就是对象传值过程中没传成功了。

我就想身为String对象的时间传成功了,是不是问题出现在对象上,city_id和com_id都是int型的。

通过查资料才知道,原来是mybatis处理int型时,0判定为'',就是空,如果不能0,就不为空就好,大于0这种条件结果就是false了,改成这样就可以了。搞定:)

	<sql id="param">
		where 1 = 1
		<if test="res.start_date != null">
			and c.open_time > #{res.start_date}
		</if>
		<if test="res.end_date != null">
			<![CDATA[ and c.open_time < #{res.end_date} ]]>
		</if>
		<if test="res.city_id != null and res.com_id == null">
			and com.city_id = #{res.city_id}
		</if>
		<if test="res.city_id != null and res.com_id != null">
			and com.city_id = #{res.city_id}
			and com.id = #{res.com_id}
		</if>
	</sql>

 

 

 

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值