一篇文章帶你了解mybatis的動(dòng)態(tài)SQL
1、動(dòng)態(tài)SQL:if 語(yǔ)句
根據(jù) username 和 sex 來(lái)查詢(xún)數(shù)據(jù)。如果username為空,那么將只根據(jù)sex來(lái)查詢(xún);反之只根據(jù)username來(lái)查詢(xún)
首先不使用 動(dòng)態(tài)SQL 來(lái)書(shū)寫(xiě)
<select id="selectUserByUsernameAndSex"
resultType="user" parameterType="com.ys.po.User">
<!-- 這里和普通的sql 查詢(xún)語(yǔ)句差不多,對(duì)于只有一個(gè)參數(shù),后面的 #{id}表示占位符,里面不一定要寫(xiě)id,
寫(xiě)啥都可以,但是不要空著,如果有多個(gè)參數(shù)則必須寫(xiě)pojo類(lèi)里面的屬性 -->
select * from user where username=#{username} and sex=#{sex}
</select>上面的查詢(xún)語(yǔ)句,我們可以發(fā)現(xiàn),如果 #{username} 為空,那么查詢(xún)結(jié)果也是空,如何解決這個(gè)問(wèn)題呢?使用 if 來(lái)判斷
<select id="selectUserByUsernameAndSex" resultType="user" parameterType="com.ys.po.User">
select * from user where
<if test="username != null">
username=#{username}
</if>
<if test="username != null">
and sex=#{sex}
</if>
</select>這樣寫(xiě)我們可以看到,如果 sex 等于 null,那么查詢(xún)語(yǔ)句為 select * from user where username=#{username},但是如果usename 為空呢?那么查詢(xún)語(yǔ)句為 select * from user where and sex=#{sex},這是錯(cuò)誤的 SQL 語(yǔ)句,如何解決呢?請(qǐng)看下面的 where 語(yǔ)句
2、動(dòng)態(tài)SQL:if+where語(yǔ)句
<select id="selectUserByUsernameAndSex" resultType="user" parameterType="com.ys.po.User">
select * from user
<where>
<if test="username != null">
username=#{username}
</if>
<if test="username != null">
and sex=#{sex}
</if>
</where>
</select>這個(gè)“where”標(biāo)簽會(huì)知道如果它包含的標(biāo)簽中有返回值的話,它就插入一個(gè)‘where’。此外,如果標(biāo)簽返回的內(nèi)容是以AND 或OR 開(kāi)頭的,則它會(huì)剔除掉。
3、動(dòng)態(tài)SQL:if+set 語(yǔ)句
同理,上面的對(duì)于查詢(xún) SQL 語(yǔ)句包含 where 關(guān)鍵字,如果在進(jìn)行更新操作的時(shí)候,含有 set 關(guān)鍵詞,我們?cè)趺刺幚砟兀?/p>
<!-- 根據(jù) id 更新 user 表的數(shù)據(jù) -->
<update id="updateUserById" parameterType="com.ys.po.User">
update user u
<set>
<if test="username != null and username != ''">
u.username = #{username},
</if>
<if test="sex != null and sex != ''">
u.sex = #{sex}
</if>
</set>
where id=#{id}
</update>這樣寫(xiě),如果第一個(gè)條件 username 為空,那么 sql 語(yǔ)句為:update user u set u.sex=? where id=?
如果第一個(gè)條件不為空,那么 sql 語(yǔ)句為:update user u set u.username = ? ,u.sex = ? where id=?
4、動(dòng)態(tài)SQL:choose(when,otherwise) 語(yǔ)句
有時(shí)候,我們不想用到所有的查詢(xún)條件,只想選擇其中的一個(gè),查詢(xún)條件有一個(gè)滿(mǎn)足即可,使用 choose 標(biāo)簽可以解決此類(lèi)問(wèn)題,類(lèi)似于 Java 的 switch 語(yǔ)句
<select id="selectUserByChoose" resultType="com.ys.po.User" parameterType="com.ys.po.User">
select * from user
<where>
<choose>
<when test="id !='' and id != null">
id=#{id}
</when>
<when test="username !='' and username != null">
and username=#{username}
</when>
<otherwise>
and sex=#{sex}
</otherwise>
</choose>
</where>
</select>也就是說(shuō),這里我們有三個(gè)條件,id,username,sex,只能選擇一個(gè)作為查詢(xún)條件
如果 id 不為空,那么查詢(xún)語(yǔ)句為:select * from user where id=?
如果 id 為空,那么看username 是否為空,如果不為空,那么語(yǔ)句為 select * from user whereusername=?;
如果 username 為空,那么查詢(xún)語(yǔ)句為 select * from user where sex=?
5、動(dòng)態(tài)SQL:trim 語(yǔ)句
trim標(biāo)記是一個(gè)格式化的標(biāo)記,可以完成set或者是where標(biāo)記的功能
①、用 trim 改寫(xiě)上面第二點(diǎn)的 if+where 語(yǔ)句
<select id="selectUserByUsernameAndSex" resultType="user" parameterType="com.ys.po.User">
select * from user
<!-- <where>
<if test="username != null">
username=#{username}
</if>
<if test="username != null">
and sex=#{sex}
</if>
</where> -->
<trim prefix="where" prefixOverrides="and | or">
<if test="username != null">
and username=#{username}
</if>
<if test="sex != null">
and sex=#{sex}
</if>
</trim>
</select>prefix:前綴
prefixoverride:去掉第一個(gè)and或者是or
②、用 trim 改寫(xiě)上面第三點(diǎn)的 if+set語(yǔ)句
<!-- 根據(jù) id 更新 user 表的數(shù)據(jù) -->
<update id="updateUserById" parameterType="com.ys.po.User">
update user u
<!-- <set>
<if test="username != null and username != ''">
u.username = #{username},
</if>
<if test="sex != null and sex != ''">
u.sex = #{sex}
</if>
</set> -->
<trim prefix="set" suffixOverrides=",">
<if test="username != null and username != ''">
u.username = #{username},
</if>
<if test="sex != null and sex != ''">
u.sex = #{sex},
</if>
</trim>
where id=#{id}
</update>suffix:后綴
suffixoverride:去掉最后一個(gè)逗號(hào)(也可以是其他的標(biāo)記,就像是上面前綴中的and一樣)
6、動(dòng)態(tài)SQL: SQL 片段
有時(shí)候可能某個(gè) sql 語(yǔ)句我們用的特別多,為了增加代碼的重用性,簡(jiǎn)化代碼,我們需要將這些代碼抽取出來(lái),然后使用時(shí)直接調(diào)用。
比如:假如我們需要經(jīng)常根據(jù)用戶(hù)名和性別來(lái)進(jìn)行聯(lián)合查詢(xún),那么我們就把這個(gè)代碼抽取出來(lái),如下:
<!-- 定義 sql 片段 -->
<sql id="selectUserByUserNameAndSexSQL">
<if test="username != null and username != ''">
AND username = #{username}
</if>
<if test="sex != null and sex != ''">
AND sex = #{sex}
</if>
</sql>引用 sql 片段
注意:
①、最好基于 單表來(lái)定義 sql 片段,提高片段的可重用性
②、在 sql 片段中最好不要包括 where
7、動(dòng)態(tài)SQL: foreach 語(yǔ)句
需求:我們需要查詢(xún) user 表中 id 分別為1,2,3的用戶(hù)
sql語(yǔ)句:
select * from user where id=1 or id=2 or id=3
select * from user where id in (1,2,3)
①、建立一個(gè) UserVo 類(lèi),里面封裝一個(gè) List<Integer> ids 的屬性
package com.ys.vo;
import java.util.List;
public class UserVo {
//封裝多個(gè)用戶(hù)的id
private List<Integer> ids;
public List<Integer> getIds() {
return ids;
}
public void setIds(List<Integer> ids) {
this.ids = ids;
}
} ②、我們用 foreach 來(lái)改寫(xiě)select * from user where id=1 or id=2 or id=3
<select id="selectUserByListId" parameterType="com.ys.vo.UserVo" resultType="com.ys.po.User">
select * from user
<where>
<!--
collection:指定輸入對(duì)象中的集合屬性
item:每次遍歷生成的對(duì)象
open:開(kāi)始遍歷時(shí)的拼接字符串
close:結(jié)束時(shí)拼接的字符串
separator:遍歷對(duì)象之間需要拼接的字符串
select * from user where 1=1 and (id=1 or id=2 or id=3)
-->
<foreach collection="ids" item="id" open="and (" close=")" separator="or">
id=#{id}
</foreach>
</where>
</select>測(cè)試:
//根據(jù)id集合查詢(xún)user表數(shù)據(jù)
@Test
public void testSelectUserByListId(){
String statement = "com.ys.po.userMapper.selectUserByListId";
UserVo uv = new UserVo();
List<Integer> ids = new ArrayList<>();
ids.add(1);
ids.add(2);
ids.add(3);
uv.setIds(ids);
List<User> listUser = session.selectList(statement, uv);
for(User u : listUser){
System.out.println(u);
}
session.close();
}③、我們用 foreach 來(lái)改寫(xiě)select * from user where id in (1,2,3)
<select id="selectUserByListId" parameterType="com.ys.vo.UserVo" resultType="com.ys.po.User">
select * from user
<where>
<!--
collection:指定輸入對(duì)象中的集合屬性
item:每次遍歷生成的對(duì)象
open:開(kāi)始遍歷時(shí)的拼接字符串
close:結(jié)束時(shí)拼接的字符串
separator:遍歷對(duì)象之間需要拼接的字符串
select * from user where 1=1 and id in (1,2,3)
-->
<foreach collection="ids" item="id" open="and id in (" close=") " separator=",">
#{id}
</foreach>
</where>
</select>8、總結(jié)
其實(shí)動(dòng)態(tài) sql 語(yǔ)句的編寫(xiě)往往就是一個(gè)拼接的問(wèn)題,為了保證拼接準(zhǔn)確,我們最好首先要寫(xiě)原生的 sql 語(yǔ)句出來(lái),然后在通過(guò) mybatis 動(dòng)態(tài)sql 對(duì)照著改,防止出錯(cuò)。
本篇文章就到這里了,希望能夠給你帶來(lái)幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
- Mybatis在注解上如何實(shí)現(xiàn)動(dòng)態(tài)SQL
- Mybatis超級(jí)強(qiáng)大的動(dòng)態(tài)SQL語(yǔ)句大全
- MyBatis深入解讀動(dòng)態(tài)SQL的實(shí)現(xiàn)
- MyBatis?超詳細(xì)講解動(dòng)態(tài)SQL的實(shí)現(xiàn)
- 關(guān)于mybatis遇到Integer類(lèi)型的參數(shù)時(shí)動(dòng)態(tài)sql需要注意條件
- MyBatis連接池的深入和動(dòng)態(tài)SQL詳解
- mybatis的動(dòng)態(tài)SQL以及連接池詳解
- MyBatis動(dòng)態(tài)SQL如何實(shí)現(xiàn)前端指定返回字段
- Mybatis詳解動(dòng)態(tài)SQL以及單表多表查詢(xún)的應(yīng)用
相關(guān)文章
Java實(shí)現(xiàn)簡(jiǎn)易畫(huà)圖板
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)簡(jiǎn)易畫(huà)圖板,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
Java中spring boot validation自定義注解使用方式
這篇文章主要介紹了Java中spring boot validation自定義注解使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08
SpringBoot自帶模板引擎Thymeleaf使用示例詳解
Thymeleaf是一款用于渲染XML/HTML5內(nèi)容的模板引擎,類(lèi)似JSP,它可以輕易的與SpringMVC等Web框架進(jìn)行集成作為Web應(yīng)用的模板引擎,本文給大家介紹SpringBoot自帶模板引擎Thymeleaf使用示例,感興趣的朋友一起看看吧2023-12-12
SpringBoot整合EasyExcel實(shí)現(xiàn)批量導(dǎo)入導(dǎo)出
這篇文章主要為大家詳細(xì)介紹了SpringBoot整合EasyExcel實(shí)現(xiàn)批量導(dǎo)入導(dǎo)出功能的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),需要的小伙伴可以參考下2024-03-03
springboot調(diào)用python文件的詳細(xì)方案
這篇文章主要為大家詳細(xì)介紹了springboot調(diào)用python文件的詳細(xì)方案,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-04-04
Springboot項(xiàng)目中單元測(cè)試時(shí)注入bean失敗的解決方案
這篇文章主要介紹了Springboot項(xiàng)目中單元測(cè)試時(shí)注入bean失敗的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11

