用于实现动态SQL的元素主要有
-
if
用于判断 示例
<update id="upda" parameterType="User">
update smbms_user
<set>
<!--修改时可以判断userCode是否是空的如果不为空就把数据库中这一列的值更改掉
如果为空就不修改这一列数据库这一列的值也不会为Null-->
<if test="userCode!=null and userCode!=''">
userCode=#{userCode},
</if>
<if test="userName!=null and userName!=''">
userName=#{userName},
</if>
<if test="userPassword!=null and userPassword!=''">
userPassword=#{userPassword},
</if>
<if test="gender!=null and gender!=''">
gender=#{gender},
</if>
<if test="phone!=null and phone!=''">
phone=#{phone},
</if>
<if test="address!=null and address!=''">
address=#{address},
</if>
<if test="userRole!=null and userRole!=''">
userRole=#{userRole},
</if>
<if test="createdBy!=null and createdBy!=''">
createdBy=#{createdBy},
</if>
</set>
where id=#{id}
</update>
-
trim
trim 属性 prefix suffix prefixOverrides suffixOverrides 更灵活地去除多余关键字 替代where和set
if+trim 使用if+trim替代if+set进行更新用户表数据,效果一样的 如下:
<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">userCode = #{userName },</if>
<if test="userPassword!= null">userPassword=#{userPassword },</if>
</trim>
</update>
其中:
prefix表示有一个if成立则插入where语句
suffix表示后缀,插入到最后,与perfix正好相反
suffixOverrides="xxx"表示如果最后生成的sql语句多一个xxx,则自动去掉
prefixOverrides的意思是去掉前缀,和suffixOverrides的使用正好相反
这里如果任意一个<if>条件为true,<trim>元素会插入WHERE,并且移除紧跟where后面的(and或or)
-
where
SELECT u.*,r.roleName,r.roleCode FROM smbms_user u INNER JOIN smbms_role r ON u.userrole=r.id
<where>
<!-- where相当于 select * from pet where id=1 中的where一样 -->
<if test="usercode !=null and usercode !=''">
AND userCode LIKE CONCAT('%',#{usercode},'%')
</if>
<if test="userrole!=0">
AND userRole=#{userrole}
</if>
<if test="gender!=null and gender!=0">
AND gender=#{gender}
</if>
</where>
-
set
<update id="upda" parameterType="User">
update smbms_user
<set><!-- 与修改时搭配使用我们修改set的sql语句的‘,’的最后一列会被自动省略 -->
<if test="userCode!=null and userCode!=''">
userCode=#{userCode},
</if>
<if test="userName!=null and userName!=''">
userName=#{userName},
</if>
<if test="userPassword!=null and userPassword!=''">
userPassword=#{userPassword},
</if>
<if test="gender!=null and gender!=''">
gender=#{gender},
</if>
<if test="phone!=null and phone!=''">
phone=#{phone},
</if>
<if test="address!=null and address!=''">
address=#{address},
</if>
<if test="userRole!=null and userRole!=''">
userRole=#{userRole},
</if>
<if test="createdBy!=null and createdBy!=''">
createdBy=#{createdBy},
</if>
</set>
where id=#{id}
</update>
-
choose(when、otherwise)
相当于Java中switch语句 当when有条件满足的时候,就跳出choose
<choose>
<when test ="条件1"> …</when>
<when test ="条件2"> …</when>
<when test ="条件3"> …</when>
…
<otherwise>…</otherwise>
</choose>
-
foreach
迭代一个集合,通常用于in条件 属性 item index collection:必须指定 list array map-key open separator close
<select id="getUserName" resultType="User" parameterType="java.util.List">
select * from smbms_user where userCode in
<foreach collection="list" open="(" close=")" item="userCode" separator=",">
#{userCode}
</foreach>
</select>