網站首頁 編程語言 正文
目錄
1.if和where標簽
2.set標簽
3.trim標簽
4.choose/when/otherwise標簽
5.foreach標簽
6.sql標簽
7.總結
1)where標簽的作用:
2)if標簽的作用:
3)set標簽的作用:
4)trim標簽的作用:
5)choose/when/otherwise標簽的作用:
6)foreach標簽的作用:
7)sql標簽的作用:
1.if和where標簽
<!-- List<Employee> selectEmployeeByCondition(Employee employee); -->
<select id="selectEmployeeByCondition" resultType="employee">
select emp_id,emp_name,emp_salary from t_emp
<!-- where標簽會自動去掉“標簽體內前面多余的and/or” -->
<where>
<!-- 使用if標簽,讓我們可以有選擇的加入SQL語句的片段。這個SQL語句片段是否要加入整個SQL語句,就看if標簽判斷的結果是否為true -->
<!-- 在if標簽的test屬性中,可以訪問實體類的屬性,不可以訪問數據庫表的字段 -->
<if test="empName != null">
<!-- 在if標簽內部,需要訪問接口的參數時還是正常寫#{} -->
or emp_name=#{empName}
</if>
<if test="empSalary > 2000">
or emp_salary>#{empSalary}
</if>
<!--
第一種情況:所有條件都滿足 WHERE emp_name=? or emp_salary>?
第二種情況:部分條件滿足 WHERE emp_salary>?
第三種情況:所有條件都不滿足 沒有where子句
-->
</where>
</select>
2.set標簽
<!-- void updateEmployeeDynamic(Employee employee) -->
<update id="updateEmployeeDynamic">
update t_emp
<!-- set emp_name=#{empName},emp_salary=#{empSalary} -->
<!-- 使用set標簽動態管理set子句,并且動態去掉兩端多余的逗號 -->
<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=?
沒有set子句的update語句會導致SQL語法錯誤
-->
</update>
3.trim標簽
使用trim標簽控制條件部分兩端是否包含某些字符
- prefix屬性:指定要動態添加的前綴
- suffix屬性:指定要動態添加的后綴
- prefixOverrides屬性:指定要動態去掉的前綴,使用“|”分隔有可能的多個值
- suffixOverrides屬性:指定要動態去掉的后綴,使用“|”分隔有可能的多個值
<!-- 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屬性指定要動態添加的前綴 -->
<!-- suffix屬性指定要動態添加的后綴 -->
<!-- prefixOverrides屬性指定要動態去掉的前綴,使用“|”分隔有可能的多個值 -->
<!-- suffixOverrides屬性指定要動態去掉的后綴,使用“|”分隔有可能的多個值 -->
<!-- 當前例子用where標簽實現更簡潔,但是trim標簽更靈活,可以用在任何有需要的地方 -->
<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標簽
在多個分支條件中,僅執行一個。
- 從上到下依次執行條件判斷
- 遇到的第一個滿足條件的分支會被采納
- 被采納分支后面的分支都將不被考慮
- 如果所有的when分支都不滿足,那么就執行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>
<!--
第一種情況:第一個when滿足條件 where emp_name=?
第二種情況:第二個when滿足條件 where emp_salary < 3000
第三種情況:兩個when都不滿足 where 1=1 執行了otherwise
-->
</select>
5.foreach標簽
例如批量插入:
<!--
collection屬性:要遍歷的集合
item屬性:遍歷集合的過程中能得到每一個具體對象,在item屬性中設置一個名字,將來通過這個名字引用遍歷出來的對象
separator屬性:指定當foreach標簽的標簽體重復拼接字符串時,各個標簽體字符串之間的分隔符
open屬性:指定整個循環把字符串拼好后,字符串整體的前面要添加的字符串
close屬性:指定整個循環把字符串拼好后,字符串整體的后面要添加的字符串
index屬性:這里起一個名字,便于后面引用
遍歷List集合,這里能夠得到List集合的索引值
遍歷Map集合,這里能夠得到Map集合的key
-->
<foreach collection="empList" item="emp" separator="," open="values" index="myIndex">
<!-- 在foreach標簽內部如果需要引用遍歷得到的具體的一個對象,需要使用item屬性聲明的名稱 -->
(#{emp.empName},#{myIndex},#{emp.empSalary},#{emp.empGender})
</foreach>
批量更新時需要注意
上面批量插入的例子本質上是一條SQL語句,而實現批量更新則需要多條SQL語句拼起來,用分號分開。也就是一次性發送多條SQL語句讓數據庫執行。此時需要在數據庫連接信息的URL地址中設置:
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標簽
抽取重復的sql片段
<!-- 使用sql標簽抽取重復出現的SQL片段 -->
<sql id="mySelectSql">
select emp_id,emp_name,emp_age,emp_salary,emp_gender from t_emp
</sql>
引用已抽取的sql片段
<!-- 使用include標簽引用聲明的SQL片段 -->
<include refid="mySelectSql"/>
7.總結
1)where標簽的作用:
1.自動 添加 where關鍵字 2.自動 去掉 標簽體前面多余的and和or關鍵字
2)if標簽的作用:
1.根據if標簽判斷的結果,選擇是否加入sql語句的片段
3)set標簽的作用:
1.自動 添加 set關鍵字 2.自動 去掉 兩端多余的逗號
4)trim標簽的作用:
1.動態添加或去掉前后綴
5)choose/when/otherwise標簽的作用:
1.在多個分支條件中,僅執行一個
6)foreach標簽的作用:
1.遍歷
7)sql標簽的作用:
1.抽取重復的sql片段
原文鏈接:https://blog.csdn.net/weixin_69134926/article/details/136487399
- 上一篇:沒有了
- 下一篇:沒有了
相關推薦
- 2022-08-04 react使用mobx封裝管理用戶登錄的store示例詳解_React
- 2022-10-04 numpy稀疏矩陣的實現_python
- 2022-03-14 has been blocked by CORS policy: Response to prefl
- 2023-05-29 Postgresql數據庫角色創建登錄詳解_PostgreSQL
- 2022-08-26 C#中WPF內存回收與釋放LierdaCracker的實現_C#教程
- 2022-03-28 python將天數轉換為日期字符串的方法實例_python
- 2022-03-20 .NET+Sqlite支持加密的操作方法_實用技巧
- 2024-03-10 【Redis】Redis中大key怎么處理?
- 欄目分類
-
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支