mybatis 中的where标签

时间:2023-03-09 06:58:06
mybatis   中的where标签

mybatis中的where标签可以去除 开头的 and 或者 or 但是放在后面的不行

失败的:

<select id="countNotesByParam" parameterType="map"       resultType="int">
select
count(*)
from
cn_note
<where>
<if test="userId !=null">
cn_user_id= #{userId} and
</if>
<if test="statusId !=null">
cn_note_status_id= #{statusId}
</if>
</where>
</select>

and 放在后面不能自动去除

成功:

<select id="countNotesByParam" parameterType="map" resultType="int">
select
count(*)
from
cn_note
<where>
<if test="userId !=null">
cn_user_id= #{userId}
</if>
<if test="statusId !=null">
and cn_note_status_id= #{statusId}
</if>
</where>
</select>

如果不放在规定位置  也可以使用 trim标签