update t_users <trim prefix="set" suffixOverrides="," suffix=" where id = #{id} "> <if test="name != null and name.length() > 0"> name=#{name}, </if> <if test="gender != null and gender.length() > 0"> gender=#{gender}, </if> </trim>
trim标签
prefix 给sql语句拼接的前缀
suffix 给sql语句拼接的后缀
prefixOverrides 去除sql语句前面的关键字或者字符,该关键字或者字符由prefixOverrides属性指定,假设该属性指定为"AND",当sql语句的开头为"AND",trim标签将会去除该"AND"
suffixOverrides 去除sql语句后面的关键字或者字符,该关键字或者字符由suffixOverrides属性指定
<choose> <when test="entetype == 2"> SELECT e.entecode, e.pentecode,e.entename,e.entefullname,e.fsguid, CONCAT('[',e.regicode,']',IF(e.entefullname is null or e.entefullname='',e.entename,e.entefullname)) AS fullname FROM dw_enterprise e WHERE 1=1 <if test="regicode != null and regicode != ''"> and regicode=#{regicode} </if> and entetype=#{entetype} and status=1 ORDER BY e.entecode </when> <when test="entetype == 3"> SELECT e.entecode, e.pentecode,e.entename,e.entefullname,e.fsguid, CONCAT('[',(SELECT p.CODE FROM pj_printenterprise p WHERE SUBSTR(e.entecode,7)=p.FS_GUID),']',IF(e.entefullname is null or e.entefullname='',e.entename,e.entefullname)) AS fullname FROM dw_enterprise e WHERE 1=1 <if test="regicode != null and regicode != ''"> and regicode=#{regicode} </if> and entetype=#{entetype} AND status=1 ORDER BY e.entecode </when> <otherwise> SELECT e.entecode, e.pentecode, e.entename, e.entefullname, e.fsguid, CONCAT( '[', IFNULL( e.licencecode, e.fscode ), ']', IF( e.entefullname != '', e.entefullname, e.entename )) AS fullname, e.chargeCard FROM dw_enterprise e WHERE e.STATUS = 1 <if test="entetype != null and entetype!= ''"> AND e.entetype = #{entetype} </if> <if test="regicode != null and regicode != ''"> AND regicode = #{regicode} </if> ORDER BY e.source DESC, e.licencecode </otherwise> </choose>
选择语句
<select id="selectByList" parameterType="java.util.List" resultType="user"> select * from user <where> <!-- 传递List,List中是pojo --> <if test="list!=null"> <foreach collection="list" item="item" open="and id in(" separator="," close=")"> #{item.id} </foreach> </if> </where> </select>
foreach 说明
你可以传递一个 list 实例或者 array 数组作为参数对象传给 MyBatis。当你这么做的时候,MyBatis 会自动将它包装在一个 Map 中,用名称作为键。list 实例将会以 list 作为键,而 array 数组实例将会以 array 作为键
foreach 元素的属性主要有 item,index,collection,open,separator,close
item:表示集合中每一个元素进行迭代时的别名
index:指定一个名字,用于表示在迭代过程中,每次迭代到的位置
open:表示该语句以什么开始
separator:表示在每次进行迭代之间以什么符号作为分隔符
close:表示以什么结束
collection:循环值的类型
<if test="enteType != ''"> <foreach collection="enteType.split(',')" item="item" open="and u.enteType in(" separator="," close=")"> #{item} </foreach> </if>