1. 程式人生 > 資料庫 >Mybatis4 之Mybatis動態sql的實現程式碼

Mybatis4 之Mybatis動態sql的實現程式碼

1.什麼是動態SQL

傳統的使用JDBC的方法,相信大家在組合複雜的的SQL語句的時候,需要去拼接,稍不注意哪怕少了個空格,都會導致錯誤。Mybatis的動態SQL功能正是為了解決這種問題, 其通過 if,choose,when,otherwise,trim,where,set,foreach標籤,可組合成非常靈活的SQL語句,從而提高開發人員的效率。

SQL語句不固定,會根據前臺使用者的操作而進行變化的SQL語句,可以被稱之為動態SQL. 在MyBatis中,提供了一組標籤,用於方便的實現動態SQL,不需要通過java程式碼拼接字串了.
###2.動態sql中的標籤

1. <if>

用於條件判斷,test屬性表示判斷結果,要求是一個boolean.

2.<where>

用於維護where子句,通常配合一起使用. 如下功能:
a)當沒有條件時,不會建立WHERE關鍵字;
b)當有條件時,會自動生成WHERE關鍵字;
c)會自動去掉第一個條件的and/or關鍵字.

3.<choose><when><otherwise>

功能類似於switch…case…default,表示多分支判斷,只能成立一個條件

<mapper namespace="com.bjsxt.mapper.UserMapper">
 <select id="selByCondition" resultType="user">
 select * from tb_user
 <where>
  <if test="id != null">
  and id=#{id}
  </if>
  <if test="username != null and username != ''">
  and username=#{username}
  </if>
  <if test="age != null">
  and age &lt;&gt; #{age}
  </if>
  <choose>
  <when test="birthday != null and birthday != ''">
   and birthday = #{birthday}
  </when>
  <otherwise>
   and birthday is null
  </otherwise>
  </choose>
 </where>
 </select>
</mapper>

4.<bind>

對引數進行加工,通常用於模糊查詢給引數加萬用字元

<select id="sel2" resultType="user">
 <include refid="base_sql" />
 <where>
 <if test="realname != null and realname != ''">
  <bind name="realname" value="'%' + realname + '%'"/>
  and realname like #{realname}
 </if>
 </where>
</select>

5.<include>

配合使用,用於提取通用sql語句片段,用於引用SQL片段

<sql id="base_sql">
 select
 id,username,password,realname,age,birthday,reg_time regTime
 from tb_user
</sql>
<select id="sel2" resultType="user">
 <include refid="base_sql" />
 <where>
 <if test="realname != null and realname != ''">
  <bind name="realname" value="'%' + realname + '%'"/>
  and realname like #{realname}
 </if>
 </where>
</select>

6.<set>

用於維護update語句中的set子句,特點是可以刪除多餘的逗號

<update id="upd">
 update
 tb_user
 <set>
 <if test="username != null and username != ''">
  username=#{username},</if>
 <if test="age != null">
  age=#{age}
 </if>
 </set>
 where
 id=#{id}
</update>

7.<foreach>

遍歷集合(陣列,List,Set,Map),通常用於in操作或批量新增. 屬性簡介:

a)collection: 要遍歷的集合

b)item: 迭代項

c)open: 以什麼字元開頭

d)close: 以什麼字元結束

e)separator: 多個迭代項之間的分隔符

<delete id="delBatch">
 delete from tb_user
 <where>
 id in
 <foreach collection="ids" item="id" open="(" close=")" separator=",">
  #{id}
 </foreach>
 </where>
</delete>

8.<trim>

在語句的前後進行追加和去除指定的字元.

<insert id="insBatch">
 insert into tb_user values
 <foreach collection="users" item="user" separator=",">
 <trim prefix="(" prefixOverrides="," suffix=")" suffixOverrides=",">,default,#{user.username},#{user.password},#{user.realname},#{user.age},#{user.birthday},now(),</trim>
 </foreach>
</insert>

知識點補充:靜態sql與動態sql有什麼區別

SQL 語句從編譯和執行的角度可以分為兩種,靜態 SQL和 動態 SQL,這兩種 SQL 在使用方式、執行機制和效能表現等方面各有特點 :

靜態 SQL:靜態 SQL 語句一般用於嵌入式 SQL 應用中,在程式執行前,SQL 語句必須是確定的,例如 SQL 語句中涉及的列名和表名必須是存在的。靜態 SQL 語句的編譯是在應用程式執行前進行的,編譯的結果會儲存在資料庫內部。而後程式執行時,資料庫將直接執行編譯好的 SQL 語句,降低執行時的開銷。

動態 SQL:動態 SQL 語句是在應用程式執行時被編譯和執行的,例如,使用 DB2 的互動式工具 CLP 訪問資料庫時,使用者輸入的 SQL 語句是不確定的,因此 SQL 語句只能被動態地編譯。動態 SQL 的應用較多,常見的 CLI 和 JDBC 應用程式都使用動態 SQL。

靜態sql:語句型別在程式設計時候必須是確定好的。比如

select * from employee where empno='abc'
select * from employee where empno='12'

都必須是確定的,唯一可以變化的是abc的值。

動態sql:語句型別可以在執行期間指定,比如clp就是最典型的動態sql程式,你可以輸入任何命令。

靜態sql的存取路徑是在執行前就確定好的,而動態sql的存取路徑是在執行時動態生成的。因此生成的存取計劃相對更優,但考慮到生成存取路徑的開銷,有可能應用程式的執行時間相對會比靜態sql長些。

總結

到此這篇關於Mybatis4 之Mybatis動態sql的實現程式碼的文章就介紹到這了,更多相關mybatis動態sql內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!