Springboot Mybatis Plus自動(dòng)生成工具類詳解代碼
前言
代碼生成器,也叫逆向工程,是根據(jù)數(shù)據(jù)庫里的表結(jié)構(gòu),自動(dòng)生成對應(yīng)的實(shí)體類、映射文件和接口。
看到很多小伙伴在為數(shù)據(jù)庫生成實(shí)體類發(fā)愁,現(xiàn)分享給大家,提高開發(fā)效率。
一、pom依賴
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.30</version>
</dependency>
二、工具類
package com.his.utils;
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.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
* Mybatis plus代碼自動(dòng)生成
*/
public class MybatisPlusUtil {
/** 作者 */
public static final String AUTHOR = "dd";
/** 類命名 */
/**
* Entity命名
*/
public static final String FILE_NAME_ENTITY = "%sEntity";
/**
* MAPPER命名
*/
public static final String FILE_NAME_MAPPER = "%sMapper";
/**
* xml命名
*/
public static final String FILE_NAME_XML = "%sMapper";
/**
* Service命名
*/
public static final String FILE_NAME_SERVICE = "%sService";
/**
* ServiceImpl命名
*/
public static final String FILE_NAME_SERVICE_IMPL = "%sDO";
/**
* Controller命名
*/
public static final String FILE_NAME_CONTROLLER = "%sController";
/**
包命名,可以根據(jù)自己的項(xiàng)目情況自定義生成后的存放路徑
entity默認(rèn)路徑為父目錄.entity
mapper默認(rèn)路徑為父目錄.mapper
service默認(rèn)路徑為父目錄.service
serviceImpl默認(rèn)路徑為父目錄.service.impl
controller默認(rèn)路徑為父目錄.controller
*/
/**
* PARENT命名
*/
public static final String PACKAGE_NAME_PARENT = "com.his";
/**
* Entity命名
*/
public static final String PACKAGE_NAME_ENTITY = "repository.entity.control";
/**
* MAPPER命名
*/
public static final String PACKAGE_NAME_MAPPER = "repository.mapper.control";
/**
* xml命名
*/
public static final String PACKAGE_NAME_XML = "sys";
/**
* Service命名
*/
public static final String PACKAGE_NAME_SERVICE = "domain.control";
/**
* ServiceImpl命名
*/
public static final String PACKAGE_NAME_SERVICE_IMPL = "domain.control";
/**
* Controller命名
*/
public static final String PACKAGE_NAME_CONTROLLER = "facade.controller.control";
/**
* 讀取控制臺(tái)內(nèi)容
*/
private 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.isNotBlank(ipt)) {
return ipt;
}
}
throw new MybatisPlusException("請輸入正確的" + tip + "!");
}
/**
* 運(yùn)行這個(gè)main方法進(jìn)行代碼生成
*/
public static void main(String[] args) {
// 代碼生成器
AutoGenerator mpg = new AutoGenerator();
// 全局配置
GlobalConfig gc = new GlobalConfig();
String projectPath = System.getProperty("user.dir");
gc.setOutputDir(projectPath + "/src/main/java");
gc.setFileOverride(true);
gc.setAuthor(AUTHOR);
gc.setOpen(false);
gc.setActiveRecord(false);// 不需要ActiveRecord特性的請改為false
gc.setEnableCache(false);// XML 二級緩存
gc.setSwagger2(true); // 實(shí)體屬性 Swagger2 注解
gc.setBaseResultMap(true);
gc.setBaseColumnList(true);
gc.setEntityName(FILE_NAME_ENTITY);
gc.setMapperName(FILE_NAME_MAPPER);
gc.setXmlName(FILE_NAME_XML);
gc.setServiceName(FILE_NAME_SERVICE);
gc.setServiceImplName(FILE_NAME_SERVICE_IMPL);
gc.setControllerName(FILE_NAME_CONTROLLER);
mpg.setGlobalConfig(gc);
// 數(shù)據(jù)源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:oracle:thin:@ip:port/test");
dsc.setDriverName("oracle.jdbc.OracleDriver");
dsc.setUsername("user");
dsc.setPassword("pass");
mpg.setDataSource(dsc);
//包配置
PackageConfig pc = new PackageConfig();
pc.setModuleName(null);
pc.setParent(PACKAGE_NAME_PARENT);
pc.setController(PACKAGE_NAME_CONTROLLER);
pc.setService(PACKAGE_NAME_SERVICE);
pc.setServiceImpl(PACKAGE_NAME_SERVICE_IMPL);
pc.setMapper(PACKAGE_NAME_MAPPER);
pc.setEntity(PACKAGE_NAME_ENTITY);
pc.setXml(PACKAGE_NAME_XML);
mpg.setPackageInfo(pc);
// 策略配置
StrategyConfig strategy = new StrategyConfig();
strategy.setNaming(NamingStrategy.underline_to_camel);
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
//strategy.setEntityLombokModel(true);
strategy.setRestControllerStyle(true);
strategy.setInclude(scanner("表名,多個(gè)英文逗號(hào)分割").split(","));
strategy.setControllerMappingHyphenStyle(true);
// 設(shè)置表前綴
strategy.setTablePrefix("IEMR_");
mpg.setStrategy(strategy);
mpg.setTemplateEngine(new FreemarkerTemplateEngine());
// 自定義配置
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {
// to do nothing
}
};
// 如果模板引擎是 freemarker
String templatePath = "/templates/mapper.xml.ftl";
// 如果模板引擎是 velocity
// String templatePath = "/templates/mapper.xml.vm";
// 自定義輸出配置
List<FileOutConfig> focList = new ArrayList<>();
// 自定義配置會(huì)被優(yōu)先輸出
focList.add(new FileOutConfig(templatePath) {
@Override
public String outputFile(TableInfo tableInfo) {
// 自定義輸出文件名 , 如果你 Entity 設(shè)置了前后綴、此處注意 xml 的名稱會(huì)跟著發(fā)生變化??!
return projectPath + "/src/main/resources/mapper/"
+ "/" + tableInfo.getMapperName() + StringPool.DOT_XML;
}
});
cfg.setFileOutConfigList(focList);
mpg.setCfg(cfg);
// 配置模板
TemplateConfig templateConfig = new TemplateConfig();
templateConfig.setXml(null);
mpg.setTemplate(templateConfig);
mpg.execute();
}
}
結(jié)尾
感謝大家的耐心閱讀,如有建議請私信或評論留言。如有收獲,勞煩支持,關(guān)注、點(diǎn)贊、評論、收藏均可,博主會(huì)經(jīng)常更新,與大家共同進(jìn)步
到此這篇關(guān)于Springboot Mybatis Plus自動(dòng)生成工具類詳解代碼的文章就介紹到這了,更多相關(guān)Springboot Mybatis Plus 生成工具類內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot如何使用MyBatisPlus逆向工程自動(dòng)生成代碼
- SpringBoot集成Mybatis-plus并實(shí)現(xiàn)自動(dòng)生成相關(guān)文件的示例代碼
- SpringBoot項(xiàng)目使用mybatis-plus逆向自動(dòng)生成全套代碼
- SpringBoot整合Mybatis Generator自動(dòng)生成代碼
- SpringBoot根據(jù)目錄結(jié)構(gòu)自動(dòng)生成路由前綴的實(shí)現(xiàn)代碼
- springboot整合freemarker代碼自動(dòng)生成器
- SpringBoot整合screw實(shí)現(xiàn)數(shù)據(jù)庫文檔自動(dòng)生成的示例代碼
- springboot 通過代碼自動(dòng)生成pid的方法
- SpringBoot+MyBatis-Plus+Velocity實(shí)現(xiàn)代碼自動(dòng)生成
相關(guān)文章
Java并發(fā)編程之阻塞隊(duì)列(BlockingQueue)詳解
這篇文章主要介紹了詳解Java阻塞隊(duì)列(BlockingQueue)的實(shí)現(xiàn)原理,阻塞隊(duì)列是Java util.concurrent包下重要的數(shù)據(jù)結(jié)構(gòu),有興趣的可以了解一下2021-09-09
Druid連接池的自定義過濾功能實(shí)現(xiàn)方法
在數(shù)據(jù)密集型應(yīng)用中,監(jiān)控和分析數(shù)據(jù)庫操作對于確保性能和穩(wěn)定性至關(guān)重要,本文將探討如何實(shí)現(xiàn)一個(gè)自定義的Druid過濾器來捕獲數(shù)據(jù)庫請求并進(jìn)行日志記錄,以輔助開發(fā)和維護(hù)工作,需要的朋友可以參考下2023-11-11
java Socket編程實(shí)現(xiàn)I/O多路復(fù)用的示例
本文主要介紹了java Socket編程實(shí)現(xiàn)I/O多路復(fù)用的示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-09-09
基于SpringBoot上傳任意文件功能的實(shí)現(xiàn)
下面小編就為大家?guī)硪黄赟pringBoot上傳任意文件功能的實(shí)現(xiàn)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-08-08
Java中短路運(yùn)算符與邏輯運(yùn)算符示例詳解
這篇文章主要給大家介紹了關(guān)于Java中短路運(yùn)算符與邏輯運(yùn)算符的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
Java調(diào)用DeepSeek?API的最佳實(shí)踐及詳細(xì)代碼示例
這篇文章主要介紹了如何使用Java調(diào)用DeepSeek?API,包括獲取API密鑰、添加HTTP客戶端依賴、創(chuàng)建HTTP請求、處理響應(yīng)、錯(cuò)誤處理、測試和部署,文章還提供了代碼示例和注意事項(xiàng),幫助開發(fā)者順利完成API調(diào)用,需要的朋友可以參考下2025-02-02
Java工具jsch.jar實(shí)現(xiàn)上傳下載
這篇文章主要為大家詳細(xì)介紹了Java操作ftp的一款工具,利用jsch.jar針對sftp的上傳下載工具類,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12
SpringBoot使用Maven打包異常-引入外部jar的問題及解決方案
這篇文章主要介紹了SpringBoot使用Maven打包異常-引入外部jar,需要的朋友可以參考下2020-06-06

