Springboot Mybatis Plus自動(dòng)生成工具類詳解代碼
前言
代碼生成器,也叫逆向工程,是根據(jù)數(shù)據(jù)庫(kù)里的表結(jié)構(gòu),自動(dòng)生成對(duì)應(yīng)的實(shí)體類、映射文件和接口。
看到很多小伙伴在為數(shù)據(jù)庫(kù)生成實(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("請(qǐng)輸入" + tip + ":");
System.out.println(help.toString());
if (scanner.hasNext()) {
String ipt = scanner.next();
if (StringUtils.isNotBlank(ipt)) {
return ipt;
}
}
throw new MybatisPlusException("請(qǐng)輸入正確的" + 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特性的請(qǐng)改為false
gc.setEnableCache(false);// XML 二級(jí)緩存
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é)尾
感謝大家的耐心閱讀,如有建議請(qǐng)私信或評(píng)論留言。如有收獲,勞煩支持,關(guān)注、點(diǎn)贊、評(píng)論、收藏均可,博主會(huì)經(jīng)常更新,與大家共同進(jìn)步
到此這篇關(guān)于Springboot Mybatis Plus自動(dòng)生成工具類詳解代碼的文章就介紹到這了,更多相關(guān)Springboot Mybatis Plus 生成工具類內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(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ù)庫(kù)文檔自動(dòng)生成的示例代碼
- springboot 通過(guò)代碼自動(dòng)生成pid的方法
- SpringBoot+MyBatis-Plus+Velocity實(shí)現(xiàn)代碼自動(dòng)生成
相關(guān)文章
Java集合框架之Stack Queue Deque使用詳解刨析
早在 Java 2 中之前,Java 就提供了特設(shè)類。比如:Dictionary, Vector, Stack, 和 Properties 這些類用來(lái)存儲(chǔ)和操作對(duì)象組。雖然這些類都非常有用,但是它們?nèi)鄙僖粋€(gè)核心的,統(tǒng)一的主題。由于這個(gè)原因,使用 Vector 類的方式和使用 Properties 類的方式有著很大不同2021-10-10
Idea Jrebel 報(bào)錯(cuò):Cannot reactivate,offline 
本文主要介紹了Idea Jrebel 報(bào)錯(cuò):Cannot reactivate,offline seat in use,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06
Java8?Stream?collect(Collectors.toMap())的使用
這篇文章主要介紹了Java8?Stream?collect(Collectors.toMap())的使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05
IDEA常量字符串過(guò)長(zhǎng)問(wèn)題及解決方案
在編譯Java項(xiàng)目時(shí)遇到“常量字符串過(guò)長(zhǎng)”錯(cuò)誤,可以通過(guò)修改編譯器設(shè)置解決,具體方法是進(jìn)入IDE的設(shè)置(File>>Settings>>Build, Execution, Deployment>>Compiler>>Java Compiler),將使用的編譯器更改為Eclipse,如果問(wèn)題依舊2024-10-10
一文帶你了解Java創(chuàng)建型設(shè)計(jì)模式之原型模式
原型模式其實(shí)就是從一個(gè)對(duì)象在創(chuàng)建另外一個(gè)可定制的對(duì)象,不需要知道任何創(chuàng)建的細(xì)節(jié)。本文就來(lái)通過(guò)示例為大家詳細(xì)聊聊原型模式,需要的可以參考一下2022-09-09
IntelliJ IDEA中折疊所有Java代碼,再也不怕大段的代碼了
今天小編就為大家分享一篇關(guān)于IntelliJ IDEA中折疊所有Java代碼,再也不怕大段的代碼了,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-10-10

