MyBatis深入解讀動態(tài)SQL的實現(xiàn)
mybatis最強大的功能之一便是它的動態(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>使用這種標簽 , 動態(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的強大之處
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>當<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 語句中進行迭代一個集合。foreach元素的屬性主要有 item,index,collection,open,separator,close。 item 表示集合中每一個元素進行迭代時的別名,index 指定一個名字,用于表示在迭代過程中,每次迭代到的位置,open 表示該語句以什么開始, separator 表示在每次進行迭代之間以什么符號作為分隔符,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)文章
使用Java實現(xiàn)通用樹形結(jié)構(gòu)構(gòu)建工具類
這篇文章主要為大家詳細介紹了如何使用Java實現(xiàn)通用樹形結(jié)構(gòu)構(gòu)建工具類,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-03-03
詳解JAVA抓取網(wǎng)頁的圖片,JAVA利用正則表達式抓取網(wǎng)站圖片
這篇文章主要介紹了詳解JAVA抓取網(wǎng)頁的圖片,JAVA利用正則表達式抓取網(wǎng)站圖片,非常具有實用價值,需要的朋友可以參考下。2016-12-12
SpringBoot中使用JeecgBoot的Autopoi導(dǎo)出Excel的方法步驟
這篇文章主要介紹了SpringBoot中使用JeecgBoot的Autopoi導(dǎo)出Excel的方法步驟,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
Java構(gòu)建JDBC應(yīng)用程序的實例操作
在本篇文章里小編給大家整理了一篇關(guān)于Java構(gòu)建JDBC應(yīng)用程序的實例操作,有興趣的朋友們可以學(xué)習(xí)參考下。2021-03-03
Spring?Boot?@Autowired?@Resource屬性賦值時機探究
這篇文章主要為大家介紹了Spring?Boot?@Autowired?@Resource屬性賦值時機,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-07-07

