不建议使用,但是有些需求又不得不用回来温习下。动态SQL语句,也就意味着SQL语句不在是一成不变的而是具有多样性.
if的用法还是跟平常差不多的(不过没有else if也没有else)
<update id="modify" parameterType="User">
UPDATE `smbms_user`
<trim prefix="set" suffixOverrides="," suffix="where id=#{id}">
<if test="userCode!=null">`userCode` = #{userCode} , if>
<if test="userName!=null">`userName` = #{userName} , if>
<if test="userPassword!=null">`userPassword` = #{userPassword} , if>
<if test="gender!=null">`gender` = #{gender} , if>
<if test="birthday!=null">`birthday` = #{birthday} ,if>
<if test="phone!=null">`phone` = #{phone} , if>
<if test="address!=null">`address` = #{address} , if>
<if test="userRole!=null">`userRole` = #{userRole} , if>
<if test="createdBy!=null">`createdBy` = #{createdBy} , if>
<if test="creationDate!=null">`creationDate` = #{creationDate} , if>
<if test="modifyBy!=null">`modifyBy` = #{modifyBy}if>
trim>
update>
如上面的代码,如果是空的字段则不执行更新操作
choose就相当于Java中的switch,when相当于case,otherwise相当于default
<select id="getBill" resultType="Bill">
SELECT * FROM smbms_bill b JOIN smbms_provider p ON b.providerId=p.id
WHERE 1=1
<choose>
<!-- 条件为false则继续执行下面的when,反之亦然如此 -->
<when test="id!=null">
AND b.providerId=#{id}
</when>
<!-- 条件为true则不会执行otherwise,反之亦然如此 -->
<when test="proName!=null">
AND p.proName LIKE CONCAT('%',#{proName},'%')
</when>
<!-- 当都不满足时执行默认值 -->
<otherwise>
AND p.proContact LIKE CONCAT('%张%')
</otherwise>
</choose>
</select>
此标签可以忽略条件中多余的and和or如下
SELECT * FROM smbms_bill b JOIN smbms_provider p ON b.providerId=p.id
<where>
<if test="id!=null">
AND b.providerId=#{id}
</if>
<if test="proName!=null">
AND p.proName LIKE CONCAT('%',#{proName},'%')
</if>
</where>
假设id为1 proName为北京 两个if都满足条件时就会拼凑成这样
SELECT * FROM smbms_bill b JOIN smbms_provider p ON b.providerId=p.id WHERE b.providerId=1 AND p.proName LIKE CONCAT('%北京%')
其实解决这种情况不用where也是可以的,只需在前面加上一个为真的条件即可
SELECT * FROM smbms_bill b JOIN smbms_provider p ON b.providerId=p.id
where 1=1
<if test="id!=null">
AND b.providerId=#{id}
</if>
<if test="proName!=null">
AND p.proName LIKE CONCAT('%',#{proName},'%')
</if>
效果也是一样的
此标签可以忽略多余的逗号
UPDATE `smbms_user`
<set>
<if test="userPassword!=null">
`userPassword` = #{userPassword},
</if>
</set>
WHERE `id` = #{id} ;
此标签属性如下
栗子1
<!-- 添加内容前面加上 where 并且忽略 and或者or -->
<trim prefix="where" prefixOverrides="and|or">
<if test="userRole!=null">
and userRole=#{userRole}
</if>
<if test="userName!=null and userName!=''">
and u.userName like CONCAT('%',#{userName},'%')
</if>
order by creationDate DESC limit #(from),#(pageSize)
</trim>
栗子2
UPDATE `smbms_user`
<!-- 在内容前加上 set 并且忽略内容最后的逗号 且在内容后面加上查询条件 -->
<trim prefix="set" suffixOverrides="," suffix="where id=#{id}">
<if test="userCode!=null">`userCode` = #{userCode} , </if>
<if test="userName!=null">`userName` = #{userName} , </if>
</trim>
此标签的属性如下
栗子1
当入参为list时
<select id="getUSerByRoleId_foreach_list" resultMap="userMapByRole">
select * from smbms_user where userRole in
<!-- 因为此处用了in,因此以 "("开头 ,")"结尾 ,","分隔 -->
<foreach collection="list" item="roleList" open="(" separator="," close=")">
#{roleList}
</foreach>
</select>
栗子2
当入参为数组时
<select id="getUserByRoleId_foreach_array" resultMap="userMapByRole">
select * from smbms_user where userRole in
<!-- 因为此处用了in,因此以 "("开头 ,")"结尾 ,用","分隔 -->
<foreach collection="array" item="roleIds" open="(" separator="," close=")">
#{roleIds}
</foreach>
</select>
栗子3
当为map时
<select id="getProvider" resultMap="providerresult">
SELECT * FROM smbms_bill b JOIN smbms_provider p ON b.providerId=p.id
where b.providerId in
<!-- 因为此处用了in,因此以 "("开头 ,")"结尾 ,","分隔 此时的collection的值是map其中的一个key-->
<foreach collection="#{bibli}" item="pros" open="(" separator="," close=")">
<!-- 获取对象当中的属性 -->
#{pros.providerId}
</foreach>
</select>