MyBatis?超詳細講解動態(tài)SQL的實現
情景:
我們在使用JDBC或者其他類似的框架開發(fā)進行數據庫開發(fā)時,通常要根據需求動手組裝SQL,不用想都覺得是件很痛苦的事情了,而Mybatis框架提供的對SQL語句動態(tài)組裝的功能,能很好地解決這個麻煩。
概述:
動態(tài)SQL是MyBatis框架的一個強大特性,MyBatis3可采用功能強大的基于OGNL的表達式來完成動態(tài)SQL,它刪除了之前版本中需要了解的大多數元素,只使用不到原來一半的元素就能完成所需的工作。
SQL元素:
SQL元素 | 說明 |
---|---|
<if> | 判斷語句,用于單條件分支判斷 |
<choose> (<when>,<otherwise>) | 相當于Java中的switch...case...default語句,用于多條件分支判斷 |
<where> | 簡化SQL語句中的where的條件判斷 |
<trim> | 可以靈活地去除多余的關鍵字 |
<set> | 解決動態(tài)更新語句 |
<foreach> | 循環(huán)語句,常用于in語句等列舉條件中 |
<bind> | 從OGNL表達式中創(chuàng)建一個變量,并將其綁定到上下文,常用于模糊查詢的SQL中 |
<if>:
使用動態(tài) SQL 最常見情景是根據條件包含 where 子句的一部分
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="org.example.mapper.BookMapper"> <resultMap id="BookType" type="org.example.po.Book"> <id property="id" column="id"></id> <result property="bookName" column="bookName"></result> <result property="price" column="price"></result> <result property="publisher" column="publisher"></result> </resultMap> <select id="bookSelectById" parameterType="int" resultMap="BookType"> select * from t_book <where> <if test="id!=null and id!=''"> id=#{id} </if> </where> </select> <select id="bookSelectByname" parameterType="string" resultMap="BookType"> select * from t_book <where> <if test="bookNmae!=null and bookName!=''"> bookName LIKE concat('%',#{bookName},'%') </if> </where> </select> </mapper>
<choose>:
有時候,我們不想使用所有的條件,而只是想從多個條件中選擇一個使用。針對這種情況,MyBatis 提供了 choose 元素,它有點像 Java 中的 switch 語句。
因為自己寫的是單查詢,引用官網代碼。
<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>
<where>:
<where>標簽相當于 where 1=1 ,主要用來簡化SQL語句中的where條件判斷,并能智能地處理and和or不必擔心多余關鍵字導致的語法錯誤。代碼參考<if>標簽代碼。
<trim>:
這個標簽我覺得是最靈活的,trim元素也會自動識別其標簽內是否有返回值,若有的話,則會在已包含的內容前加上某些前綴或者后綴(先判斷是否有語句包含),用到的屬性是
1、prefix(前綴),suffix(后綴)
還可以把包含內容的首內容或尾內容的符號去掉,用到的屬性是
2、prefixOverride(前綴覆蓋),suffixOverride(后綴覆蓋)
可以說功能很強大,可以利用它來替代where元素,并實現與where元素相同的效果。
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="org.example.mapper.BookMapper"> <resultMap id="BookType" type="org.example.po.Book"> <id property="id" column="id"></id> <result property="bookName" column="bookName"></result> <result property="price" column="price"></result> <result property="publisher" column="publisher"></result> </resultMap> <update id="bookUpdate" parameterType="book"> UPDATE t_book <trim prefix="set" suffixOverrides=","> <if test="bookName!=null and bookName!=''">bookName=#{bookName},</if> <if test="price!=null and price!=''">price=#{price},</if> <if test="publisher!=null and publisher!=''">publisher=#{publisher}</if> WHERE id=#{id} </trim> </update> </mapper>
<set>:
set元素主要用于更新操作,它的主要功能和where元素差不多,主要是包含的語句前輸入一個set,若包含的語句以逗號結束,則會自動把括號忽略掉,再配合if元素就可以動態(tài)地更新需要修改的字段;若不需要更改字段,則可以不再被更新。
把上面的代碼修改一下:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="org.example.mapper.BookMapper"> <resultMap id="BookType" type="org.example.po.Book"> <id property="id" column="id"></id> <result property="bookName" column="bookName"></result> <result property="price" column="price"></result> <result property="publisher" column="publisher"></result> </resultMap> <update id="bookUpdate" parameterType="book"> UPDATE t_book <!-- <trim prefix="set" suffixOverrides=",">--> <!-- <if test="bookName!=null and bookName!=''">bookName=#{bookName},</if>--> <!-- <if test="price!=null and price!=''">price=#{price},</if>--> <!-- <if test="publisher!=null and publisher!=''">publisher=#{publisher}</if>--> <!-- WHERE id=#{id}--> <!-- </trim>--> <set> <if test="bookName!=null and bookName!=''">bookName=#{bookName},</if> <if test="price!=null and price!=''">price=#{price},</if> <if test="publisher!=null and publisher!=''">publisher=#{publisher},</if> </set> WHERE id=#{id} </update> </mapper>
<foreach>:
foreach元素通常在構建in條件語句時使用,其使用方式如下。
(1). item: 表示每個元素迭代時的別名。
(2). index: 指定一個名稱,用于表示在迭代過程中每次迭代的位置
(3). open: 表示該語句以什么開始(in語句以“( ”開始)
(4). separator: 表示每次進行迭代時以上面符號作為分隔符(in語句以“ ,”作為分隔符)
(5). close: 表示該語句以什么結束(in語句以“ )”結束)
(注意: 這個open,separator,close基本上就是固定格式了)
(6). collection: 表示最關鍵且最容易出錯的屬性,需格外注意。該屬性必須指定,不同情況下該屬性的值是不一樣的,主要有三種情況:
1). 若入參為單參數且參數類型是一個list時,collection屬性值為list
2). 若入參為單參數且參數類型是一個數組時,collection屬性值為array
3). 若入參為多參數,需要將其封裝為一個Map進行處理
格式:
<select id="selectPostIn" resultType="domain.blog.Post"> SELECT * FROM POST P <where> <foreach item="item" index="id" collection="list" open="ID in (" separator="," close=")" nullable="true"> #{item} </foreach> </where> </select>
<bind>:
bind元素通常用于需要模糊查詢的語句中,使用bind元素定義了一個name為pattern_username的變量,value的屬性值就是拼接的查詢字符串,其中_parameter.getTitle()表示傳遞進來的參數(也可以直接寫成對應的參數變量名,如username)。
<select id="selectBlogsLike" resultType="Blog"> <bind name="pattern_username" value="'%' + _parameter.getTitle() + '%'" /> SELECT * FROM BLOG WHERE title LIKE #{pattern} </select>
到此這篇關于MyBatis 超詳細講解動態(tài)SQL的實現的文章就介紹到這了,更多相關MyBatis 動態(tài)SQL內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
java多線程join()方法的作用和實現原理解析(應用場景)
join方法主要是用于將當前線程掛起,等待其他線程結束后在執(zhí)行當前線程,本文通過應用場景分析代碼示例講解java多線程join()方法的作用和實現原理,感興趣的朋友一起看看吧2021-07-07