<foreach>
<!-- foreach --> <delete id="delMulti" parameterType="java.util.List"> delete from user where id in <!--collection:輸入參數為List集合時,必須寫list, item:為集合里的每一項起名,可以任意定義 separator:每一項中間的分割符 open:在執行循環體之前拼接的內容; close:在執行循環體之后拼接的內容;--> <foreach collection="list" item="uid" separator="," open="(" close=")"> #{uid} </foreach> </delete>
where
<!--where:解析成where關鍵字,會自動去掉第一個符合條件的限定條件中的and --> <select id="getByNameSex" parameterType="map" resultType="user"> select * from user <where> <if test="uname!=null and uname!=''"> and username like "%"#{uname}"%" </if> <if test="usex!=null and usex!=''"> and sex=#{usex} </if> </where>
choose when otherwise
<!--choose when otherwise:某個判斷滿足條件后,其他條件就不會再執行 --> <select id="getByNameSex1" parameterType="map" resultType="user"> select * from user where <choose> <when test="uname!=null and uname!=''"> username like "%"#{uname}"%" </when> <when test="usex!=null and usex!=''"> sex=#{usex} </when> <otherwise> 1=1 </otherwise> </choose> </select>
set
<!-- set:解析為set關鍵字,可以自動去掉最后一個更新的字段后面的逗號 --> <update id="updUser" parameterType="user"> update user <set> <if test="username!=null and username!=''"> username=#{username}, </if> <if test="sex!=null and sex!=''"> sex=#{sex} </if> </set> where id=#{id} </update>
trim:使用次數較少
<update id="updUser1" parameterType="user"> <!--prefix:前綴,在trim中內容執行之前拼接 suffix:后綴:在trim中內容執行之后拼接 suffixOverrides:忽略后綴 prefixOverrides:忽略前綴--> <trim prefix="update user set" suffix="where id=#{id}" suffixOverrides=","> <if test="username!=null and username!=''"> username=#{username}, </if> <if test="sex!=null and sex!=''"> sex=#{sex} </if> </trim> </update>
bind使用較少
<select id="getByUname" parameterType="string" resultType="User"> <!--name:新的字符串的變量名 --> <bind name="uname" value="'%'+_parameter+'%'"/> select * from user where username like #{uname} </select>
