網(wǎng)站首頁 編程語言 正文
目錄
- 動態(tài)SQL
- if
- trim (where, set)
- choose (when, otherwise)
- SQL片段
- foreach
Mybatis 官方文檔: https://mybatis.org/mybatis-3/zh/dynamic-sql.html
動態(tài)SQL
- if
- choose (when, otherwise)
- trim (where, set)
- foreach
數(shù)據(jù)表
CREATE TABLE `blog` (
`id` VARCHAR(50) NOT NULL COMMENT '博客id',
`title` VARCHAR(100) NOT NULL COMMENT '博客標題',
`author` VARCHAR(30) NOT NULL COMMENT '博客作者',
`create_time` DATETIME NOT NULL COMMENT '創(chuàng)建時間',
`views` INT(30) NOT NULL COMMENT '瀏覽量'
) ENGINE=INNODB CHARSET=utf8 COLLATE=utf8_general_ci;
實體類
public class Blog {
private String id;
private String title;
private String auther;
private java.util.Date createTime;
private int views;
}
開啟駝峰命名自動映射
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
settings>
if
<select id="queryBlogIf" parameterType="map" resultType="Blog">
select * from blog where 1=1
<if test="title != null">
and title = #{title}
if>
select>
trim (where, set)
trim
- prefix:在包裹的代碼塊前面添加一個
xxx
- prefixOverrides:屬性會忽略通過管道符分隔的文本序列(注意此例中的空格是必要的)
- suffixOverrides: 忽略最后一個
xxx
## 等價于 where 標簽
<trim prefix="WHERE" prefixOverrides="AND |OR ">
...
trim>
## 等價于 set 標簽
<trim prefix="SET" suffixOverrides=",">
...
trim>
where
若子句的開頭為 “AND” 或 “OR”,where 元素也會將它們?nèi)コ?/strong>
HashMap hashMap = new HashMap();
hashMap.put("title","java");
hashMap.put("author","自己");
<select id="queryBlogIf" parameterType="map" resultType="Blog">
select * from blog
<where>
<if test="title != null">
title = #{title}
if>
<if test="author != null">
and author = #{author}
if>
where>
select>
set
set 元素會動態(tài)地在行首插入 SET 關鍵字,并會刪掉額外的逗號
<update id="updateBlog" parameterType="map">
update blog
<set>
<if test="title != null">
title = #{title},
if>
<if test="author != null">
author = #{author},
if>
set>
where id = #{id}
update>
choose (when, otherwise)
choose
類似 Java 中的 switch
HashMap hashMap = new HashMap();
hashMap.put("title","java");
// hashMap.put("author","自己");
hashMap.put("views", 1000);
<select id="queryBlogChoose" parameterType="map" resultType="Blog">
select * from blog
<where>
<choose>
<when test="title != null">
title = #{title}
when>
<when test="author != null">
author = #{author}
when>
<otherwise>
views = #{views}
otherwise>
choose>
where>
select>
SQL片段
我們可以把一些功能抽取出來,方便復用
- sql:抽取代碼片段
- include: 引用sql抽取的代碼片段
<sql id="if-title-author">
<if test="title != null">
title = #{title}
if>
<if test="author != null">
and author = #{author}
if>
sql>
<select id="queryBlogIf" parameterType="map" resultType="Blog">
select * from blog
<where>
<include refid="if-title-author"/>
where>
select>
注意事項
- 最好基于單表來定義SQL片段
- 不要存在 where 標簽
foreach
- collection:遍歷對象
- item:每一項
- index:索引
- open:開頭
- separator:分隔符
- close:結(jié)尾
int[] array = new int[]{10, 5000, 9999};
List<Integer> list = new ArrayList<>();
for (int i : array) {
list.add(i);
}
<select id="getBlogIn" parameterType="list" resultType="Blog">
select * from blog
<where>
<if test="list != null and list.size() > 0">
views in
<foreach collection="list" item="id" index="index" open="(" separator="," close=")">
#{id}
foreach>
if>
where>
select>
原文鏈接:https://blog.csdn.net/weixin_44953227/article/details/112790534
相關推薦
- 2022-11-25 Python?Django教程之模型中字段驗證詳解_python
- 2022-04-12 原生drag拖拽后元素過大,擋住其他可拖動位置無法拖動問題
- 2022-07-12 Linux命令之美|linux使用tar誤解壓之后,如何刪除解壓后的文件
- 2022-05-23 vmware增加新硬盤無需重啟即可生效的命令腳本_VMware
- 2023-04-01 路由react-router-dom的基本使用教程_React
- 2022-12-06 c++入門必學算法之快速冪思想及實現(xiàn)_C 語言
- 2022-03-24 基于Docker的可持續(xù)交付問題_docker
- 2022-10-25 linux服務器校對時間方法命令詳解_Linux
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支