MyBatis?詳細(xì)講解動(dòng)態(tài)?SQL的使用
MyBatis 框架動(dòng)態(tài) SQL
動(dòng)態(tài) SQL,通過(guò) 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,主要用于解決查詢(xún)條件不確定的情況:在程序運(yùn)行期間,根據(jù) 用戶(hù)提交的查詢(xún)條件進(jìn)行查詢(xún)。提交的查詢(xún)條件不同,執(zhí)行的 SQL 語(yǔ)句不同。 若將每種可能的情況均逐一列出,對(duì)所有條件進(jìn)行排列組合,將會(huì)出現(xiàn)大量的 SQL 語(yǔ)句。此時(shí),可使用動(dòng)態(tài) SQL 來(lái)解決這樣的問(wèn)題。
環(huán)境準(zhǔn)備
1、創(chuàng)建新的 maven 項(xiàng)目,加入 mybatis , mysql 驅(qū)動(dòng)依賴(lài)
2、創(chuàng)建實(shí)體類(lèi) Student , StudentDao 接口,StudentDao.xml ,mybatis.xml , 測(cè)試類(lèi)
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ò)問(wèn)題。
特別是對(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 后若又沒(méi) 有 1=1 子句,則 SQL 中就會(huì)只剩下一個(gè)空的 where,SQL 出錯(cuò)。
所以,在 where 后,需要添加永為真子句 1=1,以防止這種情況的發(fā)生。但當(dāng)數(shù)據(jù)量很大時(shí),會(huì)嚴(yán)重影響查詢(xún)效率。
使用<where/>標(biāo)簽,在有查詢(xún)條件時(shí),可以自動(dòng)添加上 where 子句;沒(méi) 有查詢(xún)條件時(shí),不會(huì)添加 where 子句。需要注意的是,第一個(gè)<if/>標(biāo)簽中的 SQL 片斷,可以不包含 and。
不過(guò),寫(xiě)上 and 也不錯(cuò),系統(tǒng)會(huì)將多出的 and 去掉。但其它<if/>中 SQL 片斷的 and,必須要求寫(xiě)上。否則 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 表示要遍歷的集合類(lèi)型, list ,array 等。
- open、close、separator 為對(duì)遍歷內(nèi)容的 SQL 拼接。
語(yǔ)法:
<foreach collection="集合類(lèi)型" open="開(kāi)始的字符" close="結(jié)束的字符" item="集合中的成員" separator="集合成員之間的分隔符"> #{item 的值} </foreach>
1、遍歷List<簡(jiǎn)單類(lèi)型>
表達(dá)式中的 List 使用 list 表示,其大小使用 list.size 表示。
需求:查詢(xún)學(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ì)象類(lèi)型>
接口方法:
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:片段的自定義名稱(chēng)--> <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)文章
IDEA使用properties配置文件進(jìn)行mysql數(shù)據(jù)庫(kù)連接的教程圖解
Properties類(lèi)是 鍵和值均為字符串的可以永久存儲(chǔ)到文件中的key-value集合。這篇文章主要介紹了IDEA使用properties配置文件進(jìn)行mysql數(shù)據(jù)路連接 ,需要的朋友可以參考下2018-10-10詳解@ConfigurationProperties如何裝載到Spring容器中
這篇文章主要為大家詳細(xì)介紹了@ConfigurationProperties該如何裝載到Spring容器中,文中的示例代碼講解詳細(xì),需要的小伙伴可以參考一下2023-07-07JMeter連接Mysql數(shù)據(jù)庫(kù)的實(shí)現(xiàn)步驟
本文主要介紹了JMeter操作Mysql數(shù)據(jù)庫(kù),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-12-12Java實(shí)現(xiàn)兩人五子棋游戲(五) 判斷是否有一方勝出
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)兩人五子棋游戲,判斷是否有一方勝出,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03Java實(shí)戰(zhàn)員工績(jī)效管理系統(tǒng)的實(shí)現(xiàn)流程
只學(xué)書(shū)上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實(shí)戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+SSM+Mysql+Maven+HTML實(shí)現(xiàn)一個(gè)員工績(jī)效管理系統(tǒng),大家可以在過(guò)程中查缺補(bǔ)漏,提升水平2022-01-01總結(jié)java多線(xiàn)程之互斥與同步解決方案
文中總結(jié)了線(xiàn)程互斥與同步,synchronized使用細(xì)節(jié)及原理,Reentrylock使用細(xì)節(jié)等知識(shí),對(duì)解決Java多線(xiàn)程互斥與同步等問(wèn)題很有效,,需要的朋友可以參考下2021-05-05