一、if數據庫
<select id="findActiveBlogLike" resultType="Blog"> SELECT * FROM BLOG <where> <if test="state != null"> state = #{state} </if> <if test="title != null"> AND title like #{title} </if> <if test="author != null and author.name != null"> AND author_name like #{author.name} </if> </where> </select> or <select id="findActiveBlogLike" resultType="Blog"> SELECT * FROM BLOG where 1=1 <if test="state != null"> state = #{state} </if> <if test="title != null"> AND title like #{title} </if> <if test="author != null and author.name != null"> AND author_name like #{author.name} </if> </select>
二、choose、when、otherwiseoracle
<select id="findActiveBlogLike" resultType="Blog"> SELECT * FROM BLOG WHERE state = ‘ACTIVE’ <choose> <when test="title != null"> AND title like #{title} </when> <when test="author != null and author.name != null"> AND author_name like #{author.name} </when> <otherwise> AND featured = 1 </otherwise> </choose> </select>
三、trim、set
<trim prefix="" suffix="" suffixOverrides="" prefixOverrides=""></trim>
prefix: 添加前綴
suffix: 添加後綴
suffixOverrides: 去除前綴
prefixOverrides: 去除後綴dom
<insert id="insert" parameterType="com.tortuousroad.groupon.cart.entity.Cart"> insert into cart <trim prefix="(" suffix=")" suffixOverrides=","> <if test="id != null"> id, </if> <if test="userId != null"> user_id, </if> <if test="dealId != null"> deal_id, </if> <if test="dealSkuId != null"> deal_sku_id, </if> </trim> <trim prefix="values (" suffix=")" suffixOverrides=","> <if test="id != null"> #{id,jdbcType=BIGINT}, </if> <if test="userId != null"> #{userId,jdbcType=BIGINT}, </if> <if test="dealId != null"> #{dealId,jdbcType=BIGINT}, </if> <if test="dealSkuId != null"> #{dealSkuId,jdbcType=BIGINT}, </if> </trim> </insert>
setide
<update id="updateAuthorIfNecessary"> update Author <set> <if test="username != null">username=#{username},</if> <if test="password != null">password=#{password},</if> <if test="email != null">email=#{email},</if> <if test="bio != null">bio=#{bio}</if> </set> where id=#{id} </update>
四、foreachcode
<select id="selectPostIn" resultType="domain.blog.Post"> SELECT * FROM POST P WHERE ID in <foreach item="item" index="index" collection="list" open="(" separator="," close=")"> #{item} </foreach> </select>
五、script
要在帶註解的映射器接口類中使用動態 SQL,能夠使用 script 元素。好比:blog
@Update({"<script>", "update Author", " <set>", " <if test='username != null'>username=#{username},</if>", " <if test='password != null'>password=#{password},</if>", " <if test='email != null'>email=#{email},</if>", " <if test='bio != null'>bio=#{bio}</if>", " </set>", "where id=#{id}", "</script>"}) void updateAuthorValues(Author author);
六、bind
bind 元素容許你在 OGNL 表達式之外建立一個變量,並將其綁定到當前的上下文。好比:接口
<select id="selectBlogsLike" resultType="Blog"> <bind name="pattern" value="'%' + _parameter.getTitle() + '%'" /> SELECT * FROM BLOG WHERE title LIKE #{pattern} </select>
七、多數據庫支持
若是配置了 databaseIdProvider,你就能夠在動態代碼中使用名爲 「_databaseId」 的變量來爲不一樣的數據庫構建特定的語句。好比下面的例子:ip
<insert id="insert"> <selectKey keyProperty="id" resultType="int" order="BEFORE"> <if test="_databaseId == 'oracle'"> select seq_users.nextval from dual </if> <if test="_databaseId == 'db2'"> select nextval for seq_users from sysibm.sysdummy1" </if> </selectKey> insert into users values (#{id}, #{name}) </insert>
5、SQL 語句構建器get