目錄
- 動態SQL
- if
- trim (where, set)
- choose (when, otherwise)
- SQL片段
- foreach
Mybatis 官方檔案: https://mybatis.org/mybatis-3/zh/dynamic-sql.html
動態SQL
- if
- choose (when, otherwise)
- trim (where, set)
- foreach
資料表
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 '創建時間',
`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="https://www.cnblogs.com/pojo/p/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 元素也會將它們去除,
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 元素會動態地在行首插入 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:結尾
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://www.uj5u.com/houduan/250452.html
標籤:Java
下一篇:java開發學習路線~
