MyBatis?超詳細(xì)講解動態(tài)SQL的實(shí)現(xiàn)
情景:
我們在使用JDBC或者其他類似的框架開發(fā)進(jìn)行數(shù)據(jù)庫開發(fā)時,通常要根據(jù)需求動手組裝SQL,不用想都覺得是件很痛苦的事情了,而Mybatis框架提供的對SQL語句動態(tài)組裝的功能,能很好地解決這個麻煩。
概述:
動態(tài)SQL是MyBatis框架的一個強(qiáng)大特性,MyBatis3可采用功能強(qiáng)大的基于OGNL的表達(dá)式來完成動態(tài)SQL,它刪除了之前版本中需要了解的大多數(shù)元素,只使用不到原來一半的元素就能完成所需的工作。
SQL元素:
SQL元素 | 說明 |
---|---|
<if> | 判斷語句,用于單條件分支判斷 |
<choose> (<when>,<otherwise>) | 相當(dāng)于Java中的switch...case...default語句,用于多條件分支判斷 |
<where> | 簡化SQL語句中的where的條件判斷 |
<trim> | 可以靈活地去除多余的關(guān)鍵字 |
<set> | 解決動態(tài)更新語句 |
<foreach> | 循環(huán)語句,常用于in語句等列舉條件中 |
<bind> | 從OGNL表達(dá)式中創(chuàng)建一個變量,并將其綁定到上下文,常用于模糊查詢的SQL中 |
<if>:
使用動態(tài) SQL 最常見情景是根據(jù)條件包含 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 元素,它有點(diǎn)像 Java 中的 switch 語句。
因?yàn)樽约簩懙氖菃尾樵?,引用官網(wǎng)代碼。
<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>標(biāo)簽相當(dāng)于 where 1=1 ,主要用來簡化SQL語句中的where條件判斷,并能智能地處理and和or不必?fù)?dān)心多余關(guān)鍵字導(dǎo)致的語法錯誤。代碼參考<if>標(biāo)簽代碼。
<trim>:
這個標(biāo)簽我覺得是最靈活的,trim元素也會自動識別其標(biāo)簽內(nèi)是否有返回值,若有的話,則會在已包含的內(nèi)容前加上某些前綴或者后綴(先判斷是否有語句包含),用到的屬性是
1、prefix(前綴),suffix(后綴)
還可以把包含內(nèi)容的首內(nèi)容或尾內(nèi)容的符號去掉,用到的屬性是
2、prefixOverride(前綴覆蓋),suffixOverride(后綴覆蓋)
可以說功能很強(qiáng)大,可以利用它來替代where元素,并實(shí)現(xiàn)與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,若包含的語句以逗號結(jié)束,則會自動把括號忽略掉,再配合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元素通常在構(gòu)建in條件語句時使用,其使用方式如下。
(1). item: 表示每個元素迭代時的別名。
(2). index: 指定一個名稱,用于表示在迭代過程中每次迭代的位置
(3). open: 表示該語句以什么開始(in語句以“( ”開始)
(4). separator: 表示每次進(jìn)行迭代時以上面符號作為分隔符(in語句以“ ,”作為分隔符)
(5). close: 表示該語句以什么結(jié)束(in語句以“ )”結(jié)束)
(注意: 這個open,separator,close基本上就是固定格式了)
(6). collection: 表示最關(guān)鍵且最容易出錯的屬性,需格外注意。該屬性必須指定,不同情況下該屬性的值是不一樣的,主要有三種情況:
1). 若入?yún)閱螀?shù)且參數(shù)類型是一個list時,collection屬性值為list
2). 若入?yún)閱螀?shù)且參數(shù)類型是一個數(shù)組時,collection屬性值為array
3). 若入?yún)槎鄥?shù),需要將其封裝為一個Map進(jìn)行處理
格式:
<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()表示傳遞進(jìn)來的參數(shù)(也可以直接寫成對應(yīng)的參數(shù)變量名,如username)。
<select id="selectBlogsLike" resultType="Blog"> <bind name="pattern_username" value="'%' + _parameter.getTitle() + '%'" /> SELECT * FROM BLOG WHERE title LIKE #{pattern} </select>
到此這篇關(guān)于MyBatis 超詳細(xì)講解動態(tài)SQL的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)MyBatis 動態(tài)SQL內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Mybatis在注解上如何實(shí)現(xiàn)動態(tài)SQL
- Mybatis超級強(qiáng)大的動態(tài)SQL語句大全
- MyBatis深入解讀動態(tài)SQL的實(shí)現(xiàn)
- 關(guān)于mybatis遇到Integer類型的參數(shù)時動態(tài)sql需要注意條件
- MyBatis連接池的深入和動態(tài)SQL詳解
- mybatis的動態(tài)SQL以及連接池詳解
- MyBatis動態(tài)SQL如何實(shí)現(xiàn)前端指定返回字段
- 一篇文章帶你了解mybatis的動態(tài)SQL
- Mybatis詳解動態(tài)SQL以及單表多表查詢的應(yīng)用
相關(guān)文章
java多線程join()方法的作用和實(shí)現(xiàn)原理解析(應(yīng)用場景)
join方法主要是用于將當(dāng)前線程掛起,等待其他線程結(jié)束后在執(zhí)行當(dāng)前線程,本文通過應(yīng)用場景分析代碼示例講解java多線程join()方法的作用和實(shí)現(xiàn)原理,感興趣的朋友一起看看吧2021-07-07SpringMVC?bean實(shí)現(xiàn)加載控制方法詳解
SpringMVC是一種基于Java,實(shí)現(xiàn)了Web?MVC設(shè)計(jì)模式,請求驅(qū)動類型的輕量級Web框架,即使用了MVC架構(gòu)模式的思想,將Web層進(jìn)行職責(zé)解耦?;谡埱篁?qū)動指的就是使用請求-響應(yīng)模型,框架的目的就是幫助我們簡化開發(fā),SpringMVC也是要簡化我們?nèi)粘eb開發(fā)2022-08-08Spring-boot結(jié)合Shrio實(shí)現(xiàn)JWT的方法
這篇文章主要介紹了Spring-boot結(jié)合Shrio實(shí)現(xiàn)JWT的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05