Mybaits动态SQL之where标签
where标签是用来管理where子句的。使用该标签后,就不需要写 where 1=1
没有使用where标签
<select id="query" resultType="user" parameterType="user">
select
*
from
t_user
where
1=1
<if test="name != null">
and name=#{name}
</if>
<if test="age > 0">
and age = #{age}
</if>
<if test="id!=null and id > 0">
and id = #{id}
</if>
</select>
使用where标签来管理
<select id="query1" resultType="user" parameterType="user">
select
*
from
t_user
<where>
<if test="name != null">
and name=#{name}
</if>
<if test="age > 0">
and age = #{age}
</if>
<if test="id!=null and id > 0">
and id = #{id}
</if>
</where>
</select>
总结: