动态SQL其实还是SQL。
与普通SQL的区别就在于,动态SQL可以进行逻辑判断。普通SQL语句需要进行手动编写。
choose标签就类似于java中的switch。从多个条件中选择一个进行查找。
<select id="queryBlogIf" resultType="blog" parameterType="map">
select * from mybatis.blog where
<choose>
<when test="title!=null">
title = #{title}
</when>
<when test="author!=null">
author = #{author}
</when>
<otherwise>
views = #{views}
</otherwise>
</choose>
</select>
这里面不需要and的原因就是这些语句只会执行一条,所以不存在需要使用and 的情况。
<set>标签用来更新操作,可以自动删除无关逗号,看几个例子就照着会写了。
<update id="updateBlog" parameterType="map" >
update mybatis.blog
<set>
<if test="title!=null">
title = #{title},
</if>
<if test="author!=null">
author = #{author},
</if>
<if test="views!=null">
views = #{views}
</if>
</set>
where id = #{id}
</update>