欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

MyBatis?詳細(xì)講解動(dòng)態(tài)?SQL的使用

 更新時(shí)間:2022年04月07日 12:14:12   作者:托馬斯-酷濤  
動(dòng)態(tài)?SQL?是?MyBatis?的強(qiáng)大特性之一。如果你使用過(guò)?JDBC?或其它類(lèi)似的框架,你應(yīng)該能理解根據(jù)不同條件拼接?SQL?語(yǔ)句有多痛苦,例如拼接時(shí)要確保不能忘記添加必要的空格,還要注意去掉列表最后一個(gè)列名的逗號(hào)。利用動(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)表:

<小于&lt
>大于&gt
>=大于等于&gt;=
<=小于等于&lt;=

動(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 &gt; #{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 &gt; #{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)文章

最新評(píng)論