概念

  • 根據不同環境生成不同SQL語句,擺脫SQL語句拼接的煩惱【doge】

  • 本質:SQL語句的拼接

環境搭建

  • 搭建資料庫
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 DEFAULT CHARSET=utf8
  • 建立一個基礎Maven工程
    • 編寫配置檔案(略)
    • 編寫實體類
package cn.iris.pojo;

import java.util.Date;

public class Blog {
private String id;
private String title;
private String author;
private Date creatTime;
private int views; public Blog() {
} public Blog(String id, String title, String author, Date creatTime, int views) {
this.id = id;
this.title = title;
this.author = author;
this.creatTime = creatTime;
this.views = views;
} Getter&Setter @Override
public String toString()
}
    • 編寫實體類對應Mapper介面以及Mapper.xml檔案

IF語句

  • Mapper.xml
    <select id="queryBlogIF" parameterType="map" resultType="Blog">
SELECT * FROM blog WHERE 1=1
<if test="title != null">
AND title like #{title}
</if>
<if test="author != null">
AND author like #{author}
</if>
<if test="views != 0">
AND views >= #{views}
</if>
</select>

常用標籤

  1. where&set

  • 取代死板的關鍵字【WHERE】和【SET】,提供靈活的SQL語句拼接,在滿足條件後才加上對應SQL關鍵字
  1. choose&when&otherwise

  • 類似於Java中的switch-case-default結構
  • 匹配when則執行
  • Mapper.xml
    <select id="queryBlogChoose" parameterType="map" resultType="Blog">
SELECT * FROM blog
<where>
<choose>
<when test="title != null">
AND title like #{title}
</when>
<when test="author != null">
AND author like #{author}
</when>
<otherwise>
1=1
</otherwise>
</choose>
</where>
</select>
  1. trim

  • 提供自定義【WHERE】或【SET】標籤內容
<trim prefix="WHERE" prefixOverrides="AND |OR ">
...
</trim>
<trim prefix="SET" suffixOverrides=",">
...
</trim>

SQL片段

  • 某些可複用的功能片段
  • 定義SQL片段【sql標籤抽取部分sql程式碼】
<sql id="if-title-author">
...
</sql>
  • 引用SQL片段【include標籤引用對應id的sql程式碼片段】
<include refid="if-title-author"></include>
  • 注意事項:
    • 最好基於單表定義SQL片段
    • 不要存在where標籤

Foreach

  • 對集合遍歷,通常是構建IN語句時
  • open 第一個拼接的字元【'('】, separator 分隔符【','】,close 結尾拼接符【')'】,等同於IN(?,?,?)
  • collection對應傳入的集合,index為下標,item則為集合內對應下標元素,從而遍歷集合內元素
  • 若傳入型別為【Map】或者【Map.Entry 物件】的集合,index傳入鍵,item傳入值
  • Mapper.xml
    <!--
sql: SELECT * FROM blog WHERE 1=1 AND (id=1 OR id=2 OR id=3)
在傳遞Map時,Map中可存在集合
-->
<select id="queryByForeach" parameterType="map" resultType="Blog">
SELECT * FROM blog
<where>
<foreach collection="ids" item="id" index=""
open="AND (" separator="OR" close=")">
id = #{id}
</foreach>
</where>
</select>
  • 測試方法
    @Test
public void queryBlogByForeach() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
HashMap<Object, Object> hashMap = new HashMap<>();
ArrayList<Integer> ids = new ArrayList<>();
ids.add(1);
ids.add(4);
hashMap.put("ids",ids); List<Blog> blogs = mapper.queryByForeach(hashMap); for ( Blog blog : blogs) {
System.out.println(blog);
}
sqlSession.close();
}

小結

  • 動態SQL即是在拼接SQL語句,只要保證SQL的正確性,依照SQL的格式排列即可。
  • 編寫程式碼前應先實現SQL語法查詢,然後依照SQL規則實現動態SQL。