if where
If标签:作为判断入参来使用的,如果符合条件,则把if标签体内的SQL拼接上
注意:用if进行判断是否为空时,不仅要判断null,也要判断空字符串‘’;
Where标签:会去掉条件中的第一个and符号。
<select id="findUserListByPar" parameterType="edu.ccit.cn.domain.pojo.UserQueryVo" resultType="user">
select * from t_user
<where>
<if test="user!=null">
<if test="user.username!=null and user.username!=''">
username='%${user.username}%'
</if>
<if test="user.classid!=null and user.classid!=''">
and classid=#{user.classid}
</if>
</if>
</where>
</select>
sql片段
提高sql重用性
<!-- sql片段 提高sql重用性-->
<sql id="select_User_where">
<if test="user!=null">
<if test="user.username!=null and user.username!=''">
username='%${user.username}%'
</if>
<if test="user.classid!=null and user.classid!=''">
and classid=#{user.classid}
</if>
</if>
</sql>
<select id="findUserListBySqlpd" parameterType="edu.ccit.cn.domain.pojo.UserQueryVo" resultType="user">
select * from user
<where>
<include refid="select_User_where"></include>
</where>
</select>
foreach拼接
<!-- sql循环拼接 -->
<select id="findUserListByforeach" parameterType="edu.ccit.cn.domain.pojo.UserQueryVo" resultType="user">
select * from t_user
<where>
<if test="ids.size>0">
<foreach collection="ids" item="id" open="and id in(" close=")" separator=",">
${id}
</foreach>
</if>
</where>
</select>
还可以直接使用list对象作为参数进行