add utils
This commit is contained in:
parent
9f6876f1cd
commit
e10fa83f73
@ -21,8 +21,12 @@ dependencies {
|
||||
implementation 'com.baomidou:mybatis-plus-boot-starter:3.4.2'
|
||||
implementation 'com.baomidou:mybatis-plus-generator:3.4.1'
|
||||
implementation 'org.freemarker:freemarker:2.3.31'
|
||||
implementation 'org.springframework.boot:spring-boot-configuration-processor'
|
||||
implementation 'org.springframework.boot:spring-boot-configuration-processor'
|
||||
implementation 'org.postgresql:postgresql:42.2.19'
|
||||
implementation 'org.springframework.boot:spring-boot-configuration-processor:2.1.4.RELEASE'
|
||||
// https://mvnrepository.com/artifact/org.projectlombok/lombok
|
||||
implementation'org.projectlombok:lombok:1.18.20'
|
||||
|
||||
}
|
||||
|
||||
test {
|
||||
|
||||
@ -1,13 +0,0 @@
|
||||
package cn.rainss.codegenerator;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class CodeGeneratorApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(CodeGeneratorApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
7
src/main/java/cn/rainss/codegenerator/DDDGenerator.java
Normal file
7
src/main/java/cn/rainss/codegenerator/DDDGenerator.java
Normal file
@ -0,0 +1,7 @@
|
||||
package cn.rainss.codegenerator;
|
||||
|
||||
public class DDDGenerator {
|
||||
public static void main(String[] args) {
|
||||
|
||||
}
|
||||
}
|
||||
@ -1,139 +0,0 @@
|
||||
package cn.rainss.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.NamingStrategy;
|
||||
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class Main {
|
||||
/**
|
||||
* <p>
|
||||
* 读取控制台内容
|
||||
* </p>
|
||||
*/
|
||||
public static String scanner(String tip) {
|
||||
System.out.printf("当前路径"+System.getProperty("user.dir"));
|
||||
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 + "!");
|
||||
}
|
||||
|
||||
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.setAuthor("jobob");
|
||||
gc.setOpen(false);
|
||||
// gc.setSwagger2(true); 实体属性 Swagger2 注解
|
||||
mpg.setGlobalConfig(gc);
|
||||
|
||||
// 数据源配置
|
||||
DataSourceConfig dsc = new DataSourceConfig();
|
||||
dsc.setUrl("jdbc:postgresql://db.timemail.email:5432/timemail");
|
||||
// dsc.setSchemaName("public");
|
||||
dsc.setDriverName("org.postgresql.Driver");
|
||||
dsc.setUsername("postgres");
|
||||
dsc.setPassword("timemail.email");
|
||||
mpg.setDataSource(dsc);
|
||||
|
||||
// 包配置
|
||||
PackageConfig pc = new PackageConfig();
|
||||
pc.setModuleName(scanner("模块名"));
|
||||
pc.setParent("com.baomidou.ant");
|
||||
mpg.setPackageInfo(pc);
|
||||
|
||||
// 自定义配置
|
||||
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<>();
|
||||
// 自定义配置会被优先输出
|
||||
focList.add(new FileOutConfig(templatePath) {
|
||||
@Override
|
||||
public String outputFile(TableInfo tableInfo) {
|
||||
// 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
|
||||
return projectPath + "/src/main/resources/mapper/" + pc.getModuleName()
|
||||
+ "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
|
||||
}
|
||||
});
|
||||
/*
|
||||
cfg.setFileCreate(new IFileCreate() {
|
||||
@Override
|
||||
public boolean isCreate(ConfigBuilder configBuilder, FileType fileType, String filePath) {
|
||||
// 判断自定义文件夹是否需要创建
|
||||
checkDir("调用默认方法创建的目录,自定义目录用");
|
||||
if (fileType == FileType.MAPPER) {
|
||||
// 已经生成 mapper 文件判断存在,不想重新生成返回 false
|
||||
return !new File(filePath).exists();
|
||||
}
|
||||
// 允许生成模板文件
|
||||
return true;
|
||||
}
|
||||
});
|
||||
*/
|
||||
cfg.setFileOutConfigList(focList);
|
||||
mpg.setCfg(cfg);
|
||||
|
||||
// 配置模板
|
||||
TemplateConfig templateConfig = new TemplateConfig();
|
||||
|
||||
// 配置自定义输出模板
|
||||
//指定自定义模板路径,注意不要带上.ftl/.vm, 会根据使用的模板引擎自动识别
|
||||
// templateConfig.setEntity("templates/entity2.java");
|
||||
// templateConfig.setService();
|
||||
// templateConfig.setController();
|
||||
|
||||
templateConfig.setXml(null);
|
||||
mpg.setTemplate(templateConfig);
|
||||
|
||||
// 策略配置
|
||||
StrategyConfig strategy = new StrategyConfig();
|
||||
strategy.setNaming(NamingStrategy.underline_to_camel);
|
||||
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
|
||||
strategy.setSuperEntityClass("你自己的父类实体,没有就不用设置!");
|
||||
strategy.setEntityLombokModel(true);
|
||||
strategy.setRestControllerStyle(true);
|
||||
// 公共父类
|
||||
strategy.setSuperControllerClass("你自己的父类控制器,没有就不用设置!");
|
||||
// 写于父类中的公共字段
|
||||
strategy.setSuperEntityColumns("id");
|
||||
strategy.setTablePrefix("tm_");
|
||||
strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));
|
||||
strategy.setControllerMappingHyphenStyle(true);
|
||||
strategy.setTablePrefix(pc.getModuleName() + "_");
|
||||
mpg.setStrategy(strategy);
|
||||
mpg.setTemplateEngine(new FreemarkerTemplateEngine());
|
||||
mpg.execute();
|
||||
}
|
||||
}
|
||||
35
src/main/java/cn/rainss/codegenerator/config/PGConfig.java
Normal file
35
src/main/java/cn/rainss/codegenerator/config/PGConfig.java
Normal file
@ -0,0 +1,35 @@
|
||||
package cn.rainss.codegenerator.config;
|
||||
|
||||
|
||||
/**
|
||||
* 数据库配置常量
|
||||
*
|
||||
* @author rainerosion
|
||||
*/
|
||||
public class PGConfig {
|
||||
/**
|
||||
* 数据库驱动类
|
||||
*/
|
||||
public static final String driverClass = "org.postgresql.Driver";
|
||||
|
||||
/**
|
||||
* 数据库用户名
|
||||
*/
|
||||
public static final String username = "postgres";
|
||||
|
||||
/**
|
||||
* 密码
|
||||
*/
|
||||
public static final String password = "timemail.email";
|
||||
|
||||
/**
|
||||
* 数据库
|
||||
*/
|
||||
public static final String catalog = "timemail";
|
||||
|
||||
/**
|
||||
* 链接地址
|
||||
*/
|
||||
public static final String url = "jdbc:postgresql://db.timemail.email:5432/timemail";
|
||||
|
||||
}
|
||||
40
src/main/java/cn/rainss/codegenerator/utils/ColumnClass.java
Normal file
40
src/main/java/cn/rainss/codegenerator/utils/ColumnClass.java
Normal file
@ -0,0 +1,40 @@
|
||||
package cn.rainss.codegenerator.utils;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* 列对应
|
||||
*
|
||||
* @author echo cow
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class ColumnClass {
|
||||
/**
|
||||
* 表名称
|
||||
*/
|
||||
private String tableName;
|
||||
/**
|
||||
* 列名称
|
||||
*/
|
||||
private String columnName;
|
||||
/**
|
||||
* 列大小
|
||||
*/
|
||||
private Integer columnSize;
|
||||
/**
|
||||
* 列的类型
|
||||
*/
|
||||
private String columnType;
|
||||
/**
|
||||
* 列的注释
|
||||
*/
|
||||
private String columnComment;
|
||||
/**
|
||||
* 是否能为空值
|
||||
*/
|
||||
private Boolean nullAble;
|
||||
}
|
||||
@ -0,0 +1,91 @@
|
||||
package cn.rainss.codegenerator.utils;
|
||||
|
||||
import cn.rainss.codegenerator.config.PGConfig;
|
||||
|
||||
import java.sql.DatabaseMetaData;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class DatabaseUtils {
|
||||
public DatabaseUtils(){}
|
||||
|
||||
public DatabaseUtils(PGConfig config){
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取数据库元数据
|
||||
*
|
||||
* @return 元数据
|
||||
* @throws Exception 异常
|
||||
*/
|
||||
private DatabaseMetaData getMetaData() throws Exception {
|
||||
Class.forName(PGConfig.driverClass);
|
||||
return DriverManager.getConnection(PGConfig.url,
|
||||
PGConfig.username, PGConfig.password).getMetaData();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取库的所有表
|
||||
*
|
||||
* @return 所有表
|
||||
*/
|
||||
public List<String> getTables() {
|
||||
List<String> tables = new ArrayList<>();
|
||||
try {
|
||||
ResultSet resultSet = getMetaData().getTables(PGConfig.catalog, null,
|
||||
"%", new String[]{"TABLE"});
|
||||
while (resultSet.next()) {
|
||||
String tableName = resultSet.getString("TABLE_NAME");
|
||||
tables.add(tableName);
|
||||
}
|
||||
resultSet.close();
|
||||
} catch (Exception e) {
|
||||
// log.error("Please check your database conf! {}", e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
return tables;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定表的所有列
|
||||
*
|
||||
* @param tableName 表名
|
||||
* @return 所有列的集合
|
||||
*/
|
||||
public List<ColumnClass> getColumns(String tableName) {
|
||||
try (ResultSet resultSet = getMetaData().getColumns(PGConfig.catalog, null, tableName, "%")) {
|
||||
return getColumns(resultSet, tableName);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取某列的结果集抽取
|
||||
*
|
||||
* @param resultSet 结果集
|
||||
* @param tableName 表名
|
||||
* @throws SQLException 异常
|
||||
*/
|
||||
private List<ColumnClass> getColumns(ResultSet resultSet, String tableName) throws SQLException {
|
||||
List<ColumnClass> columns = new ArrayList<>();
|
||||
while (resultSet.next()) {
|
||||
String columnName = resultSet.getString("COLUMN_NAME");
|
||||
String remarks = resultSet.getString("REMARKS");
|
||||
Boolean nullAble = resultSet.getInt("NULLABLE") == 1;
|
||||
columns.add(new ColumnClass(
|
||||
tableName,
|
||||
GenUtil.underlineToHump(columnName),
|
||||
resultSet.getInt("COLUMN_SIZE"),
|
||||
GenUtil.fieldConversion(resultSet.getString("TYPE_NAME")),
|
||||
remarks, nullAble
|
||||
));
|
||||
}
|
||||
return columns;
|
||||
}
|
||||
}
|
||||
83
src/main/java/cn/rainss/codegenerator/utils/GenUtil.java
Normal file
83
src/main/java/cn/rainss/codegenerator/utils/GenUtil.java
Normal file
@ -0,0 +1,83 @@
|
||||
package cn.rainss.codegenerator.utils;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 工具类
|
||||
*
|
||||
* @author echo
|
||||
*/
|
||||
@Slf4j
|
||||
public class GenUtil {
|
||||
private static final String UNDERLINE = "_";
|
||||
private static final Map<String, String> MYSQL_TO_JAVA = new HashMap<>();
|
||||
public static final String SUFFIX = ".java";
|
||||
|
||||
static {
|
||||
MYSQL_TO_JAVA.put("VARCHAR", "java.lang.String");
|
||||
MYSQL_TO_JAVA.put("BIGINT", "java.lang.Long");
|
||||
MYSQL_TO_JAVA.put("DATE", "java.time.LocalDate");
|
||||
MYSQL_TO_JAVA.put("FLOAT", "java.lang.Float");
|
||||
MYSQL_TO_JAVA.put("TINYINT", "java.lang.Integer");
|
||||
MYSQL_TO_JAVA.put("INT", "java.lang.Integer");
|
||||
MYSQL_TO_JAVA.put("INT2", "java.lang.Short");
|
||||
MYSQL_TO_JAVA.put("INT4", "java.lang.Integer");
|
||||
MYSQL_TO_JAVA.put("INT8", "java.lang.Long");
|
||||
MYSQL_TO_JAVA.put("BINARY", "java.lang.Byte");
|
||||
MYSQL_TO_JAVA.put("SMALLINT", "java.lang.Short");
|
||||
MYSQL_TO_JAVA.put("DATETIME", "java.time.LocalDateTime");
|
||||
MYSQL_TO_JAVA.put("TIMESTAMP", "java.time.LocalDateTime");
|
||||
MYSQL_TO_JAVA.put("BIT", "java.lang.Boolean");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 下划线命名转驼峰式命名
|
||||
*
|
||||
* @param para 下划线命名
|
||||
* @return 驼峰式命名
|
||||
*/
|
||||
public static String underlineToHump(String para) {
|
||||
StringBuilder result = new StringBuilder();
|
||||
for (String s : para.split(UNDERLINE)) {
|
||||
if (!para.contains("_")) {
|
||||
result.append(s);
|
||||
continue;
|
||||
}
|
||||
if (result.length() == 0) {
|
||||
result.append(s.toLowerCase());
|
||||
} else {
|
||||
result.append(s.substring(0, 1).toUpperCase());
|
||||
result.append(s.substring(1).toLowerCase());
|
||||
}
|
||||
}
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 下划线命名转驼峰式命名
|
||||
*
|
||||
* @param para 下划线命名
|
||||
* @param firstCharChange 首字母是否转换
|
||||
* @return 驼峰式命名
|
||||
*/
|
||||
public static String underlineToHump(String para, boolean firstCharChange) {
|
||||
String result = underlineToHump(para);
|
||||
return firstCharChange ? result.substring(0, 1).toUpperCase() + result.substring(1) : result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 数据库字段转换
|
||||
*
|
||||
* @param mysqlDataType 数据库字段类型
|
||||
* @return 转换结果
|
||||
*/
|
||||
public static String fieldConversion(String mysqlDataType) {
|
||||
return MYSQL_TO_JAVA.getOrDefault(mysqlDataType, "Object");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@ -1 +0,0 @@
|
||||
|
||||
12
src/main/resources/application.yaml
Normal file
12
src/main/resources/application.yaml
Normal file
@ -0,0 +1,12 @@
|
||||
application:
|
||||
generate:
|
||||
# 驱动类
|
||||
driver-class: org.
|
||||
# 用户名
|
||||
username: root
|
||||
# 密码
|
||||
password: 123456
|
||||
# 库名
|
||||
catalog: generate
|
||||
# 数据库地址
|
||||
url: jdbc:postgresql://db.timemail.email:5432/timemail
|
||||
26
src/main/resources/template/entity.ftl
Normal file
26
src/main/resources/template/entity.ftl
Normal file
@ -0,0 +1,26 @@
|
||||
package ${package_name};
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Table;
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* ${table_name}
|
||||
*
|
||||
* @author rainerosion
|
||||
* @date ${.now?datetime}
|
||||
*/
|
||||
@Data
|
||||
@Table(name = "${table_name}")
|
||||
@Entity(name = "${table_name}")
|
||||
public class ${class_name} implements Serializable {
|
||||
<#list columns as column>
|
||||
|
||||
/**
|
||||
* ${column.columnComment}
|
||||
*/
|
||||
private ${column.columnType} ${column.columnName};
|
||||
</#list>
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user