MyBatis動(dòng)態(tài)SQL特性詳解
概述
動(dòng)態(tài)SQL:SQL語(yǔ)句會(huì)隨著用戶(hù)輸入或外部條件的變化而變化 。
例如:我們?cè)谧龆鄺l件查詢(xún)的時(shí)候,編寫(xiě)SQL語(yǔ)句的查詢(xún)操作,我們并不知道用戶(hù)實(shí)際操作時(shí)會(huì)選擇多少條件進(jìn)行查詢(xún),假如有三個(gè)條件(日期,大小,名字)供用戶(hù)選擇后查詢(xún),如果用戶(hù)只選擇了一個(gè)條件(大?。┻M(jìn)行查詢(xún),那么我們應(yīng)該動(dòng)態(tài)的進(jìn)行SQL語(yǔ)句的編寫(xiě)。
再例如:當(dāng)做信息修改時(shí),可修改信息有 用戶(hù)名,密碼,性別,愛(ài)好。用戶(hù)如果全部修改,這當(dāng)然沒(méi)什么好說(shuō)的。但是如果用戶(hù)只修改某些信息,并沒(méi)有全部修改。如果我們還用修改全部的那一套SQL語(yǔ)句,那么沒(méi)有修改到的信息在SQL語(yǔ)句中對(duì)應(yīng)值就會(huì)為null。這明顯有問(wèn)題,所以我們需要使用到動(dòng)態(tài)SQL。
再再例如:我們做批量刪除,我們?cè)诰帉?xiě)SQL語(yǔ)句時(shí),并不知道用戶(hù)實(shí)際操作時(shí)選擇多少條數(shù)據(jù),所以我們使用SQL語(yǔ)句就可以解決這樣的問(wèn)題。
動(dòng)態(tài)多條件查詢(xún)
環(huán)境準(zhǔn)備:mybatis環(huán)境正常,完善數(shù)據(jù)表和pojo類(lèi)。
我們?cè)谧鰟?dòng)態(tài)多條件時(shí),SQL語(yǔ)句中需要判斷用戶(hù)輸入了哪些條件,沒(méi)有輸入哪些條件,但是我們不能使用Java的判斷語(yǔ)法,MyBatis對(duì)動(dòng)態(tài)SQL有很強(qiáng)大的支撐,給我們提供了一系列的標(biāo)簽供我們使用。
例如 <if> 、<choose>、<set>、<where>、<foreach>等,如何使用呢?看案例。
現(xiàn)有一張數(shù)據(jù)表,表中有三個(gè)字段:用戶(hù)名,密碼,賬戶(hù)余額。
案例需求:用戶(hù)通過(guò)用戶(hù)名,密碼這兩條件進(jìn)行模糊查詢(xún),也可以只用單個(gè)條件進(jìn)行查詢(xún),在此演示只通過(guò)用戶(hù)名中包含"A"為條件進(jìn)行查詢(xún)用戶(hù)信息。
數(shù)據(jù)層接口方法
List<User> selectByCondition(User user);
SQL映射文件-SQL語(yǔ)句
<select id="selectByCondition" resultType="User">
select *
from user_table
<!--需要使用where標(biāo)簽,否則兩個(gè)條件為空時(shí),會(huì)存在sql語(yǔ)法錯(cuò)誤 -->
<where>
<if test="user != null and user != ''">
user like #{user}
</if>
<if test="password != null and password != null">
and password like #{password}
</if>
</where>
</select>測(cè)試方法
/**
* 動(dòng)態(tài)條件查詢(xún):
* 用戶(hù)輸入的可能不是全部參數(shù),而是部分參數(shù)
* 動(dòng)態(tài)SQL
*/
@Test
public void ConditionSelectTest() throws IOException {
//模擬前端傳入?yún)?shù)
String userName = "A";
//String password = "4";
//處理參數(shù)-配置為模糊查詢(xún)形式
userName = "%"+ userName + "%";
//password = "%"+ password + "%";
//封裝成對(duì)象的形式傳入
User user = new User();
user.setUser(userName);
//user.setPassword(password);
String resource = "mybatis-config.xml";
InputStream is = Resources.getResourceAsStream(resource);
SqlSessionFactory build = new SqlSessionFactoryBuilder().build(is);
SqlSession sqlSession = build.openSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
//調(diào)用方法
List<User> users = mapper.selectByCondition(user);
users.forEach(System.out::println);
}原始數(shù)據(jù):

執(zhí)行方法:查詢(xún)user中存在"A"的記錄信息。
User{user= 'AA ', password= '123456', balance=5500}
User{user='AB', password='123456', balance=3000}
User{user='AD', password=' 132456', balance=2000}
Process finished with exit code 0
動(dòng)態(tài)修改
案例需求:通過(guò)用戶(hù)名進(jìn)行修改密碼或賬戶(hù)余額??梢灾皇切薷囊粋€(gè),也可以修改全部。
說(shuō)明:
當(dāng)前案例,只是對(duì)于數(shù)據(jù)層的測(cè)試,不做業(yè)務(wù)層和表現(xiàn)層的功能實(shí)現(xiàn),所以我們都是通過(guò)模擬前端傳入數(shù)據(jù)來(lái)進(jìn)行測(cè)試。
數(shù)據(jù)層接口方法
//動(dòng)態(tài)修改void alterRecordByName(User user);
SQL映射文件-SQL語(yǔ)句
<update id="alterRecordByName">
update user_table
<!--set標(biāo)簽 代表的就是set-->
<set>
<!--if標(biāo)簽 判斷用戶(hù)是否輸入,不輸入就不進(jìn)行修改-->
<if test="password != null and password != ''">
password = #{password},
</if>
<if test="balance != null ">
balance = #{balance}
</if>
</set>
where user = #{user};
</update>if標(biāo)簽中的test屬性,用于填寫(xiě)條件判斷。
測(cè)試方法
/**
* 修改部分-動(dòng)態(tài)SQL
*/
@Test
public void testAlterCondition() throws IOException {
//模擬前端傳入?yún)?shù)
String userName = "BB";
//密碼和賬戶(hù)余額修改任意,當(dāng)前只修改賬戶(hù)余額
//String password = "123456";
int balance = 5000;
//封裝成對(duì)象的形式傳入,只將賬戶(hù)余額進(jìn)行對(duì)象封裝,密碼不變
User user = new User();
user.setUser(userName);
// user.setPassword(password);
user.setBalance(balance);
String resource = "mybatis-config.xml";
InputStream is = Resources.getResourceAsStream(resource);
SqlSessionFactory build = new SqlSessionFactoryBuilder().build(is);
//獲取SqlSession對(duì)象的時(shí)候,openSession空參時(shí):autoCommit = false
SqlSession sqlSession = build.openSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
mapper.alterRecordByName(user);
System.out.println("修改成功...");
//需要手動(dòng)提交,或者設(shè)置:openSession(true)
sqlSession.commit();
}原始數(shù)據(jù):

執(zhí)行方法后數(shù)據(jù):成功。

動(dòng)態(tài)批量刪除
案例需求:根據(jù)傳進(jìn)來(lái)的賬戶(hù)余額鎖定用戶(hù),都進(jìn)行刪除。
數(shù)據(jù)層接口方法
//參數(shù)為數(shù)組,可接收多個(gè)值
void deleteByBalances(@Param("balance") int[] balance);SQL映射文件-SQL語(yǔ)句
<!--批量刪除-->
<delete id="deleteByBalances">
delete from user_table
where balance in
<foreach collection="balance" separator="," item="balance" open="(" close=")" >
#{balance}
</foreach>;
</delete>foreach標(biāo)簽屬性說(shuō)明:
//collection:代表遍歷對(duì)象
//item:代表每一次獲取的對(duì)象
//separator:分隔符,數(shù)組中元素就是,為分隔符
//open,close:代表開(kāi)始和結(jié)束位置需要填寫(xiě)的括號(hào)()
測(cè)試方法
刪除賬戶(hù)余額為2000,3000的。
/**
* 批量刪除
*/
@Test
public void testDeletes() throws IOException {
//模擬前端傳入?yún)?shù)
int[] balances = new int[]{2000,3000};
String resource = "mybatis-config.xml";
InputStream is = Resources.getResourceAsStream(resource);
SqlSessionFactory build = new SqlSessionFactoryBuilder().build(is);
//獲取SqlSession對(duì)象的時(shí)候,openSession空參時(shí):autoCommit = false(默認(rèn))
SqlSession sqlSession = build.openSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
mapper.deleteByBalances(balances);
System.out.println("刪除成功---->");
//需要手動(dòng)提交,或者設(shè)置:openSession(true)
sqlSession.commit();
}原始數(shù)據(jù):

執(zhí)行方法后數(shù)據(jù):

到此這篇關(guān)于MyBatis動(dòng)態(tài)SQL特性詳解的文章就介紹到這了,更多相關(guān)MyBatis動(dòng)態(tài)SQL內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Mybatis多表查詢(xún)與動(dòng)態(tài)SQL特性詳解
- MyBatis動(dòng)態(tài)sql查詢(xún)及多參數(shù)查詢(xún)方式
- Mybatis的動(dòng)態(tài)Sql組合模式詳情
- Mybatis如何實(shí)現(xiàn)@Select等注解動(dòng)態(tài)組合SQL語(yǔ)句
- MyBatis?@Select注解介紹:基本用法與動(dòng)態(tài)SQL拼寫(xiě)方式
- MyBatis在注解上使用動(dòng)態(tài)SQL方式(@select使用if)
- Mybatis中xml的動(dòng)態(tài)sql實(shí)現(xiàn)示例
- MyBatis動(dòng)態(tài)SQL表達(dá)式詳解
相關(guān)文章
Spring?Boot項(xiàng)目中使用OpenAI-Java的示例詳解
Spring?Boot是由Pivotal團(tuán)隊(duì)提供的全新框架,其設(shè)計(jì)目的是用來(lái)簡(jiǎn)化新Spring應(yīng)用的初始搭建以及開(kāi)發(fā)過(guò)程,這篇文章主要介紹了Spring?Boot項(xiàng)目中使用OpenAI-Java的示例詳解,需要的朋友可以參考下2023-04-04
Spring Security實(shí)現(xiàn)不同接口安全策略方法詳解
這篇文章主要介紹了Spring Security實(shí)現(xiàn)不同接口安全策略方法詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09
java.lang.AbstractMethodError: org.apache.xerces.dom.Documen
這篇文章主要介紹了java.lang.AbstractMethodError: org.apache.xerces.dom.DocumentImpl.setXmlVersion問(wèn)題解決方法,導(dǎo)致本文問(wèn)題的原因是缺少一個(gè)xerces.jar jar包,需要的朋友可以參考下2015-03-03
springboot 異步調(diào)用的實(shí)現(xiàn)方法
這篇文章主要介紹了springboot 異步調(diào)用的實(shí)現(xiàn)方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-04-04

