MyBatis深入解讀動態(tài)SQL的實現(xiàn)
mybatis最強(qiáng)大的功能之一便是它的動態(tài)sql能力
借用官方文檔的一段話 : 如果您以前有使用JDBC或者類似框架的 經(jīng)歷,您就會明白把SQL語句條件連接在一起是多么的痛苦,要確保不能忘記空格或者不要在 columns列后面省略一個逗號等。動態(tài)語句能夠完全解決掉這些痛苦。
那么如果沒有這種功能到底有多痛苦呢 ? 我們來舉例說明
這是一張表 , 試想如果我們通過 name 和 age來查詢表信息時 , sql語句中肯定會存在 where和and字句 , 但是如果 name或者age 有一個為null或者都為null , 那么此時的 where 和and就會被孤立,那么這樣肯定會出現(xiàn)很多問題 , 所以mybatis的動態(tài)sql功能幫助我們完美解決
MyBatis 中用于實現(xiàn)動態(tài) SQL 的元素主要有:
If where trim set choose(when, otherwise) foreach
if和where
<select id="selectAllEmployee1" resultMap="employeeMap2" parameterType="Employee"> SELECT e.id,e.name ename,e.age,d.name dname FROM employee e LEFT JOIN dept d ON e.deptId = d.id <where> <if test="name!=null & name!=''"> e.name =#{name} </if> <if test="age!=null & age!=''"> and e.age =#{age} </if> </where> </select>
使用這種標(biāo)簽 , 動態(tài)sql可以根據(jù) 條件來自動幫我們完善sql
SqlSession sqlSession= MybatisUtil.getSqlSession(); EmployeeDao mapper=sqlSession.getMapper(EmployeeDao.class); //創(chuàng)建一個對象,set值 Employee employee = new Employee(); employee.setName(""); employee.setAge(null); List<Employee> employees = mapper.selectAllEmployee1(employee); System.out.println(employees); sqlSession.commit(); sqlSession.close();
第一次我們都設(shè)置null值, 表中數(shù)據(jù)完全被查詢
第二次我們只查詢年齡
employee.setName(""); employee.setAge(20);
查詢到兩條年齡為20的數(shù)據(jù) , 這就是mybatis動態(tài)sql的強(qiáng)大之處
trim
上述的 where 與 if 我們也可以使用 trim 來代替where
<select id="selectAllEmployee1" resultMap="employeeMap2" parameterType="Employee"> SELECT e.id,e.name ename,e.age,d.name dname FROM employee e LEFT JOIN dept d ON e.deptId = d.id <trim prefix="where" prefixOverrides="or|and"> <if test="name!=null & name!=''"> e.name =#{name} </if> <if test="age!=null & age!=''"> and e.age =#{age} </if> </trim> </select>
這里有兩個屬性 prefix , prefixOverrides
prefix : 代表前綴 , 如果if 中有成立的條件, 就會在sql前面拼入where字句
prefixOverrides : 根據(jù)if 條件自動判斷是否去除 or | and字句
相應(yīng)的也有suffix與suffixOverrides , 代表對尾部的判斷
Choose
choose代表多路選擇(多選一)
<select id="selectAllEmployee1" resultMap="employeeMap2" parameterType="Employee"> SELECT e.id,e.name ename,e.age,d.name dname FROM employee e LEFT JOIN dept d ON e.deptId = d.id <trim prefix="where" prefixOverrides="or|and"> <choose> <when test="name!=null & name!=''"> and e.name =#{name} </when> <when test="age!=null"> and e.age =#{age} </when> <otherwise> and e.name ='李雷' </otherwise> </choose> </trim> </select>
當(dāng)<when>中的條件成立時, 走when中的語句,都不成立走<otherwise>
Set
set 可以根據(jù)條件自動添加set字句,動態(tài)更新列,也可以來剔除追加到條件末尾的任何不相關(guān)的逗號
<update id="updateEmployee"> update employee <set> <if test="name!=null & name!=''"> name=#{name}, </if> <if test="age!=null"> age=#{age}, </if> </set> where id=#{id} </update>
foreach
<foreach> 主要用在構(gòu)建 in 條件中,它可以在 SQL 語句中進(jìn)行迭代一個集合。foreach元素的屬性主要有 item,index,collection,open,separator,close。 item 表示集合中每一個元素進(jìn)行迭代時的別名,index 指定一個名字,用于表示在迭代過程中,每次迭代到的位置,open 表示該語句以什么開始, separator 表示在每次進(jìn)行迭代之間以什么符號作為分隔符,close 表示以什么結(jié)束,在使用 foreach 的時候最關(guān)鍵的也是最容易出錯的就是 collection 屬性,該屬性是必須指定的,但是在不同情況下,該屬性的值是不一樣的。
– 如果傳入的是單參數(shù)且參數(shù)類型是一個 List 的時候,collection 屬 性值為 list
– 如果傳入的是單參數(shù)且參數(shù)類型是一個 array 數(shù)組的時候, collection 的屬性值為array
//創(chuàng)建一個list集合 List<Integer> list = new ArrayList<>(); list.add(19); list.add(20); List<Employee> employees = mapper.selectAllEmployee2(list);
接口方法如下 :
List<Employee> selectAllEmployee2(List<Integer> list);
對應(yīng)動態(tài)sql如下 :
<select id="selectAllEmployee2" resultMap="employeeMap2" parameterType="Employee"> SELECT e.id,e.name ename,e.age,d.name dname FROM employee e LEFT JOIN dept d ON e.deptId = d.id where age in <foreach collection="list" item="age" open="(" separator="," close=")"> #{age} </foreach> </select>
這里我們傳入的是一個集合, 所以參數(shù)選擇 list , 通過foreach我們可以動態(tài)的根據(jù)集合里的值來查詢
關(guān)于動態(tài)sql就介紹到這,感謝閱讀 !
到此這篇關(guān)于MyBatis深入解讀動態(tài)SQL的實現(xiàn)的文章就介紹到這了,更多相關(guān)MyBatis動態(tài)SQL內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
hadoop中實現(xiàn)java網(wǎng)絡(luò)爬蟲(示例講解)
下面小編就為大家?guī)硪黄猦adoop中實現(xiàn)java網(wǎng)絡(luò)爬蟲(示例講解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-09-09使用HandlerMethodArgumentResolver用于統(tǒng)一獲取當(dāng)前登錄用戶
這篇文章主要介紹了使用HandlerMethodArgumentResolver用于統(tǒng)一獲取當(dāng)前登錄用戶實例,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12java 啟動exe程序,傳遞參數(shù)和獲取參數(shù)操作
這篇文章主要介紹了java 啟動exe程序,傳遞參數(shù)和獲取參數(shù)操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01Spring Boot 2.7.6整合redis與低版本的區(qū)別
這篇文章主要介紹了Spring Boot 2.7.6整合redis與低版本的區(qū)別,文中補(bǔ)充介紹了SpringBoot各個版本使用Redis之間的區(qū)別實例講解,需要的朋友可以參考下2023-02-02