亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

MyBatis 框架動態(tài) SQL

動態(tài) SQL,通過 MyBatis 提供的各種標(biāo)簽對條件作出判斷以實現(xiàn)動態(tài)拼接 SQL 語句。這里的條件判斷使用的表達(dá)式為 OGNL 表達(dá)式。常用的動態(tài) SQL 標(biāo)簽有<if>、<where>、<choose/>、<foreach>等。

MyBatis 的動態(tài) SQL 語句,與 JSTL 中的語句非常相似。

動態(tài) SQL,主要用于解決查詢條件不確定的情況:在程序運行期間,根據(jù) 用戶提交的查詢條件進(jìn)行查詢。提交的查詢條件不同,執(zhí)行的 SQL 語句不同。 若將每種可能的情況均逐一列出,對所有條件進(jìn)行排列組合,將會出現(xiàn)大量的 SQL 語句。此時,可使用動態(tài) SQL 來解決這樣的問題。

環(huán)境準(zhǔn)備

1、創(chuàng)建新的 maven 項目,加入 mybatis , mysql 驅(qū)動依賴

2、創(chuàng)建實體類 Student , StudentDao 接口,StudentDao.xml ,mybatis.xml , 測試類

3、使用之前的表 student。

在 mapper 的動態(tài) SQL 中若出現(xiàn)大于號(>)、小于號(=),小于等于號(<=)等符號,最好將其轉(zhuǎn)換為實體符號。否則, XML 可能會出現(xiàn)解析出錯問題。

特別是對于小于號(<),在 XML 中是絕不能出現(xiàn)的。否則解析 mapper 文件會出錯。

實體符號表:

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

動態(tài) SQL 之 if

對于該標(biāo)簽的執(zhí)行,當(dāng) test 的值為 true 時,會將其包含的 SQL 片斷拼接 到其所在的 SQL 語句中。

語法: <if test="條件">  sql 語句的部分</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>

測試方法:

@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));
}

動態(tài) SQL 之 where

<if/>標(biāo)簽的中存在一個比較麻煩的地方:需要在 where 后手工添加 1=1 的子句。因為,若 where 后的所有<if/>條件均為 false,而 where 后若又沒 有 1=1 子句,則 SQL 中就會只剩下一個空的 where,SQL 出錯。

所以,在 where 后,需要添加永為真子句 1=1,以防止這種情況的發(fā)生。但當(dāng)數(shù)據(jù)量很大時,會嚴(yán)重影響查詢效率。

使用<where/>標(biāo)簽,在有查詢條件時,可以自動添加上 where 子句;沒 有查詢條件時,不會添加 where 子句。需要注意的是,第一個<if/>標(biāo)簽中的 SQL 片斷,可以不包含 and。

不過,寫上 and 也不錯,系統(tǒng)會將多出的 and 去掉。但其它<if/>中 SQL 片斷的 and,必須要求寫上。否則 SQL 語句將拼接出錯。

語法:<where> 其他動態(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>

測試方法:

@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));
}

動態(tài) SQL 之 foreach

<foreach/>標(biāo)簽用于實現(xiàn)對于數(shù)組與集合的遍歷。對其使用,需要注意:

  • collection 表示要遍歷的集合類型, list ,array 等。
  • open、close、separator 為對遍歷內(nèi)容的 SQL 拼接。

語法:

<foreach collection="集合類型" open="開始的字符" close="結(jié)束的字符" 
 item="集合中的成員" separator="集合成員之間的分隔符">
 #{item 的值}
</foreach>

1、遍歷List<簡單類型>

表達(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>

測試方法:

@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<對象類型>

接口方法:

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>

測試方法:

@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));
}

動態(tài) SQL 之代碼片段

<sql/>標(biāo)簽用于定義 SQL 片斷,以便其它 SQL 標(biāo)簽復(fù)用。而其它標(biāo)簽使 用該 SQL 片斷,需要使用<include/>子標(biāo)簽。該<sql/>標(biāo)簽可以定義 SQL 語句中的任何部分,所以<include/>子標(biāo)簽可以放在動態(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>

測試方法:

@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ì)講解動態(tài) SQL的使用的文章就介紹到這了,更多相關(guān)MyBatis 動態(tài) SQL內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論