SpringBoot項(xiàng)目使用mybatis-plus逆向自動生成全套代碼
1.在你的SpringBoot項(xiàng)目下新建子模塊項(xiàng)目
pom.xml添加以下依賴:
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.3.2</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-extension</artifactId>
<version>3.3.2</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
<version>2.3.1.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
ps:名稱隨意,最好帶上generator 來辨別這是代碼自動生成模塊

2.在此模塊下新建一個(gè)包與一個(gè)java類 類名: CodeGenerator

完整代碼如下:
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
* @Description: 代碼生成類
*/
public class CodeGenerator {
//數(shù)據(jù)庫連接參數(shù)
public static String driver = "com.mysql.cj.jdbc.Driver";
public static String url = "jdbc:mysql://localhost:3306/rht_test?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true";
public static String username="root";
public static String password="123456";
//父級別包名稱
public static String parentPackage = "cn.rht";
//代碼生成的目標(biāo)路徑
public static String generateTo = "/rht-generator/src/main/java";
//mapper.xml的生成路徑
public static String mapperXmlPath = "/rht-generator/src/main/resources/mapper";
//控制器的公共基類,用于抽象控制器的公共方法,null值表示沒有父類
public static String baseControllerClassName ;
//業(yè)務(wù)層的公共基類,用于抽象公共方法
public static String baseServiceClassName ;
//作者名
public static String author = "rht.cn";
//模塊名稱,用于組成包名
public static String modelName = "portal";
//Mapper接口的模板文件,不用寫后綴 .ftl
public static String mapperTempalte = "/ftl/mapper.java";
/**
* <p>
* 讀取控制臺內(nèi)容
* </p>
*/
public static String scanner(String tip) {
Scanner scanner = new Scanner(System.in);
StringBuilder help = new StringBuilder();
help.append("請輸入" + tip + ":");
System.out.println(help.toString());
if (scanner.hasNext()) {
String ipt = scanner.next();
if (StringUtils.isNotEmpty(ipt)) {
return ipt;
}
}
throw new MybatisPlusException("請輸入正確的" + tip + "!");
}
/**
* RUN THIS
*/
public static void main(String[] args) {
// 代碼生成器
AutoGenerator mpg = new AutoGenerator();
// 全局配置
GlobalConfig gc = new GlobalConfig();
String projectPath = System.getProperty("user.dir");
gc.setOutputDir(projectPath + generateTo);
gc.setAuthor(author);
gc.setOpen(false);
//設(shè)置時(shí)間類型為Date
gc.setDateType(DateType.TIME_PACK);
//開啟swagger
//gc.setSwagger2(true);
//設(shè)置mapper.xml的resultMap
gc.setBaseResultMap(true);
mpg.setGlobalConfig(gc);
// 數(shù)據(jù)源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl(url);
// dsc.setSchemaName("public");
dsc.setDriverName(driver);
dsc.setUsername(username);
dsc.setPassword(password);
mpg.setDataSource(dsc);
// 包配置
PackageConfig pc = new PackageConfig();
pc.setEntity("model");
//pc.setModuleName(scanner("模塊名"));
pc.setModuleName(modelName);
pc.setParent(parentPackage);
mpg.setPackageInfo(pc);
// 自定義配置
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {
// to do nothing
}
};
List<FileOutConfig> focList = new ArrayList<>();
focList.add(new FileOutConfig("/templates/mapper.xml.ftl") {
@Override
public String outputFile(TableInfo tableInfo) {
// 自定義輸入文件名稱
return projectPath + mapperXmlPath
+ "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
}
});
cfg.setFileOutConfigList(focList);
mpg.setCfg(cfg);
mpg.setTemplate(new TemplateConfig().setXml(null));
mpg.setTemplate(new TemplateConfig().setMapper(mapperTempalte));
// 策略配置
StrategyConfig strategy = new StrategyConfig();
strategy.setNaming(NamingStrategy.underline_to_camel);
//字段駝峰命名
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
//設(shè)置實(shí)體類的lombok
strategy.setEntityLombokModel(true);
//設(shè)置controller的父類
if (baseControllerClassName!=null) strategy.setSuperControllerClass(baseControllerClassName);
//設(shè)置服務(wù)類的父類
if (baseServiceClassName !=null ) strategy.setSuperServiceImplClass(baseServiceClassName);
// strategy.
//設(shè)置實(shí)體類屬性對應(yīng)表字段的注解
strategy.setEntityTableFieldAnnotationEnable(true);
//設(shè)置表名
String tableName = scanner("表名, all全部表");
if(! "all".equalsIgnoreCase(tableName)){
strategy.setInclude(tableName);
}
strategy.setTablePrefix(pc.getModuleName() + "_");
strategy.setRestControllerStyle(true);
mpg.setStrategy(strategy);
// 選擇 freemarker 引擎需要指定如下加,注意 pom 依賴必須有!
mpg.setTemplateEngine(new FreemarkerTemplateEngine());
mpg.execute();
}
}
3.在 resources 下新建 文件夾,用來存放 mapper文件
新建模板文件: mapper.java.ftl


模板完整代碼如下:
package ${package.Mapper};
import ${package.Entity}.${entity};
import ${superMapperClassPackage};
import org.springframework.stereotype.Repository;
/**
* <p>
* ${table.comment!} Mapper 接口
* </p>
*
* @author ${author}
* @since ${date}
*/
<#if kotlin>
interface ${table.mapperName} : ${superMapperClass}<${entity}>
<#else>
@Repository
public interface ${table.mapperName} extends ${superMapperClass}<${entity}> {
}
</#if>
4.配置CodeGenerator類
ps:請根據(jù)自己實(shí)際路徑配置

5.啟動代碼生成類main方法
ps:輸入all 將會自動生成配置數(shù)據(jù)庫下的所有配置文件,或者直接輸入單表名稱生成某一個(gè)表的Controller,mapper,service,model層與mapper.xml文件

下面是我輸入表名為:user,自動生成的部分文件信息展示

User實(shí)體類

UserMapper.xml文件

如果你有很多表要生成時(shí),但又不想全部生成時(shí),可以在CodeGenerator類代碼中134行代碼
//設(shè)置表名
String tableName = scanner("表名, all全部表");
if(! "all".equalsIgnoreCase(tableName)){
strategy.setInclude(tableName);
}
替換為:
String[] tableNames = {"user","dept"};//數(shù)據(jù)庫表名的集合
for (int i = 0; i <tableNames.length ; i++) {
strategy.setInclude(tableNames);
}
來生成自己想要生成的文件
6.刪除文件
最后:也是重要的一點(diǎn),在您將這些文件復(fù)制到了項(xiàng)目模塊上的時(shí)候,留下CodeGenerator類與文件夾下的mapper.java.ftl配置,其他生成的請及時(shí)刪除
至于原因是將來業(yè)務(wù)拓展后,數(shù)據(jù)庫新增表后,只要新創(chuàng)建表的文件,如果不刪除以前生成過的文件,到時(shí)候找起來比較麻煩,沒必要給自己添這層麻煩

到此這篇關(guān)于SpringBoot項(xiàng)目使用mybatis-plus逆向自動生成全套代碼的文章就介紹到這了,更多相關(guān)mybatis-plus逆向自動生成代碼內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot如何使用MyBatisPlus逆向工程自動生成代碼
- SpringBoot集成Mybatis-plus并實(shí)現(xiàn)自動生成相關(guān)文件的示例代碼
- Springboot Mybatis Plus自動生成工具類詳解代碼
- SpringBoot整合Mybatis Generator自動生成代碼
- SpringBoot根據(jù)目錄結(jié)構(gòu)自動生成路由前綴的實(shí)現(xiàn)代碼
- springboot整合freemarker代碼自動生成器
- SpringBoot整合screw實(shí)現(xiàn)數(shù)據(jù)庫文檔自動生成的示例代碼
- springboot 通過代碼自動生成pid的方法
- SpringBoot+MyBatis-Plus+Velocity實(shí)現(xiàn)代碼自動生成
相關(guān)文章
RocketMQ獲取指定消息的實(shí)現(xiàn)方法(源碼)
這篇文章主要給大家介紹了關(guān)于RocketMQ獲取指定消息的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用RocketMQ具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
SpringBoot項(xiàng)目中使用Sharding-JDBC實(shí)現(xiàn)讀寫分離的詳細(xì)步驟
Sharding-JDBC是一個(gè)分布式數(shù)據(jù)庫中間件,它不僅支持?jǐn)?shù)據(jù)分片,還可以輕松實(shí)現(xiàn)數(shù)據(jù)庫的讀寫分離,本文介紹如何在Spring Boot項(xiàng)目中集成Sharding-JDBC并實(shí)現(xiàn)讀寫分離的詳細(xì)步驟,需要的朋友可以參考下2024-08-08
Java實(shí)現(xiàn)一個(gè)簡易版的多級菜單功能
這篇文章主要給大家介紹了關(guān)于Java如何實(shí)現(xiàn)一個(gè)簡易版的多級菜單功能的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-01-01
使用Spring MVC攔截器實(shí)現(xiàn)日志記錄的方法
本篇文章主要介紹了使用Spring MVC攔截器實(shí)現(xiàn)日志記錄的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-04-04
Java?AQS?原理與?ReentrantLock?實(shí)現(xiàn)方法
AQS 的作用是解決同步器的實(shí)現(xiàn)問題,它將復(fù)雜的同步器實(shí)現(xiàn)分解為簡單的框架方法,開發(fā)者只需要實(shí)現(xiàn)少量特定的方法就能快速構(gòu)建出可靠的同步器,這篇文章主要介紹Java AQS原理與ReentrantLock實(shí)現(xiàn),需要的朋友可以參考下2025-03-03
mybatis的mapper特殊字符轉(zhuǎn)移及動態(tài)SQL條件查詢小結(jié)
mybatis mapper文件中條件查詢符,如>=,<,之類是不能直接寫的會報(bào)錯(cuò)的需要轉(zhuǎn)移一下,本文給大家介紹了常見的條件查詢操作,對mybatis的mapper特殊字符及動態(tài)SQL條件查詢相關(guān)知識感興趣的朋友一起看看吧2021-09-09
java面試JDK8?new?ReentrantLock()加鎖流程解析
這篇文章主要為大家介紹了java面試JDK8?new?ReentrantLock()加鎖流程解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07

