1. if单个条件判断,用以实现条件筛选
<select id="selectUserInfo" parameterType="map" resultType="map">
??select * from user_info
??where 1=1
??<if test="userSex !=null and userSex !='' ">
?? and userSex=#{userSex}
??</if>
??<if test="userName !=null and userName !='' ">
?? and userName like CONCAT('%',#{userName},'%')
??</if>
</select>
2. foreach用于更新或保存数据时的批量操作
insert into user_info(
userName,
userSex
)values
<foreach item="item" index="index" collection="userList" separator="," >
(
#{item.userName},
#{item.userSex}
)
</foreach>
insert into user_info(
userName
)values
<foreach item="item" index="index" collection="userList" separator="," >
(
#{userName}
)
</foreach>
update user_info
set userAge=#{userAge}
where id in
<foreach collection="keyIds" index="index" item="item" separator="," open="(" close=")">
#{item}
</foreach>
3. choose/when/otherwise用以实现条件的多种判断,类似与if else
<select id="selectUserInfo" parameterType="map" resultType="map">
??select * from user_info
??where 1=1
??<choose>
?? <when test="userFlag!=null and userFlag!='' and userFlag=='Y'">
?? and id<=100
?? </when>
?? <when test="userFlag!=null and userFlag!='' and userFlag=='N'">
?? and id <=200
?? </when>
?? <otherwise>
?? and id<=300
?? </otherwise>
??</choose>
</select>
4. where只会在子元素返回任何内容的情况下才插入 “WHERE” 子句,并且可以自动处理判断条件语句返回的第一个and或or
<select id="selectUserInfo" parameterType="map" resultType="map">
??select * from user_info
??<where>
??<if test="userSex !=null and userSex !='' ">
?? userSex=#{userSex}
??</if>
??<if test="userName !=null and userName !='' ">
?? and userName like CONCAT('%',#{userName},'%')
??</if>
??</where>
</select>
5. set可以动态更新需要更新的列,忽略其它不更新的列
<update id="updateUserInfo" parameterType="map">
??update ?user_info
??<set>
??<if test="userName!= null and userName!=''">
??userName=#{userName},
??</if>
??userSex=#{userSex}
??</set>
??where id=#{keyId}
</update>