Mybatis动态SQL常用标签
1、where标签使用:
<select id="queryBlogIf" parameterType="map" resultType="Blog">
select * from mybatis.blog
<where><!--会动态前置where关键字,同时自动删除拼接不需要的and-->
<if test="title!=null">
<!--如果title不为空,追加下方代码-->
title=#{title}
</if>
<if test="author!=null">
<!--如果author不为空,追加下方代码-->
and author=#{author}
</if>
</where>
</select>
2、set标签使用:
<update id="updateBlogSet" parameterType="map">
update mybatis.blog
<set><!--动态前置Set关键字,同时自动删除拼接不需要的逗号-->
<if test="title!=null">
title=#{title},
</if>
<if test="author!=null">
author=#{author},
</if>
</set>
where id=#{id}
</update>
3、顺便介绍一下choose标签使用:
<select id="queryBlogChoose" parameterType="map" resultType="Blog">
select * from mybatis.blog where
<choose><!--Choose只会选择拼接一句-->
<when test="author!=null">
author=#{author}
</when>
<when test="title!=null">
title=#{title}
</when>
<otherwise>
views=#{views}
</otherwise>
</choose>
</select>
choose标签类似Java语言的switch。choose只会选择第一个判断成立的sql语句进行拼接。