1:简单的if
<select id="dynamicIfTest" parameterType="Model" resultType="Model">
select * from t_blog where 11 = 1
<if test="title != null">
and title = #{title}
</if>
<if test="content != null">
and content = #{content}
</if>
<if test="owner != null">
and owner = #{owner}
</if>
</select>
简述,这里的title != <if test="title != null"> 也支持 title != <if test="title != ' ' ">
2:choose语句
<select id="dynamicChooseTest" parameterType="Model" resultType="Model">
select * from t_blog where 11 = 1
<choose>
<when test="title != null">
and title = #{title}
</when>
<when test="content != null">
and content = #{content}
</when>
<otherwise>
and owner = "owner1"
</otherwise>
</choose>
</select>
简述,当when中有条件满足的时候,就会跳出choose(和if else if else 差不多)如果两个when都不满足,则<otherwise>代码块就会执行。
3:where 标签
<select id="dynamicWhereTest" parameterType="Blog" resultType="Blog">
select * from t_blog
<where>
<if test="title != null">
title = #{title}
</if>
<if test="content != null">
and content = #{content}
</if>
<if test="owner != null">
and owner = #{owner}
</if>
</where>
</select>
简述,这个<where>标签的好处就是自动只能的给你添加 and 和 or 关键字,比如tile == null 而 content !=null 就会输出 select * from t_blog where content = #{content} 自动去掉 and 很只能吧!
这些基本就够用了!