Mybatiplus通用3.5.1版本及其以上的代码生成器工具类
Mybatiplus通用3.5.1版本及其以上的代码生成器工具类
package com.gton.util;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.querys.MySqlQuery;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine;
import com.baomidou.mybatisplus.generator.fill.Column;
import com.baomidou.mybatisplus.generator.fill.Property;
import com.baomidou.mybatisplus.generator.keywords.MySqlKeyWordsHandler;
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Properties;
/**
* @description: Mybatis-Plus3.5.1代码生成器
* @author: GuoTong
* @createTime: 2022-02-13 16:59
* @since JDK 1.8 OR 11
**/
@SuppressWarnings("unchecked")
public class AutoGeneratorTools {
/**
* Description:
* 使用前:
* 指定生成的模块名称:modelName
* 指定生成业务子包名称:mkdirFiile
*
* @author: GuoTong
* @date: 2022-10-22 22:03:01
*/
private static String modelName = "/commodity/";
// 按照业务划分不同多个子包
private static String mkdirFiile = "user";
private final String url = "jdbc:mysql://127.0.0.1:3306/study?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC";
// 高版本的DataSource不需要驱动 保留
private final String driver = "com.mysql.jdbc.Driver";
private final String username = "root";
private final String password = "root";
public static void main(String[] args) {
AutoGeneratorTools autoGeneratorTools = new AutoGeneratorTools();
String userDir = System.getProperty("user.dir");
String path = "src/main/java";
String parentPath = "com.gton." + mkdirFiile;
String mapperXmlPath = "/com/gton/" + mkdirFiile + "/mapper/xml";
List<String> tables = new ArrayList<>();
tables.add("product_buying");
autoGeneratorTools.getDataSourceByFile();
// 代码自动生成
autoCreateCode(autoGeneratorTools, userDir, path, parentPath, mapperXmlPath, tables);
}
private Properties getDataSourceByFile() {
FileInputStream dataSource = null;
InputStream dataSourceStream = null;
try {
String property = System.getProperty("user.dir");
dataSource = new FileInputStream(property + "/common/src/main/resources/application.properties");
dataSourceStream = new BufferedInputStream(dataSource);
Properties ps = new Properties();
ps.load(dataSourceStream);
return ps;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (dataSourceStream != null) {
try {
dataSourceStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (dataSource != null) {
try {
dataSource.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return new Properties();
}
private static void autoCreateCode(AutoGeneratorTools autoGeneratorTools, String userDir, String path, String parentPath, String mapperXmlPath, List<String> tables) {
FastAutoGenerator.create(autoGeneratorTools.getDataConfigBuilder()).
globalConfig(builder ->
builder.author("GuoTong").
commentDate("yyyy-MM-dd").
enableSwagger().
dateType(DateType.TIME_PACK).
disableOpenDir().
outputDir(userDir + modelName + path)).
packageConfig(builder ->
builder.parent(parentPath).
moduleName("").
entity("entity").
service("service").
serviceImpl("service.impl").
mapper("mapper").
xml("mapper.xml").
controller("controller").
pathInfo(Collections.singletonMap(OutputFile.mapperXml, userDir + modelName + path + mapperXmlPath))).
strategyConfig(builder ->
builder.addInclude(tables).
entityBuilder().
idType(IdType.ASSIGN_ID).
enableLombok().
enableTableFieldAnnotation().
enableChainModel().
logicDeleteColumnName("is_del").
logicDeletePropertyName("isDel").
addTableFills(new Column("create_time", FieldFill.INSERT)).
addTableFills(new Property("createTime", FieldFill.INSERT)).
addTableFills(new Column("update_time", FieldFill.INSERT_UPDATE)).
addTableFills(new Property("updateTime", FieldFill.INSERT_UPDATE)).
controllerBuilder().
formatFileName("%sController").
enableRestStyle().
serviceBuilder().
formatServiceFileName("%sService").
formatServiceImplFileName("%sServiceImpl").
mapperBuilder().
enableBaseColumnList().
enableMapperAnnotation().
enableBaseResultMap().
formatMapperFileName("%sMapper").
formatXmlFileName("%sMapper")).
templateEngine(new VelocityTemplateEngine()).execute();
}
private DataSourceConfig.Builder getDataConfigBuilder() {
Properties dataSourceByFile = getDataSourceByFile();
Object dbURl = dataSourceByFile.getOrDefault("spring.datasource.url", url);
Object dbUsername = dataSourceByFile.getOrDefault("spring.datasource.username", username);
Object dbPassword = dataSourceByFile.getOrDefault("spring.datasource.password", password);
return new DataSourceConfig.Builder(dbURl.toString(), dbUsername.toString(), dbPassword.toString()).
dbQuery(new MySqlQuery()).
keyWordsHandler(new MySqlKeyWordsHandler()).
schema("Mybatis-Plus").
typeConvert(new MySqlTypeConvert());
}
}
可以读取classpath下的数据源配置:application.properties
#数据源
spring.datasource.url=jdbc:mysql://130.33.238.27:3306/cloud_market?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=root
作者:隔壁老郭
个性签名:独学而无友,则孤陋而寡闻。做一个灵魂有趣的人!
如果觉得这篇文章对你有小小的帮助的话,记得在右下角点个“推荐”哦,博主在此感谢!
Java入门到入坟
万水千山总是情,打赏一分行不行,所以如果你心情还比较高兴,也是可以扫码打赏博主,哈哈哈(っ•̀ω•́)っ✎⁾⁾!