SpringBoot如何使用MyBatisPlus逆向工程自動生成代碼
SpringBoot + MyBatis-Plus逆向工程自動生成代碼 Swagger3.0 訪問
廢話不多說,直接see code
1. 新建SpringBoot項目

我這里是SpringBoot 2.7.1版本
2. 導(dǎo)入相關(guān)依賴
<!-- Mybatis-plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.1</version>
</dependency>
<!-- 代碼生成器(新版本) -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.0</version>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- swagger3 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
<!-- 測試 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
- 大家要注意 swagger 版本和 springboot 版本有沖突,大坑
- 我這里 swagger3 + springboot 2.7.1 是沒問題的
3. 配置文件application.yml
server:
port: 8081
spring:
mvc:
pathmatch:
# 因為Springfox使用的路徑匹配是基于AntPathMatcher的,而SpringBoot2.6.X以上使用的是PathPatternMatcher
matching-strategy: ant_path_matcher
mybatis-plus:
mapper-locations: classpath:mapper/*.xml
# 日志打印SQL開啟,便于查找問題
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
auto-mapping-behavior: full #自動填充
matching-strategy: ant_path_matcher
因為Springfox使用的路徑匹配是基于AntPathMatcher的,而SpringBoot2.6.X以上使用的是PathPatternMatcher
不配置訪問不到(無法訪問此網(wǎng)站)
4. 新建測試類(重點)
package com.soul.zzq;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.fill.Column;
import com.baomidou.mybatisplus.generator.fill.Property;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.Arrays;
import java.util.Collections;
/**
* mybatisPlus逆向工程(懂得都懂)
* @Author zhouzhongqing
* @Date 2021/10/31 17:31
* @Version 1.0
**/
@SpringBootTest
public class MybatisAutoGeneratorCodeTest {
/**
* 只需改動以下,其他基本不動
* create 數(shù)據(jù)庫連接
* outputDir 指定輸出目錄 (這個一般都是固定的)
* parent 設(shè)置父包名 (com/cn.xxx.xxx)
* moduleName 設(shè)置父包模塊名 (設(shè)置這個就會把entity、service、mapper、controller都放在一個單獨文件中,
* 例如:
* {
* user - entity
* service
* mapper
* controller
* }
*
* )
* pathInfo 設(shè)置mapperXml生成路徑 (這個一般都是固定的)
* addInclude 設(shè)置需要生成的表名 (可以傳數(shù)組多個,也可以單個)
* addTablePrefix 設(shè)置過濾表前綴(可選修改,也可以注釋,根據(jù)需求)
*
* @Author: zhouStart
* @Date 2023/12/10 13:36
*/
@Test
public void generator() {
// FastAutoGenerator:快速自動生成,采用建造者設(shè)計模式
FastAutoGenerator.create("jdbc:mysql://localhost:3306/soul",
"root", "123456")
.globalConfig(builder -> {
builder.author("zhouStart") // 設(shè)置作者
.enableSwagger() // 開啟 swagger 模式
// .fileOverride() // 覆蓋已生成文件,默認值:false
.disableOpenDir() //禁止打開輸出目錄,默認值:true
.dateType(DateType.ONLY_DATE) //定義生成的實體類中日期類型 時間策略 DateType.ONLY_DATE 默認值: DateType.TIME_PACK
.outputDir("D:\\project\\soul-docker" + "/src/main/java"); // 指定輸出目錄
})
.packageConfig(builder -> {
builder.parent("com.soul.zzq") // 設(shè)置父包名
// .moduleName("") // 設(shè)置父包模塊名
.pathInfo(Collections.singletonMap(OutputFile.mapperXml, "D:\\project\\soul-docker" + "/src/main/resources/mapper")); // 設(shè)置mapperXml生成路徑
})
.strategyConfig(builder -> {
builder.addInclude(Arrays.asList("user","product")); // 設(shè)置需要生成的表名
// .addTablePrefix("soul"); // 設(shè)置過濾表前綴
builder.entityBuilder()
.naming(NamingStrategy.underline_to_camel)//數(shù)據(jù)庫表映射到實體的命名策略.默認下劃線轉(zhuǎn)駝峰命名
.columnNaming(NamingStrategy.underline_to_camel)//數(shù)據(jù)庫表字段映射到實體的命名策略,默認為 null,未指定按照 naming 執(zhí)行
.addTableFills(new Column("createTime", FieldFill.INSERT))
.addTableFills(new Property("updateTime", FieldFill.INSERT_UPDATE))
.idType(IdType.AUTO) //主鍵策略
.enableLombok();//開啟 lombok 模型,默認值:false
builder.controllerBuilder()
.enableHyphenStyle()//開啟駝峰轉(zhuǎn)連字符,默認值:false
.enableRestStyle();//開啟生成@RestController 控制器,默認值:false
builder.serviceBuilder()
.formatServiceImplFileName("%sServiceImp")//格式化 service 實現(xiàn)類文件名稱
.formatServiceFileName("%sService");//格式化 service 接口文件名稱,去掉Service接口的首字母I
})
.strategyConfig(builder -> {
})
// .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板,默認的是Velocity引擎模板
.execute();
}
}
運行之前確認好數(shù)據(jù)庫表是否已經(jīng)創(chuàng)建好了
5. 創(chuàng)建Swagger配置類
package com.soul.zzq.config;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* @Description
* @Author zhouzhongqing
* @Date 2023/12/10 14:37
* @Version 1.0
**/
@Configuration
@EnableOpenApi
public class SwaggerConfig {
/**
* 配置文檔生成最佳實踐
*
* @return
*/
@Bean
public Docket docket() {
return new Docket(DocumentationType.OAS_30).pathMapping("/")
// 是否啟用Swagger
.enable(true)
// 用來創(chuàng)建該API的基本信息,展示在文檔的頁面中(自定義展示的信息)
.apiInfo(apiInfo())
.groupName("管理員")
// 設(shè)置哪些接口暴露給Swagger展示
.select()
// 掃描所有有注解的api,用這種方式更靈活
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
// 掃描指定包中的swagger注解
.apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
.apis(RequestHandlerSelectors.basePackage("com.soul.zzq.controller"))
// 掃描所有 .apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
/**
* 配置基本信息
*
* @return
*/
@Bean
public ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Swagger3")
.description("swagger3")
.version("v1.0")
.termsOfServiceUrl("https://swagger.io/")
.contact(new Contact("zhouStart", "https://swagger.io/", "zhouStart@163.com"))
.build();
}
}
6. 啟動項目
瀏覽器訪問Swagger http://localhost:8081/swagger-ui/#/

完美
7. 完整pom.xml 和 項目文件
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.soul</groupId>
<artifactId>soul-docker</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Mybatis-plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.1</version>
</dependency>
<!-- 代碼生成器(新版本) -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.0</version>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- swagger3 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
<!-- 測試 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>soul</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
- springboot3.0整合mybatis-flex實現(xiàn)逆向工程的示例代碼
- SpringBoot整合MybatisPlusGernerator實現(xiàn)逆向工程
- Spring Boot整合MybatisPlus逆向工程(MySQL/PostgreSQL)
- springboot整合mybatis-plus逆向工程的實現(xiàn)
- mybatis逆向工程與分頁在springboot中的應(yīng)用及遇到坑
- SpringBoot整合MyBatis逆向工程及 MyBatis通用Mapper實例詳解
- 淺析Spring和MyBatis整合及逆向工程
- spring和Mybatis逆向工程的實現(xiàn)
相關(guān)文章
MyBatis傳入List集合查詢數(shù)據(jù)問題
這篇文章主要介紹了MyBatis傳入List集合查詢數(shù)據(jù)問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02
JAVA中關(guān)于Long類型返回前端精度丟失問題處理辦法
這篇文章主要介紹了后端JavaBean的id屬性從Long類型改為雪花算法后出現(xiàn)的精度丟失問題,解決方案包括將id字段類型改為字符串或使用Jackson序列化方式,需要的朋友可以參考下2024-11-11
代理模式:JAVA靜態(tài)代理和動態(tài)代理的實例和實現(xiàn)詳解
這篇文章主要給大家介紹了關(guān)于Java靜態(tài)代理和動態(tài)代理的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-08-08
SpringBoot集成Flink-CDC實現(xiàn)對數(shù)據(jù)庫數(shù)據(jù)的監(jiān)聽問題
Flink CDC(Flink Change Data Capture)是一種基于數(shù)據(jù)庫日志的CDC技術(shù),它實現(xiàn)了一個全增量一體化的數(shù)據(jù)集成框架,這篇文章主要介紹了SpringBoot集成Flink-CDC,實現(xiàn)對數(shù)據(jù)庫數(shù)據(jù)的監(jiān)聽,需要的朋友可以參考下2024-07-07
Java關(guān)鍵字instanceof的兩種用法實例
這篇文章主要介紹了Java關(guān)鍵字instanceof的兩種用法實例,本文給出了instanceof關(guān)鍵字用于判斷一個引用類型變量所指向的對象是否是一個類(或接口、抽象類、父類)及用于數(shù)組比較,需要的朋友可以參考下2015-03-03

