網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
目錄
1.if和where標(biāo)簽
2.set標(biāo)簽
3.trim標(biāo)簽
4.choose/when/otherwise標(biāo)簽
5.foreach標(biāo)簽
6.sql標(biāo)簽
7.總結(jié)
1)where標(biāo)簽的作用:
2)if標(biāo)簽的作用:
3)set標(biāo)簽的作用:
4)trim標(biāo)簽的作用:
5)choose/when/otherwise標(biāo)簽的作用:
6)foreach標(biāo)簽的作用:
7)sql標(biāo)簽的作用:
1.if和where標(biāo)簽
<!-- List<Employee> selectEmployeeByCondition(Employee employee); -->
<select id="selectEmployeeByCondition" resultType="employee">
select emp_id,emp_name,emp_salary from t_emp
<!-- where標(biāo)簽會(huì)自動(dòng)去掉“標(biāo)簽體內(nèi)前面多余的and/or” -->
<where>
<!-- 使用if標(biāo)簽,讓我們可以有選擇的加入SQL語(yǔ)句的片段。這個(gè)SQL語(yǔ)句片段是否要加入整個(gè)SQL語(yǔ)句,就看if標(biāo)簽判斷的結(jié)果是否為true -->
<!-- 在if標(biāo)簽的test屬性中,可以訪問(wèn)實(shí)體類的屬性,不可以訪問(wèn)數(shù)據(jù)庫(kù)表的字段 -->
<if test="empName != null">
<!-- 在if標(biāo)簽內(nèi)部,需要訪問(wèn)接口的參數(shù)時(shí)還是正常寫(xiě)#{} -->
or emp_name=#{empName}
</if>
<if test="empSalary > 2000">
or emp_salary>#{empSalary}
</if>
<!--
第一種情況:所有條件都滿足 WHERE emp_name=? or emp_salary>?
第二種情況:部分條件滿足 WHERE emp_salary>?
第三種情況:所有條件都不滿足 沒(méi)有where子句
-->
</where>
</select>
2.set標(biāo)簽
<!-- void updateEmployeeDynamic(Employee employee) -->
<update id="updateEmployeeDynamic">
update t_emp
<!-- set emp_name=#{empName},emp_salary=#{empSalary} -->
<!-- 使用set標(biāo)簽動(dòng)態(tài)管理set子句,并且動(dòng)態(tài)去掉兩端多余的逗號(hào) -->
<set>
<if test="empName != null">
emp_name=#{empName},
</if>
<if test="empSalary < 3000">
emp_salary=#{empSalary},
</if>
</set>
where emp_id=#{empId}
<!--
第一種情況:所有條件都滿足 SET emp_name=?, emp_salary=?
第二種情況:部分條件滿足 SET emp_salary=?
第三種情況:所有條件都不滿足 update t_emp where emp_id=?
沒(méi)有set子句的update語(yǔ)句會(huì)導(dǎo)致SQL語(yǔ)法錯(cuò)誤
-->
</update>
3.trim標(biāo)簽
使用trim標(biāo)簽控制條件部分兩端是否包含某些字符
- prefix屬性:指定要?jiǎng)討B(tài)添加的前綴
- suffix屬性:指定要?jiǎng)討B(tài)添加的后綴
- prefixOverrides屬性:指定要?jiǎng)討B(tài)去掉的前綴,使用“|”分隔有可能的多個(gè)值
- suffixOverrides屬性:指定要?jiǎng)討B(tài)去掉的后綴,使用“|”分隔有可能的多個(gè)值
<!-- List<Employee> selectEmployeeByConditionByTrim(Employee employee) -->
<select id="selectEmployeeByConditionByTrim" resultType="com.atguigu.mybatis.entity.Employee">
select emp_id,emp_name,emp_age,emp_salary,emp_gender
from t_emp
<!-- prefix屬性指定要?jiǎng)討B(tài)添加的前綴 -->
<!-- suffix屬性指定要?jiǎng)討B(tài)添加的后綴 -->
<!-- prefixOverrides屬性指定要?jiǎng)討B(tài)去掉的前綴,使用“|”分隔有可能的多個(gè)值 -->
<!-- suffixOverrides屬性指定要?jiǎng)討B(tài)去掉的后綴,使用“|”分隔有可能的多個(gè)值 -->
<!-- 當(dāng)前例子用where標(biāo)簽實(shí)現(xiàn)更簡(jiǎn)潔,但是trim標(biāo)簽更靈活,可以用在任何有需要的地方 -->
<trim prefix="where" suffixOverrides="and|or">
<if test="empName != null">
emp_name=#{empName} and
</if>
<if test="empSalary > 3000">
emp_salary>#{empSalary} and
</if>
<if test="empAge <= 20">
emp_age=#{empAge} or
</if>
<if test="empGender=='male'">
emp_gender=#{empGender}
</if>
</trim>
</select>
4.choose/when/otherwise標(biāo)簽
在多個(gè)分支條件中,僅執(zhí)行一個(gè)。
- 從上到下依次執(zhí)行條件判斷
- 遇到的第一個(gè)滿足條件的分支會(huì)被采納
- 被采納分支后面的分支都將不被考慮
- 如果所有的when分支都不滿足,那么就執(zhí)行otherwise分支
<!-- List<Employee> selectEmployeeByConditionByChoose(Employee employee) -->
<select id="selectEmployeeByConditionByChoose" resultType="com.atguigu.mybatis.entity.Employee">
select emp_id,emp_name,emp_salary from t_emp
where
<choose>
<when test="empName != null">emp_name=#{empName}</when>
<when test="empSalary < 3000">emp_salary < 3000</when>
<otherwise>1=1</otherwise>
</choose>
<!--
第一種情況:第一個(gè)when滿足條件 where emp_name=?
第二種情況:第二個(gè)when滿足條件 where emp_salary < 3000
第三種情況:兩個(gè)when都不滿足 where 1=1 執(zhí)行了otherwise
-->
</select>
5.foreach標(biāo)簽
例如批量插入:
<!--
collection屬性:要遍歷的集合
item屬性:遍歷集合的過(guò)程中能得到每一個(gè)具體對(duì)象,在item屬性中設(shè)置一個(gè)名字,將來(lái)通過(guò)這個(gè)名字引用遍歷出來(lái)的對(duì)象
separator屬性:指定當(dāng)foreach標(biāo)簽的標(biāo)簽體重復(fù)拼接字符串時(shí),各個(gè)標(biāo)簽體字符串之間的分隔符
open屬性:指定整個(gè)循環(huán)把字符串拼好后,字符串整體的前面要添加的字符串
close屬性:指定整個(gè)循環(huán)把字符串拼好后,字符串整體的后面要添加的字符串
index屬性:這里起一個(gè)名字,便于后面引用
遍歷List集合,這里能夠得到List集合的索引值
遍歷Map集合,這里能夠得到Map集合的key
-->
<foreach collection="empList" item="emp" separator="," open="values" index="myIndex">
<!-- 在foreach標(biāo)簽內(nèi)部如果需要引用遍歷得到的具體的一個(gè)對(duì)象,需要使用item屬性聲明的名稱 -->
(#{emp.empName},#{myIndex},#{emp.empSalary},#{emp.empGender})
</foreach>
批量更新時(shí)需要注意
上面批量插入的例子本質(zhì)上是一條SQL語(yǔ)句,而實(shí)現(xiàn)批量更新則需要多條SQL語(yǔ)句拼起來(lái),用分號(hào)分開(kāi)。也就是一次性發(fā)送多條SQL語(yǔ)句讓數(shù)據(jù)庫(kù)執(zhí)行。此時(shí)需要在數(shù)據(jù)庫(kù)連接信息的URL地址中設(shè)置:
atguigu.dev.url=jdbc:mysql:///mybatis-example?allowMultiQueries=true
<!-- int updateEmployeeBatch(@Param("empList") List<Employee> empList) -->
<update id="updateEmployeeBatch">
<foreach collection="empList" item="emp" separator=";">
update t_emp set emp_name=#{emp.empName} where emp_id=#{emp.empId}
</foreach>
</update>
6.sql標(biāo)簽
抽取重復(fù)的sql片段
<!-- 使用sql標(biāo)簽抽取重復(fù)出現(xiàn)的SQL片段 -->
<sql id="mySelectSql">
select emp_id,emp_name,emp_age,emp_salary,emp_gender from t_emp
</sql>
引用已抽取的sql片段
<!-- 使用include標(biāo)簽引用聲明的SQL片段 -->
<include refid="mySelectSql"/>
7.總結(jié)
1)where標(biāo)簽的作用:
1.自動(dòng) 添加 where關(guān)鍵字 2.自動(dòng) 去掉 標(biāo)簽體前面多余的and和or關(guān)鍵字
2)if標(biāo)簽的作用:
1.根據(jù)if標(biāo)簽判斷的結(jié)果,選擇是否加入sql語(yǔ)句的片段
3)set標(biāo)簽的作用:
1.自動(dòng) 添加 set關(guān)鍵字 2.自動(dòng) 去掉 兩端多余的逗號(hào)
4)trim標(biāo)簽的作用:
1.動(dòng)態(tài)添加或去掉前后綴
5)choose/when/otherwise標(biāo)簽的作用:
1.在多個(gè)分支條件中,僅執(zhí)行一個(gè)
6)foreach標(biāo)簽的作用:
1.遍歷
7)sql標(biāo)簽的作用:
1.抽取重復(fù)的sql片段
原文鏈接:https://blog.csdn.net/weixin_69134926/article/details/136487399
- 上一篇:沒(méi)有了
- 下一篇:沒(méi)有了
相關(guān)推薦
- 2022-08-05 C++詳細(xì)講解模擬實(shí)現(xiàn)位圖和布隆過(guò)濾器的方法_C 語(yǔ)言
- 2022-06-17 Android?SearchView搜索控件使用方法詳解_Android
- 2022-07-13 Python中常用序列數(shù)據(jù)結(jié)構(gòu)
- 2022-07-11 Android?Studio實(shí)現(xiàn)注冊(cè)頁(yè)面跳轉(zhuǎn)登錄頁(yè)面的創(chuàng)建_Android
- 2022-05-03 C#設(shè)計(jì)模式之工廠模式_C#教程
- 2022-05-05 Entity?Framework表拆分為多個(gè)實(shí)體_實(shí)用技巧
- 2022-08-26 Python中def()函數(shù)的實(shí)戰(zhàn)練習(xí)題_python
- 2022-05-09 OpenCV相機(jī)標(biāo)定的全過(guò)程記錄_C 語(yǔ)言
- 欄目分類
-
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門(mén)
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支