溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么在MyBatis中使用動態(tài)SQL標簽

發(fā)布時間:2021-04-16 15:52:17 來源:億速云 閱讀:170 作者:Leah 欄目:開發(fā)技術(shù)

這篇文章將為大家詳細講解有關(guān)怎么在MyBatis中使用動態(tài)SQL標簽,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。

1.MyBatis動態(tài)SQL

MyBatis 的強大特性之一便是它的動態(tài) SQL,即拼接SQL字符串。如果你有使用 JDBC 或其他類似框架的經(jīng)驗,你就能體會到根據(jù)不同條件拼接 SQL 語句有多么痛苦。拼接的時候要確保不能忘了必要的空格,還要注意省掉列名列表最后的逗號。利用動態(tài) SQL 這一特性可以徹底擺脫這種痛苦。

通常使用動態(tài) SQL 不可能是獨立的一部分,MyBatis 當然使用一種強大的動態(tài) SQL 語言來改進這種情形,這種語言可以被用在任意的 SQL 映射語句中。

動態(tài) SQL 元素和使用 JSTL 或其他類似基于 XML 的文本處理器相似。在 MyBatis 之前的版本中,有很多的元素需要來了解。MyBatis 3 大大提升了它們,現(xiàn)在用不到原先一半的元素就可以了。MyBatis 采用功能強大的基于 OGNL 的表達式來消除其他元素。

2.動態(tài)SQL標簽:if,choose (when, otherwise),trim (where, set),foreach

2.1 if標簽:直接上代碼

<select id="queryByIdAndTitle"
     resultType="Blog">
  SELECT * FROM BLOG 
  WHERE 1=1 
  <if test="id!= null and title!=null">
    AND id=#{id} and title=#{title}
  </if>
</select>

注:if標簽一般用于非空驗證,如上例,若id為空,if標簽里的代碼,將不會執(zhí)行,反之,則會執(zhí)行。

2.2 choose(when,otherwise)標簽:直接上代碼

<select id="queryBy"
     resultType="Blog">
  SELECT * FROM BLOG WHERE 1=1
  <choose>
    <when test="title != null">
      AND title like #{title}
    </when>
    <otherwise>
      AND id= 1
    </otherwise>
  </choose>
</select>

注:choose(when,otherwise)標簽相當于switch(case,default) ,如上例,若title 為空,when標簽里的代碼,將不會執(zhí)行,默認執(zhí)行otherwise標簽里面的代碼。

2.3 trim(where,set)標簽:直接上代碼

<select id="queryBy" resultType="com.scme.pojo.User" parameterType="com.scme.pojo.User">
                 select * from user 
                 <where>
                         <if test="username!=null and password!=null">
                             and username=#{username} and password=#{password}
                         </if>
                 </where>
</select>

注:假設(shè)上例傳入的username,password不為空,代碼就可以運行成功!但朋友們可能有疑問了,實際上執(zhí)行的sql語句是什么呢?其實,sql為:select * from user

 where username=? and password=?  朋友們是否發(fā)現(xiàn),where標簽代替了sql中where關(guān)鍵字,但if中的and不見了。其實where標簽可以自動去除是“AND”或“OR”開頭的sql中的“AND”或“OR”關(guān)鍵字。

如果 where 元素沒有按正常套路出牌,我們還是可以通過自定義 trim 元素來定制sql,實現(xiàn)where標簽的效果。代碼如下:

<select id="queryBy" resultType="com.scme.pojo.User" parameterType="com.scme.pojo.User">
                 select * from user 
                 <trim prefix="WHERE" prefixOverrides="AND |OR ">
                          <if test="username!=null and password!=null">
                             and username=#{username} and password=#{password}
                         </if>
                </trim>
                 <!-- 效果同上
          <where>
                         <if test="username!=null and password!=null">
                             and username=#{username} and password=#{password}
                         </if>
                 </where> -->
         </select>

set標簽,代碼如下:

<update id="updateUser" parameterType="com.scme.pojo.User">
                 update user 
                 <set>
                     <if test="username!=null">
                             username=#{username}
                     </if>
                 </set>
                 <where> 
                     <if test="id!=null">
                             id=#{id}
                     </if>
                 </where>
         </update>

注:set標簽功能和where標簽差不多,set標簽代替了sql中set關(guān)鍵字,set標簽可以自動去除sql中的多余的“,”

同理,trim標簽也可以實現(xiàn)set標簽的功能

<update id="updateUser" parameterType="com.scme.pojo.User">
                 update user 
         <trim prefix="set" prefixOverrides=",">
              <if test="username!=null"> username=#{username} </if> 
        </trim>
  
         <where> 
    
            <if test="id!=null"> id=#{id} </if> 
          
        </where>
 </update>

2.4 foreach標簽:foreach標簽實現(xiàn)批量刪除,直接上代碼

<delete id="batchDelete" parameterType="java.lang.String">
  delete from user
  where id in
  <foreach item="id" index="index" collection="list"
      open="(" separator="," close=")">
        #{id}
  </foreach>
</delete >

注:foreach標簽可迭代任何對象(如列表、集合等)和任何的字典或者數(shù)組對象傳遞給foreach作為集合參數(shù),當使用可迭代對象或者數(shù)組時,index是當前迭代的次數(shù),item的值是本次迭代獲取的元素。當使用字典(或者Map.Entry對象的集合)時,index是鍵,item是值。collection標簽可以填('list','array','map')。

foreach元素的屬性主要有 item,index,collection,open,separator,close。

item表示集合中每一個元素進行迭代時的別名;

index指 定一個名字,用于表示在迭代過程中,每次迭代到的位置;

open表示該語句以什么開始,

separator表示在每次進行迭代之間以什么符號作為分隔符;

close表示以什么結(jié)束。

3.bind

bind 元素可以從 OGNL 表達式中創(chuàng)建一個變量并將其綁定到上下文。比如:

<select id="selectBlogsLike" resultType="Blog">
  <bind name="pattern" value="'%' + _parameter.getTitle() + '%'" />
  SELECT * FROM BLOG
  WHERE title LIKE #{pattern}
</select>

4.Multi-db vendor support

一個配置了“_databaseId”變量的 databaseIdProvider 對于動態(tài)代碼來說是可用的,這樣就可以根據(jù)不同的數(shù)據(jù)庫廠商構(gòu)建特定的語句。比如下面的例子:

<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>

動態(tài) SQL 中可插拔的腳本語言

MyBatis 從 3.2 開始支持可插拔的腳本語言,因此你可以在插入一種語言的驅(qū)動(language driver)之后來寫基于這種語言的動態(tài) SQL 查詢。

可以通過實現(xiàn)下面接口的方式來插入一種語言:

public interface LanguageDriver {
  ParameterHandler createParameterHandler(MappedStatement mappedStatement, Object parameterObject, BoundSql boundSql);
  SqlSource createSqlSource(Configuration configuration, XNode script, Class<?> parameterType);
  SqlSource createSqlSource(Configuration configuration, String script, Class<?> parameterType);
}

一旦有了自定義的語言驅(qū)動,你就可以在 mybatis-config.xml 文件中將它設(shè)置為默認語言:

<typeAliases>
  <typeAlias type="org.sample.MyLanguageDriver" alias="myLanguage"/>
</typeAliases>
<settings>
  <setting name="defaultScriptingLanguage" value="myLanguage"/>
</settings>

除了設(shè)置默認語言,你也可以針對特殊的語句指定特定語言,這可以通過如下的 lang 屬性來完成:

<select id="selectBlog" lang="myLanguage">
  SELECT * FROM BLOG
</select>

或者在你正在使用的映射中加上注解 @Lang 來完成:

public interface Mapper {
  @Lang(MyLanguageDriver.class)
  @Select("SELECT * FROM BLOG")
  List<Blog> selectBlog();

關(guān)于怎么在MyBatis中使用動態(tài)SQL標簽就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI