默认mybtis的xml会删除多余的“and”,并且条件不成立也不会生成where标签。
示例:
<select id="getZisdateYesCountsVo" resultType="com.cw.bean.vo.ZisdateYesCountsVo">
SELECT
*
FROM
myTable t
<where>
<if test="name2 != '' and name2 != null">
t.NAME2 = #{name2}
</if>
</where>
</select>
但如果在where标签中添加注释,可能会被mybaits认为是where中的一个条件
<select id="getZisdateYesCountsVo" resultType="com.cw.bean.vo.ZisdateYesCountsVo">
SELECT
*
FROM
myTable t
<where>
-- 如果有注释的情况下,会误以为不为空,可能会出现不删除多余的where和and
<if test="name2 != '' and name2 != null">
t.NAME2 = #{name2}
</if>
</where>
</select>
解决办法:
1. 可以放到if标签内
2. where中有必须有的条件比如1=1
<if test="name2 != '' and name2 != null">
-- 注释放到标签内不会影响
t.NAME2 = #{name2}
</if>