mybatis-plus/mybatis 自定義 sql 語句、動態 sql

Java 開發使用 mybatis-plus 來執行 sql 操作,往往比 mybatis 能夠省時省力,因為 mybatis-plus 封裝了很多常用的接口。但對於一些更為複雜的查詢來說,mybatis-plus 也相形見絀,還得需要我們自定義 sql 語句。本文就來介紹一下在使用了 mybatis-plus/mybatis 的情況下,如何自定義 sql 語句、動態 sql 等。

一、準備工作

MyBatis-Plus(簡稱 MP)是一個 MyBatis 的增強工具,在 MyBatis 的基礎上只做增強不做改變,為簡化開發、提高效率而生。在 Java 項目內,配置如下:

1、添加 pom 依賴

<code><dependency>
<groupid>com.baomidou/<groupid>
<artifactid>mybatis-plus-boot-starter/<artifactid>
<version>3.2.0/<version>
/<dependency>
<dependency>
<groupid>mysql/<groupid>
<artifactid>mysql-connector-java/<artifactid>
<version>8.0.17/<version>
/<dependency>

<dependency>
<groupid>org.projectlombok/<groupid>
<artifactid>lombok/<artifactid>
<optional>true/<optional>
/<dependency>
/<code>

2、修改配置文件

<code>spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/spring_boot_study?allowMultiQueries=true&useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=Asia/Shanghai&characterEncoding=utf8
username: root
password: mycat123

mybatis-plus:
# 自定義xml文件路徑
mapper-locations: classpath:/mapper/*Mapper.xml
# 自定義xml文件中用到的實體類路徑
type-aliases-package: com.study.spring.entity

configuration:
# 開啟駝峰映射
map-underscore-to-camel-case: true
cache-enabled: false
# 返回map時,true:當查詢數據為空時字段返回為null;false:不加這個查詢數據為空時,字段將被隱藏
call-setters-on-nulls: true
# sql日誌打印
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
/<code>

其中 spring.datasource.url 的某些參數說明如下:

  • useUnicode:是否使用 Unicode 字符集,如果需要指定編碼,則本參數值必須設置為 true 。
  • characterEncoding:當 useUnicode 設置為 true 時,指定字符編碼。比如可設置為 utf8 。
  • serverTimezone:指定 mysql 的時區,默認是 UTC ,與北京時間相差八個小時。平時使用時可設置為 GMT%2B8 或 Asia/Shanghai 。

二、自定義 sql

自定義 sql 分為兩種,一種是註解類型,一種是自定義 xml 類型。

1、註解類型

註解類型比較簡單,在 mapper 層的接口類方法上使用 @Select、@Update、@Insert、@Delete 等註解並加上自定義的 sql 語句,即可代表

查詢、更新、存儲、刪除 等操作。如下圖所示:

mybatis-plus/mybatis 自定義 sql 語句、動態 sql

雖然使用註解類型也可以實現動態 sql 的寫法,但總歸是太亂了,沒有自定義 xml 類型條理清晰。接下來介紹自定義 xml 類型的寫法。

2、自定義 xml 類型

由於配置文件內 mybatis-plus.mapper-locations 定義的 xml 文件路徑是:classpath:/mapper/*Mapper.xml 。所以需要先創建 resources/mapper 目錄,在這裡面創建 xxxMapper.xml ,來自定義 sql 語句。

  • select – 映射查詢語句
  • insert – 映射插入語句
  • update – 映射更新語句
  • delete – 映射刪除語句
1)首先要指定 mapper 接口文件:
<code>


<mapper>
...
/<mapper>
/<code>

這樣該 xml 文件就與 NovelMapper.java 這個接口類綁定了。接口類裡面的方法名與下文 xml 文件裡面的 id 值一一對應。

2)自定義查詢 sql
<code><select>
select
max(id) maxId
from
novel_type
/<select>
/<code>

id 為接口類裡面的方法名;resultType 指定 sql 返回的結果類型。

3)動態查詢 sql

動態查詢 sql 通常會使用 <where> 和 標籤。/<where>

where 元素只會在至少有一個子元素的條件返回 SQL 子句的情況下才去插入 “WHERE” 子句。而且,若語句的開頭為 “AND” 或 “OR”,where 元素也會將它們去除。

使用 標籤來判斷查詢字段是否符合查詢條件。 標籤裡面的 test 為判斷語句。

xml 裡面的變量用 #{} 表示。下面的查詢語句的參數類型是 hashmap,參數可直接用 key 值表示。如果接口方法參數裡面使用了 @Param("xxx"),則 xml 裡面的參數也要加上 xxx。比如:#{xxx.dl},其中 dl 是 hashmap 的 key 。

<code><select>
select
id, download, introduce, novelauthor, novelname, type
from

novel_type
<where>

download = #{query.dl}


and novelauthor = #{query.nu}

/<where>
/<select>
/<code>

還有一個知識點要說一下:<resultmap> ,定義 <resultmap> 可以解決類的屬性名和數據庫列名不一致的問題。/<resultmap>/<resultmap>

比如我將 NovelEntity 實體類的 novelAuthor 屬性修改為 novel_author,這時,返回的 novel_author 字段是接收不到 sql 查詢的 novelauthor 值的。但我們可以用 <resultmap> 來解決這種不一致的問題。/<resultmap>

<code>

<resultmap>

<result>
/<resultmap>


<select>
select
id, download, introduce, novelauthor, novelname, type

from
novel_type
where
download = #{query.dl}
/<select>
/<code>
4)動態插入 sql
<code>
<insert>
insert into novel_type(
download

,introduce


,novelauthor


,novelname


,type

)
values (
#{download}

,#{introduce}


,#{novelAuthor}


,#{novelName}


,#{type}

)

/<insert>
/<code>
5)動態更新 sql

假如當只有 novel_author 參數有值, 標籤會將 標籤內的 逗號 隱藏,不會使 sql 語句報錯。set 元素會動態前置 SET 關鍵字,同時也會刪掉無關的逗號

<code><update>
update novel_type


download = #{download},


introduce = #{introduce},


novelauthor = #{novel_author},


type = #{type}


where
novelName = #{novelName}
/<update>
/<code>
6)動態刪除 sql
<code><delete>
DELETE FROM novel_type
<where>

download = #{download}


and introduce = #{introduce}


and novelauthor = #{novel_author}


and type = #{type}


and novelName = #{novelName}

/<where>
/<delete>
/<code>
  • https://mybatis.org/mybatis-3/zh/dynamic-sql.html
  • https://www.cnblogs.com/rollenholt/p/3365866.html
  • https://mybatis.org/mybatis-3/zh/sqlmap-xml.html

自定義 sql 語句、動態 sql,其實還是用的 mybatis 的那套東西,mybatis-plus 只是將 mybatis 的某些部分又封裝了一遍,簡便了平時開發。

以上描述的兩種自定義 增刪改查 SQL 類型在工作中很常用,之所以整理,也是為了系統地瞭解、測試一遍,希望也能對大家有幫助!

本文分享自微信公眾號 - 大數據實戰演練(gh_f942bfc92d26)


分享到:


相關文章: