数据库中id属性为主键,不允许为空值
<select id="getUser"
resultType="com.hundsun.zdr.Stu">
SELECT * FROM Stu WHERE TRUE
<if test="id!=0">
and id = #{id}
</if>
<if test="name!=null">
and name like CONCAT('%',#{name},'%')
</if>
<if test="birthday!=null">
AND birthday like CONCAT('%',#{birthday},'%')
</if>
<if test="sex!=null">
AND sex like CONCAT(#{sex},'%')
</if>
<if test="address!=null">
AND address like CONCAT('%',#{address},'%')
</if>
<if test="teacher!=null">
AND teacher like CONCAT('%',#{teacher},'%')
</if>
</select>
<if test="id!=0">
and id = #{id}
</if>如果写成这样
<if test="id!=null">
and id = #{id}
</if>查询的时候保证id的值不能为空才能正常的查询到值
这个问题很困扰我
本文探讨了在MyBatis中如何正确处理ID查询的问题,特别是当ID为0而非空时如何进行有效的条件过滤,避免因错误的条件设置导致查询失败。
941

被折叠的 条评论
为什么被折叠?



