mybatis中传参对sql语句的影响

1.当有类型为Intger的参数传参时,发现传0时被认为是false,使得不满足了判断条件

例如

<select id="findDataByCondition" resultMap="entity" parameterType="map">
	SELECT * from table
	WHERE 1=1
	<if test="state != null and state != ''">
            AND state = #{state}
    </if>
</select>

发现当state传值为1时,满足条件if test="state != null and state != ''",所以AND state = #{state}会进入查询条件之一,当state传值为0时,满足了state == ''也就是false,使得查询条件AND state = 0就没有被加入查询条件,所以语句应该修改为

<select id="findDataByCondition" resultMap="entity" parameterType="map">
	SELECT * from table
	WHERE 1=1
	<if test="state != null">
            AND state = #{state}
    </if>
</select>
posted @ 2024-12-24 16:17  山茶花llia  阅读(2)  评论(0编辑  收藏  举报