if 的用法
<select id="dynamicIfTest" parameterType="Blog" resultType="Blog">
select * from t_blog where 1 = 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>
2.3 if 和 set 使用组合 更新 update
<update id="updateZSInfo" parameterType="map">
UPDATE tb_OrdinaryUser
<set>
<if test="UserName != null">
UserName = #{UserName},
</if>
<if test="Phone != null">
Phone = #{Phone},
</if>
<if test="Email != null">
Email = #{Email},
</if>
</set>
WHERE userID = #{UserId}
</update>
三 . trim 关键字的使用
3.1 trim 替代 where
<select id="if_trim" resultMap="studentEntity">
SELECT ST.STUDENT_ID,
ST.STUDENT_NAME,
FROM STUDENT_TBL ST
<trim prefix="WHERE" prefixOverrides="AND|OR">
<if test="studentName !=null ">
ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName, jdbcType=VARCHAR}),'%')
</if>
<if test="studentSex != null and studentSex != '' ">
AND ST.STUDENT_SEX = #{studentSex, jdbcType=INTEGER}
</if>
<if test="studentBirthday != null ">
AND ST.STUDENT_BIRTHDAY = #{studentBirthday, jdbcType=DATE}
</if>
</trim>
</select>
3.2 trim代替 set
<update id="update_trim" parameterType="StudentEntity">
UPDATE STUDENT_TBL
<trim prefix="SET" suffixOverrides=",">
<if test="studentName != null and studentName != '' ">
STUDENT_TBL.STUDENT_NAME = #{studentName},
</if>
<if test="studentSex != null and studentSex != '' ">
STUDENT_TBL.STUDENT_SEX = #{studentSex},
</if>
</trim>
WHERE STUDENT_TBL.STUDENT_ID = #{studentId}
</update>
四 . choose
4.1 choose 是集合 when 和 otherwise 使用的
<select id="choose" resultMap="studentEntity" parameterType="StudentEntity">
SELECT ST.STUDENT_ID,
ST.STUDENT_NAME,
FROM STUDENT_TBL ST
<where>
<choose>
<when test="studentName !=null ">
ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName, jdbcType=VARCHAR}),'%')
</when >
<when test="studentBirthday != null ">
AND ST.STUDENT_BIRTHDAY = #{studentBirthday, jdbcType=DATE}
</when >
<otherwise>
</otherwise>
</choose>
</where>
</select>
链接 https://blog.csdn.net/kongbaidepao/article/details/48056449