MyBatis?詳細(xì)講解動(dòng)態(tài)?SQL的使用
MyBatis 框架動(dòng)態(tài) SQL
動(dòng)態(tài) SQL,通過 MyBatis 提供的各種標(biāo)簽對(duì)條件作出判斷以實(shí)現(xiàn)動(dòng)態(tài)拼接 SQL 語(yǔ)句。這里的條件判斷使用的表達(dá)式為 OGNL 表達(dá)式。常用的動(dòng)態(tài) SQL 標(biāo)簽有<if>、<where>、<choose/>、<foreach>等。
MyBatis 的動(dòng)態(tài) SQL 語(yǔ)句,與 JSTL 中的語(yǔ)句非常相似。
動(dòng)態(tài) SQL,主要用于解決查詢條件不確定的情況:在程序運(yùn)行期間,根據(jù) 用戶提交的查詢條件進(jìn)行查詢。提交的查詢條件不同,執(zhí)行的 SQL 語(yǔ)句不同。 若將每種可能的情況均逐一列出,對(duì)所有條件進(jìn)行排列組合,將會(huì)出現(xiàn)大量的 SQL 語(yǔ)句。此時(shí),可使用動(dòng)態(tài) SQL 來(lái)解決這樣的問題。
環(huán)境準(zhǔn)備
1、創(chuàng)建新的 maven 項(xiàng)目,加入 mybatis , mysql 驅(qū)動(dòng)依賴
2、創(chuàng)建實(shí)體類 Student , StudentDao 接口,StudentDao.xml ,mybatis.xml , 測(cè)試類
3、使用之前的表 student。
在 mapper 的動(dòng)態(tài) SQL 中若出現(xiàn)大于號(hào)(>)、小于號(hào)(=),小于等于號(hào)(<=)等符號(hào),最好將其轉(zhuǎn)換為實(shí)體符號(hào)。否則, XML 可能會(huì)出現(xiàn)解析出錯(cuò)問題。
特別是對(duì)于小于號(hào)(<),在 XML 中是絕不能出現(xiàn)的。否則解析 mapper 文件會(huì)出錯(cuò)。
實(shí)體符號(hào)表:
| < | 小于 | < |
| > | 大于 | > |
| >= | 大于等于 | >= |
| <= | 小于等于 | <= |
動(dòng)態(tài) SQL 之 if
對(duì)于該標(biāo)簽的執(zhí)行,當(dāng) test 的值為 true 時(shí),會(huì)將其包含的 SQL 片斷拼接 到其所在的 SQL 語(yǔ)句中。
語(yǔ)法: <if test="條件"> sql 語(yǔ)句的部分</if>
接口方法:
List<Student> selectStudentIf(Student student);
mapper文件:
<select id="selectStudentIf" resultType="com.bjpowernode.domain.Student">
select id,name,email,age from student
where 1=1
<if test="name != null and name !='' ">
and name = #{name}
</if>
<if test="age > 0 ">
and age > #{age}
</if>
</select>測(cè)試方法:
@Test
public void testSelect() throws IOException {
Student param = new Student();
param.setName("李力");
param.setAge(18);
List<Student> studentList = studentDao.selectStudentIf(param);
studentList.forEach( stu -> System.out.println(stu));
}動(dòng)態(tài) SQL 之 where
<if/>標(biāo)簽的中存在一個(gè)比較麻煩的地方:需要在 where 后手工添加 1=1 的子句。因?yàn)椋?where 后的所有<if/>條件均為 false,而 where 后若又沒 有 1=1 子句,則 SQL 中就會(huì)只剩下一個(gè)空的 where,SQL 出錯(cuò)。
所以,在 where 后,需要添加永為真子句 1=1,以防止這種情況的發(fā)生。但當(dāng)數(shù)據(jù)量很大時(shí),會(huì)嚴(yán)重影響查詢效率。
使用<where/>標(biāo)簽,在有查詢條件時(shí),可以自動(dòng)添加上 where 子句;沒 有查詢條件時(shí),不會(huì)添加 where 子句。需要注意的是,第一個(gè)<if/>標(biāo)簽中的 SQL 片斷,可以不包含 and。
不過,寫上 and 也不錯(cuò),系統(tǒng)會(huì)將多出的 and 去掉。但其它<if/>中 SQL 片斷的 and,必須要求寫上。否則 SQL 語(yǔ)句將拼接出錯(cuò)。
語(yǔ)法:<where> 其他動(dòng)態(tài) sql</where>
接口方法:
List<Student> selectStudentWhere(Student student);
mapper文件:
<select id="selectStudentWhere"
resultType="com.bjpowernode.domain.Student">
select id,name,email,age from student
<where>
<if test="name != null and name !='' ">
and name = #{name}
</if>
<if test="age > 0 ">
and age > #{age}
</if>
</where>
</select>測(cè)試方法:
@Test
public void testSelectWhere() throws IOException {
Student param = new Student();
param.setName("李力");
param.setAge(18);
List<Student> studentList = studentDao.selectStudentWhere(param);
studentList.forEach( stu -> System.out.println(stu));
}動(dòng)態(tài) SQL 之 foreach
<foreach/>標(biāo)簽用于實(shí)現(xiàn)對(duì)于數(shù)組與集合的遍歷。對(duì)其使用,需要注意:
- collection 表示要遍歷的集合類型, list ,array 等。
- open、close、separator 為對(duì)遍歷內(nèi)容的 SQL 拼接。
語(yǔ)法:
<foreach collection="集合類型" open="開始的字符" close="結(jié)束的字符"
item="集合中的成員" separator="集合成員之間的分隔符">
#{item 的值}
</foreach>1、遍歷List<簡(jiǎn)單類型>
表達(dá)式中的 List 使用 list 表示,其大小使用 list.size 表示。
需求:查詢學(xué)生 id 是 1002,1005,1006
接口方法:
List<Student> selectStudentForList(List<Integer> idList);
mapper文件:
<select id="selectStudentForList"
resultType="com.bjpowernode.domain.Student">
select id,name,email,age from student
<if test="list !=null and list.size > 0 ">
where id in
<foreach collection="list" open="(" close=")"
item="stuid" separator=",">
#{stuid}
</foreach>
</if>
</select>測(cè)試方法:
@Test
public void testSelectForList() {
List<Integer> list = new ArrayList<>();
list.add(1002);
list.add(1005);
list.add(1006);
List<Student> studentList = studentDao.selectStudentForList(list);
studentList.forEach( stu -> System.out.println(stu));
}2、遍歷List<對(duì)象類型>
接口方法:
List<Student> selectStudentForList2(List<Student> stuList);
mapper文件:
<select id="selectStudentForList2"
resultType="com.bjpowernode.domain.Student">
select id,name,email,age from student
<if test="list !=null and list.size > 0 ">
where id in
<foreach collection="list" open="(" close=")"
item="stuobject" separator=",">
#{stuobject.id}
</foreach>
</if>
</select>測(cè)試方法:
@Test
public void testSelectForList2() {
List<Student> list = new ArrayList<>();
Student s1 = new Student();
s1.setId(1002);
list.add(s1);
s1 = new Student();
s1.setId(1005);
list.add(s1);
List<Student> studentList = studentDao.selectStudentForList2(list);
studentList.forEach( stu -> System.out.println(stu));
}動(dòng)態(tài) SQL 之代碼片段
<sql/>標(biāo)簽用于定義 SQL 片斷,以便其它 SQL 標(biāo)簽復(fù)用。而其它標(biāo)簽使 用該 SQL 片斷,需要使用<include/>子標(biāo)簽。該<sql/>標(biāo)簽可以定義 SQL 語(yǔ)句中的任何部分,所以<include/>子標(biāo)簽可以放在動(dòng)態(tài) SQL 的任何位置。
接口方法:
List<Student> selectStudentSqlFragment(List<Student> stuList);
mapper文件:
<!--創(chuàng)建 sql 片段 id:片段的自定義名稱-->
<sql id="studentSql">
select id,name,email,age from student
</sql>
<select id="selectStudentSqlFragment"
resultType="com.bjpowernode.domain.Student">
<!-- 引用 sql 片段 -->
<include refid="studentSql"/>
<if test="list !=null and list.size > 0 ">
where id in
<foreach collection="list" open="(" close=")"
item="stuobject" separator=",">
#{stuobject.id}
</foreach>
</if>
</select>測(cè)試方法:
@Test
public void testSelectSqlFragment() {
List<Student> list = new ArrayList<>();
Student s1 = new Student();
s1.setId(1002);
list.add(s1);
s1 = new Student();
s1.setId(1005);
list.add(s1);
List<Student> studentList = studentDao.selectStudentSqlFragment(list);
studentList.forEach( stu -> System.out.println(stu));
}到此這篇關(guān)于MyBatis 詳細(xì)講解動(dòng)態(tài) SQL的使用的文章就介紹到這了,更多相關(guān)MyBatis 動(dòng)態(tài) SQL內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
mybatis對(duì)于list更新sql語(yǔ)句的寫法說(shuō)明
這篇文章主要介紹了mybatis對(duì)于list更新sql語(yǔ)句的寫法說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08
spring Retryable注解實(shí)現(xiàn)重試詳解
這篇文章主要介紹了spring Retryable注解實(shí)現(xiàn)重試詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2020-09-09
Java編程實(shí)現(xiàn)深度優(yōu)先遍歷與連通分量代碼示例
這篇文章主要介紹了Java編程實(shí)現(xiàn)深度優(yōu)先遍歷與連通分量代碼示例,2017-11-11
java設(shè)計(jì)模式之策略模式在促銷活動(dòng)場(chǎng)景中的使用案例
這篇文章主要為大家介紹了java設(shè)計(jì)模式之策略模式在促銷活動(dòng)場(chǎng)景中案例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05
SpringBoot Starter自定義之創(chuàng)建可復(fù)用的自動(dòng)配置模塊方式
本文將詳細(xì)介紹如何設(shè)計(jì)和實(shí)現(xiàn)一個(gè)自定義的Spring Boot Starter,幫助讀者掌握這一強(qiáng)大技術(shù),提升代碼復(fù)用性和開發(fā)效率,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-04-04

