溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

使用MyBatis Plus實(shí)現(xiàn)整合封裝——SpringBoot篇

發(fā)布時(shí)間:2020-11-07 15:24:55 來源:億速云 閱讀:634 作者:Leah 欄目:開發(fā)技術(shù)

這篇文章將為大家詳細(xì)講解有關(guān)使用MyBatis Plus實(shí)現(xiàn)整合封裝——SpringBoot篇,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。

前言

  MyBatis-Plus是一款MyBatis的增強(qiáng)工具(簡稱MP),為簡化開發(fā)、提高效率,但我們并沒有直接使用MP的CRUD接口,而是在原來的基礎(chǔ)上封裝一層通用代碼,單表繼承我們的通用代碼,實(shí)現(xiàn)了單表的基礎(chǔ)get、save(插入/更新)、list、page、delete接口,使用Vo去接收、傳輸數(shù)據(jù),實(shí)體負(fù)責(zé)與數(shù)據(jù)庫表映射。

  這樣做的目的是與我們之前的那套jpa保持編碼風(fēng)格上的一致,當(dāng)我們的通用接口不能滿足要求時(shí),應(yīng)當(dāng)先考慮使用MP的Service層CRUD接口,然后是Mapper的接口,最后才是自定義查詢,本文將記錄實(shí)現(xiàn)過程

  MyBatis-Plus官網(wǎng):https://baomidou.com/

  創(chuàng)建項(xiàng)目

  在我們的工程里新建子工程springboot-mybatis-plus,pom繼承父工程,引入Mybatis-Plus相關(guān)jar包

<!--添加MyBatis-Plus依賴 -->
 <dependency>
 <groupId>com.baomidou</groupId>
 <artifactId>mybatis-plus-boot-starter</artifactId>
 <version>3.4.0</version>
 </dependency>

 <!--添加代碼生成器依賴 -->
 <dependency>
 <groupId>com.baomidou</groupId>
 <artifactId>mybatis-plus-generator</artifactId>
 <version>3.4.0</version>
 </dependency>
 <!-- 模板引擎 -->
 <dependency>
 <groupId>org.apache.velocity</groupId>
 <artifactId>velocity-engine-core</artifactId>
 <version>2.0</version>
 </dependency>

  啟動(dòng)類中配置mapper掃描路徑

@SpringBootApplication
@MapperScan("cn.huanzi.qch.springbootmybatisplus.*.mapper")
public class SpringbootMybatisPlusApplication {

 public static void main(String[] args) {
 SpringApplication.run(SpringbootMybatisPlusApplication.class, args);
 }

}

  創(chuàng)建MybatisPlusConfig配置類

/**
 * MybatisPlusConfig配置類
 */
@Configuration
@ConditionalOnClass(value = {PaginationInterceptor.class})
public class MybatisPlusConfig {

 /**
 * 分頁插件相關(guān)
 */
 @Bean
 public PaginationInterceptor paginationInterceptor() {
 PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
 return paginationInterceptor;
 }

 /**
 * 主鍵策略相關(guān)
 */
 @Bean
 public IKeyGenerator keyGenerator() {
 return new H2KeyGenerator();
 }
}

  配置文件配置數(shù)據(jù)庫連接,與項(xiàng)目信息

server.port=10102
spring.application.name=springboot-mybatis-plus

#修改thymeleaf訪問根路徑
spring.thymeleaf.prefix=classpath:/view/

  yml

spring:
 datasource: #數(shù)據(jù)庫相關(guān)
 url: jdbc:mysql://localhost:3306/test&#63;serverTimezone=GMT%2B8&characterEncoding=utf-8
 username: root
 password: 123456
 driver-class-name: com.mysql.cj.jdbc.Driver
 mvc:
 date-format: yyyy-MM-dd HH:mm:ss #mvc接收參數(shù)時(shí)對(duì)日期進(jìn)行格式化

 jackson:
 date-format: yyyy-MM-dd HH:mm:ss #jackson對(duì)響應(yīng)回去的日期參數(shù)進(jìn)行格式化
 time-zone: GMT+8

  到這里項(xiàng)目簡單搭建完成

  通用代碼

  接下來就是通用代碼的編寫,我們參考之前jpa的代碼,結(jié)合Mybatis-Plus的Mapper接口進(jìn)行封裝通用get、save(插入/更新)、list、page、delete接口

  代碼布局與jpa的風(fēng)格一致

使用MyBatis Plus實(shí)現(xiàn)整合封裝——SpringBoot篇

  接口也一樣

使用MyBatis Plus實(shí)現(xiàn)整合封裝——SpringBoot篇

  代碼生成器

  MP原生的并不適合我們,我們要新建自定義模板,編寫代碼生成器

使用MyBatis Plus實(shí)現(xiàn)整合封裝——SpringBoot篇

使用MyBatis Plus實(shí)現(xiàn)整合封裝——SpringBoot篇

  運(yùn)行代碼生成器即可生成后端代碼,代碼風(fēng)格與我們之前的jpa高度一致,同樣是封裝一套通用CRUD、page分頁接口,單表繼承實(shí)現(xiàn)快速開發(fā)

使用MyBatis Plus實(shí)現(xiàn)整合封裝——SpringBoot篇

  接口效果演示

  get接口:http://localhost:10102/tbUser/get/2

使用MyBatis Plus實(shí)現(xiàn)整合封裝——SpringBoot篇

list接口:http://localhost:10102/tbUser/list、http://localhost:10102/tbUser/list&#63;id=2

使用MyBatis Plus實(shí)現(xiàn)整合封裝——SpringBoot篇

使用MyBatis Plus實(shí)現(xiàn)整合封裝——SpringBoot篇

page接口分頁、排序:http://localhost:10102/tbUser/page&#63;page=1&rows=3&sidx=id&sord=desc

使用MyBatis Plus實(shí)現(xiàn)整合封裝——SpringBoot篇

save有id,更新:http://localhost:10102/tbUser/save&#63;id=2&username=huanzixxxx

使用MyBatis Plus實(shí)現(xiàn)整合封裝——SpringBoot篇

save無id,新增:http://localhost:10102/tbUser/save&#63;username=huanziyyy&password=000000&created=2020-08-16%2019:56:04

使用MyBatis Plus實(shí)現(xiàn)整合封裝——SpringBoot篇

  delete刪除:http://localhost:10102/tbUser/delete/14

使用MyBatis Plus實(shí)現(xiàn)整合封裝——SpringBoot篇

  

關(guān)于使用MyBatis Plus實(shí)現(xiàn)整合封裝——SpringBoot篇就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI