衝刺第三天——MyBatis-Plus
電子公文傳輸系統團隊衝刺第三天——MyBatis-Plus
一、Mybatis框架
MyBatis是一個支援普通SQL查詢,儲存過程和高階對映的優秀持久層框架。MyBatis消除了幾乎所有的JDBC程式碼和引數的手工設定以及對結果集的檢索封裝。MyBatis可以使用簡單的XML或注解用於配置和原始對映,將介面和普通的Java物件對映成資料庫中的記錄。
它的作用是可以與資料庫進行聯動,在其中寫sql語句,進行資料庫的增刪改查。
二、使用 SpringBoot 快速使用 MyBatis-Plus
(1)準備工作
需要 Java 開發環境(JDK)以及相應的開發工具(IDE)。
需要 maven(用來下載相關依賴的
需要 SpringBoot。
可以使用 IDEA 安裝一個 mybatis-plus 外掛。File-->Settings,點選設定中的Plugins進行外掛的安裝
(2)使用 IDE “Spring Initializer”工具建立一個 SpringBoot 專案
(3)新增 MyBatis-Plus 依賴
<!--資料庫持久化 及 外掛依賴--> <dependency> <groupId>com.baomidou</groupId> <artifactId>dynamic-datasource-spring-boot-starter</artifactId> <version>${mybatis-plus-dynamic.version}</version> </dependency>
(4)為了測試開發,此處使用 mysql 8,需要引入 mysql 相關依賴。
為了簡化程式碼,引入 lombok 依賴
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <!--lombok--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency>
(5)完整依賴檔案
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.6.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.company.project</groupId> <artifactId>springboot-manager</artifactId> <version>0.0.1-SNAPSHOT</version> <name>company-frame</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <mybatis-plus.version>3.4.0</mybatis-plus.version> <mybatis-plus-dynamic.version>2.5.5</mybatis-plus-dynamic.version> <commons.lang.version>2.6</commons.lang.version> <commons.io.version>2.5</commons.io.version> <commons.configuration.version>1.10</commons.configuration.version> <velocity.version>1.7</velocity.version> <quartz.version>2.3.2</quartz.version> <shiro.version>1.4.0</shiro.version> <druid.version>1.1.10</druid.version> <fastjson.version>1.2.74</fastjson.version> <thymeleaf-shiro.version>2.0.0</thymeleaf-shiro.version> <knife4j.version>2.0.2</knife4j.version> <easy-captcha.version>1.6.2</easy-captcha.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> <!--資料來源--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>${druid.version}</version> </dependency> <!--shiro 依賴--> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-spring</artifactId> <version>${shiro.version}</version> </dependency> <!--lombok--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <!--fastJson--> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>${fastjson.version}</version> </dependency> <!--thymeleaf預設使用html5規則標籤必須閉合等 使用次此包正常解析--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>com.github.theborakompanioni</groupId> <artifactId>thymeleaf-extras-shiro</artifactId> <version>${thymeleaf-shiro.version}</version> </dependency> <!--資料庫持久化 及 外掛依賴--> <dependency> <groupId>com.baomidou</groupId> <artifactId>dynamic-datasource-spring-boot-starter</artifactId> <version>${mybatis-plus-dynamic.version}</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus</artifactId> <version>${mybatis-plus.version}</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>${mybatis-plus.version}</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>knife4j-spring-boot-starter</artifactId> <version>${knife4j.version}</version> </dependency> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>${commons.lang.version}</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>${commons.io.version}</version> </dependency> <dependency> <groupId>commons-configuration</groupId> <artifactId>commons-configuration</artifactId> <version>${commons.configuration.version}</version> </dependency> <dependency> <groupId>com.github.whvcse</groupId> <artifactId>easy-captcha</artifactId> <version>${easy-captcha.version}</version> </dependency> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.5.1</version> </dependency> </dependencies> <build> <finalName>manager</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <fork>true</fork> </configuration> </plugin> </plugins> </build> <repositories> <repository> <id>aliyun-repos</id> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>aliyun-plugin</id> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <snapshots> <enabled>false</enabled> </snapshots> </pluginRepository> </pluginRepositories> </project>
(6)在 application.yml 檔案中配置 mysql 資料來源資訊。
datasource: dynamic: primary: master #設定預設的資料來源或者資料來源組,預設值即為master datasource: master: username: root password: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3316/company_project?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=GMT%2b8
(7)編寫表對應的實體類
(8)編寫操作實體類的 Mapper 類。
直接繼承 BaseMapper,這是 mybatis-plus 封裝好的類。
注:這裡直接繼承BaseMapper,可以省去xml的編寫,當然也可以通過xml自定義sql查詢語句。
(9)實體類、Mapper 類都寫好了,就可以使用了。
在啟動類裡掃描 Mapper 類,即新增 @MapperScan 註解,接著測試一下。
注:@MapperScan作用:指定要變成實現類的介面所在的包,然後包下面的所有介面在編譯之後都會生成相應的實現類
新增位置:是在Springboot啟動類上面新增。
新增@MapperScan(“com.winter.dao”)註解以後,com.winter.dao包下面的介面類,在編譯之後都會生成相應的實現類
(10)通過以上操作,就能對資料庫中的表進行增刪查改,而不需要去編寫xml檔案。
三、Mybatis-Plus增刪查改操作
1、Mapper 介面方法完成增刪查改(CRUD)
使用程式碼生成器生成的 mapper 介面中,其繼承了 BaseMapper 介面。
而 BaseMapper 介面中封裝了一系列 CRUD 常用操作,可以直接使用,而不用自定義 xml 與 sql 語句進行 CRUD 操作(當然根據實際開發需要,自定義 sql 還是有必要的)。
新增資料:(增)
int insert(T entity); // 插入一條記錄 注: T 表示任意實體型別 entity 表示實體物件
刪除資料:(刪)
int deleteById(Serializable id); // 根據主鍵 ID 刪除 int deleteByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap); // 根據 map 定義欄位的條件刪除 int delete(@Param(Constants.WRAPPER) Wrapper<T> wrapper); // 根據實體類定義的 條件刪除物件 int deleteBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList); // 進行批量刪除 注: id 表示 主鍵 ID columnMap 表示表字段的 map 物件 wrapper 表示實體物件封裝操作類,可以為 null。 idList 表示 主鍵 ID 集合(列表、陣列),不能為 null 或 empty
修改資料:(改)
int updateById(@Param(Constants.ENTITY) T entity); // 根據 ID 修改實體物件。 int update(@Param(Constants.ENTITY) T entity, @Param(Constants.WRAPPER) Wrapper<T> updateWrapper); // 根據 updateWrapper 條件修改實體物件 注: update 中的 entity 為 set 條件,可以為 null。 updateWrapper 表示實體物件封裝操作類(可以為 null,裡面的 entity 用於生成 where 語句)
查詢資料:(查)
T selectById(Serializable id); // 根據 主鍵 ID 查詢資料 List<T> selectBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList); // 進行批量查詢 List<T> selectByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap); // 根據表字段條件查詢 T selectOne(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper); // 根據實體類封裝物件 查詢一條記錄 Integer selectCount(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper); // 查詢記錄的總條數 List<T> selectList(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper); // 查詢所有記錄(返回 entity 集合) List<Map<String, Object>> selectMaps(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper); // 查詢所有記錄(返回 map 集合) List<Object> selectObjs(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper); // 查詢所有記錄(但只儲存第一個欄位的值) <E extends IPage<T>> E selectPage(E page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper); // 查詢所有記錄(返回 entity 集合),分頁 <E extends IPage<Map<String, Object>>> E selectMapsPage(E page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper); // 查詢所有記錄(返回 map 集合),分頁
注: queryWrapper 表示實體物件封裝操作類(可以為 null) page 表示分頁查詢條件
四、Mybatis最後總結
(一)MyBatis的功能架構
我們可以把Mybatis的功能架構分為三層:
(1)API介面層:提供給外部使用的介面API,開發人員通過這些本地API來操縱資料庫。介面層一接收到呼叫請求就會呼叫資料處理層來完成具體的資料處理。
(2)資料處理層:負責具體的SQL查詢、SQL解析、SQL執行和執行結果對映處理等。它主要的目的是根據呼叫的請求完成一次資料庫操作。
(3)基礎支撐層:負責最基礎的功能支撐,包括連線管理、事務管理、配置載入和快取處理,這些都是共用的東西,將他們抽取出來作為最基礎的元件。為上層的資料處理層提供最基礎的支撐。
(二)MyBatis的優缺點
(1)優點:
- 簡單易學:本身就很小且簡單。沒有任何第三方依賴,最簡單安裝只要兩個jar檔案+配置幾個sql對映檔案易於學習,易於使用,通過文件和原始碼,可以比較完全的掌握它的設計思路和實現。
- 靈活:mybatis不會對應用程式或者資料庫的現有設計強加任何影響。 sql寫在xml裡,便於統一管理和優化。通過sql基本上可以實現我們不使用資料訪問框架可以實現的所有功能,或許更多。
- 解除sql與程式程式碼的耦合:通過提供DAL層,將業務邏輯和資料訪問邏輯分離,使系統的設計更清晰,更易維護,更易單元測試。sql和程式碼的分離,提高了可維護性。
- 提供對映標籤,支援物件與資料庫的orm欄位關係對映
- 提供物件關係對映標籤,支援物件關係組建維護
- 提供xml標籤,支援編寫動態sql。
(2)缺點:
- 編寫SQL語句時工作量很大,尤其是欄位多、關聯表多時,更是如此。
- SQL語句依賴於資料庫,導致資料庫移植性差,不能更換資料庫。
- 框架還是比較簡陋,功能尚有缺失,雖然簡化了資料繫結程式碼,但是整個底層資料庫查詢實際還是要自己寫的,工作量也比較大,而且不太容易適應快速資料庫修改。
- 二級快取機制不佳