動態sql常用標籤
<where></where>標籤的作用就在於,若你拼接的第一條語句前面有and則自動去除and
1,<if title=””></if>
<select id="queryBlogIF" parameterType="Map" resultType="Blog">
select * from mybatis.blog
<where>
<if test="title != null">
and title = #{title}
</if>
<if test="author != null">
and author = #{author}
</if>
</where>
</select>
if判斷若滿足則拼接
2,
<choose>
<when title=””>
</when>
<otherwise title =””>
</ otherwise >
</choose>
<select id="queryBlogChoose" parameterType="Map" resultType="Blog">
select * from mybatis.blog
<where>
<choose>
<when test="title!=null">
title = #{title}
</when>
<when test="author!=null">
and author = #{author}
</when>
<otherwise>
and views = #{views}
</otherwise>
</choose>
</where>
</select>
When判斷,只會拼接第一條滿足的語句,若均不滿足,則拼接otherwise語句
3,<set></set>標籤為sql語句動態新增set關鍵字,並去除無關的逗號
<update id="updateBlog" parameterType="Map">
update mybatis.blog
<set>
<if test="title!=null">
title=#{title}
</if>
</set>
where id=#{id}
</update>