網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
目錄
- 動(dòng)態(tài)SQL
- if
- trim (where, set)
- choose (when, otherwise)
- SQL片段
- foreach
Mybatis 官方文檔: https://mybatis.org/mybatis-3/zh/dynamic-sql.html
動(dòng)態(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 '博客標(biāo)題',
`author` VARCHAR(30) NOT NULL COMMENT '博客作者',
`create_time` DATETIME NOT NULL COMMENT '創(chuàng)建時(shí)間',
`views` INT(30) NOT NULL COMMENT '瀏覽量'
) ENGINE=INNODB CHARSET=utf8 COLLATE=utf8_general_ci;
實(shí)體類
public class Blog {
private String id;
private String title;
private String auther;
private java.util.Date createTime;
private int views;
}
開(kāi)啟駝峰命名自動(dòng)映射
<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:在包裹的代碼塊前面添加一個(gè)
xxx
- prefixOverrides:屬性會(huì)忽略通過(guò)管道符分隔的文本序列(注意此例中的空格是必要的)
- suffixOverrides: 忽略最后一個(gè)
xxx
## 等價(jià)于 where 標(biāo)簽
<trim prefix="WHERE" prefixOverrides="AND |OR ">
...
trim>
## 等價(jià)于 set 標(biāo)簽
<trim prefix="SET" suffixOverrides=",">
...
trim>
where
若子句的開(kāi)頭為 “AND” 或 “OR”,where 元素也會(huì)將它們?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 元素會(huì)動(dòng)態(tài)地在行首插入 SET 關(guān)鍵字,并會(huì)刪掉額外的逗號(hào)
<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片段
我們可以把一些功能抽取出來(lái),方便復(fù)用
- 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>
注意事項(xiàng)
- 最好基于單表來(lái)定義SQL片段
- 不要存在 where 標(biāo)簽
foreach
- collection:遍歷對(duì)象
- item:每一項(xiàng)
- index:索引
- open:開(kāi)頭
- 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
相關(guān)推薦
- 2022-04-07 你知道怎么基于?React?封裝一個(gè)組件嗎_React
- 2022-09-18 IOS開(kāi)發(fā)壓縮后圖片模糊問(wèn)題解決_IOS
- 2023-01-31 golang定時(shí)任務(wù)cron項(xiàng)目實(shí)操指南_Golang
- 2022-08-29 .NET?Core使用Eureka實(shí)現(xiàn)服務(wù)注冊(cè)_實(shí)用技巧
- 2023-02-04 Rust?語(yǔ)言的全鏈路追蹤庫(kù)?tracing使用方法_Rust語(yǔ)言
- 2022-03-25 在NET?Core?中獲取?CPU?使用率_ASP.NET
- 2022-08-11 C++超詳細(xì)講解強(qiáng)制類型轉(zhuǎn)換的用法_C 語(yǔ)言
- 2022-06-30 python數(shù)據(jù)操作之lambda表達(dá)式詳情_(kāi)python
- 最近更新
-
- 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概述快速入門
- 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)程分支