代码生成逻辑调整
This commit is contained in:
parent
76dc441444
commit
a38931a637
@ -1,25 +1,12 @@
|
|||||||
package cn.rainss.codegenerator;
|
package cn.rainss.codegenerator;
|
||||||
|
|
||||||
import cn.rainss.codegenerator.utils.Column;
|
|
||||||
import cn.rainss.codegenerator.utils.DatabaseUtil;
|
|
||||||
import cn.rainss.codegenerator.utils.GenerateConfig;
|
|
||||||
import cn.rainss.codegenerator.utils.GeneratorUtil;
|
import cn.rainss.codegenerator.utils.GeneratorUtil;
|
||||||
import org.yaml.snakeyaml.Yaml;
|
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.FileInputStream;
|
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class DDDGenerator {
|
public class DDDGenerator {
|
||||||
public static void main(String[] args) throws SQLException, FileNotFoundException, ClassNotFoundException {
|
public static void main(String[] args) throws SQLException, FileNotFoundException, ClassNotFoundException {
|
||||||
GeneratorUtil.Generator();
|
GeneratorUtil.Generator();
|
||||||
// GeneratorUtil.package2Path(DatabaseUtil.getInstance().getConfig().getGenerate().packageName);
|
|
||||||
// List<String> timemail = DatabaseUtil.getInstance().getTable();
|
|
||||||
// List<Column> tm_user = DatabaseUtil.getInstance().getColumn("tm_content");
|
|
||||||
// Yaml yaml = new Yaml();
|
|
||||||
// InputStream resourceAsStream = DDDGenerator.class.getClass().getClassLoader().getResourceAsStream("config.yaml");
|
|
||||||
// GenerateConfig generate = yaml.loadAs(new FileInputStream(new File("src/main/resources/config.yaml")),GenerateConfig.class);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
49
src/main/java/cn/rainss/codegenerator/utils/DataType.java
Normal file
49
src/main/java/cn/rainss/codegenerator/utils/DataType.java
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
package cn.rainss.codegenerator.utils;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.Getter;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
|
||||||
|
import java.sql.SQLException;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@AllArgsConstructor
|
||||||
|
public enum DataType {
|
||||||
|
BIT("bit", "Boolean"),
|
||||||
|
BOOL("bool", "Boolean"),
|
||||||
|
CHAR("char", "String"),
|
||||||
|
DATE("date", "LocalDate"),
|
||||||
|
DECIMAL("decimal", "BigDecimal"),
|
||||||
|
FLOAT4("float4", "Float"),
|
||||||
|
FLOAT8("float8", "Double"),
|
||||||
|
INT2("int2", "Short"),
|
||||||
|
INT4("int4", "Integer"),
|
||||||
|
INT8("int8", "Long"),
|
||||||
|
MONEY("money", "Double"),
|
||||||
|
NUMERIC("numeric", "BigDecimal"),
|
||||||
|
TEXT("text", "String"),
|
||||||
|
TIME("time", "LocalTime"),
|
||||||
|
TIMESTAMP("timestamp", "LocalDateTime"),
|
||||||
|
UUID("uuid", "String"),
|
||||||
|
VARCHAR("varchar", "String");
|
||||||
|
|
||||||
|
private final String sqlType;
|
||||||
|
private final String javaType;
|
||||||
|
|
||||||
|
//private final String package
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据sql数据类型获取java数据类型
|
||||||
|
* @param sqlType
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static DataType find(String sqlType){
|
||||||
|
for (DataType type : DataType.values()) {
|
||||||
|
if (StringUtils.equals(type.getSqlType(), sqlType)){
|
||||||
|
return type;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,20 +1,24 @@
|
|||||||
package cn.rainss.codegenerator.utils;
|
package cn.rainss.codegenerator.utils;
|
||||||
|
|
||||||
|
|
||||||
import cn.rainss.codegenerator.config.Config;
|
import cn.rainss.codegenerator.config.Config;
|
||||||
import freemarker.core.ParseException;
|
import freemarker.template.Configuration;
|
||||||
import freemarker.template.*;
|
import freemarker.template.Template;
|
||||||
|
import freemarker.template.TemplateException;
|
||||||
|
import freemarker.template.utility.CollectionUtils;
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
import java.io.FileWriter;
|
import java.io.FileWriter;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.lang.reflect.Type;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Objects;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 工具类
|
* 工具类
|
||||||
@ -22,58 +26,8 @@ import java.util.Objects;
|
|||||||
* @author echo
|
* @author echo
|
||||||
*/
|
*/
|
||||||
public class GeneratorUtil {
|
public class GeneratorUtil {
|
||||||
// 类型映射
|
|
||||||
private final static Map<String, String> SQL_TYPE_TO_JAVA_TYPE = new HashMap<>();
|
|
||||||
// 文件后缀
|
|
||||||
private static String SUFFIX = ".java";
|
|
||||||
|
|
||||||
private final static Config CONFIG = DatabaseUtil.getInstance().getConfig().getGenerate();
|
private final static Config CONFIG = DatabaseUtil.getInstance().getConfig().getGenerate();
|
||||||
|
|
||||||
static {
|
|
||||||
SQL_TYPE_TO_JAVA_TYPE.put("bit", "Boolean");
|
|
||||||
SQL_TYPE_TO_JAVA_TYPE.put("bool", "Boolean");
|
|
||||||
// SQL_TYPE_TO_JAVA_TYPE.put("box","");
|
|
||||||
// SQL_TYPE_TO_JAVA_TYPE.put("bytea","");
|
|
||||||
SQL_TYPE_TO_JAVA_TYPE.put("char", "String");
|
|
||||||
// SQL_TYPE_TO_JAVA_TYPE.put("cidr","");
|
|
||||||
// SQL_TYPE_TO_JAVA_TYPE.put("circle","");
|
|
||||||
SQL_TYPE_TO_JAVA_TYPE.put("date", "LocalDate");
|
|
||||||
SQL_TYPE_TO_JAVA_TYPE.put("decimal", "BigDecimal");
|
|
||||||
SQL_TYPE_TO_JAVA_TYPE.put("float4", "Float");
|
|
||||||
SQL_TYPE_TO_JAVA_TYPE.put("float8", "Double");
|
|
||||||
// SQL_TYPE_TO_JAVA_TYPE.put("inet","");
|
|
||||||
SQL_TYPE_TO_JAVA_TYPE.put("int2", "Short");
|
|
||||||
SQL_TYPE_TO_JAVA_TYPE.put("int4", "Integer");
|
|
||||||
SQL_TYPE_TO_JAVA_TYPE.put("int8", "Long");
|
|
||||||
// SQL_TYPE_TO_JAVA_TYPE.put("interval","");
|
|
||||||
// SQL_TYPE_TO_JAVA_TYPE.put("json","");
|
|
||||||
// SQL_TYPE_TO_JAVA_TYPE.put("jsonb","");
|
|
||||||
// SQL_TYPE_TO_JAVA_TYPE.put("line","");
|
|
||||||
// SQL_TYPE_TO_JAVA_TYPE.put("lseg","");
|
|
||||||
// SQL_TYPE_TO_JAVA_TYPE.put("macaddr","");
|
|
||||||
SQL_TYPE_TO_JAVA_TYPE.put("money", "Double");
|
|
||||||
SQL_TYPE_TO_JAVA_TYPE.put("numeric", "BigDecimal");
|
|
||||||
// SQL_TYPE_TO_JAVA_TYPE.put("path","");
|
|
||||||
// SQL_TYPE_TO_JAVA_TYPE.put("point","");
|
|
||||||
// SQL_TYPE_TO_JAVA_TYPE.put("polygon","");
|
|
||||||
// SQL_TYPE_TO_JAVA_TYPE.put("serial2","");
|
|
||||||
// SQL_TYPE_TO_JAVA_TYPE.put("serial4","");
|
|
||||||
// SQL_TYPE_TO_JAVA_TYPE.put("serial8","");
|
|
||||||
SQL_TYPE_TO_JAVA_TYPE.put("text", "String");
|
|
||||||
SQL_TYPE_TO_JAVA_TYPE.put("time", "LocalTime");
|
|
||||||
SQL_TYPE_TO_JAVA_TYPE.put("timestamp", "LocalDateTime");
|
|
||||||
// SQL_TYPE_TO_JAVA_TYPE.put("timestamptz","");
|
|
||||||
// SQL_TYPE_TO_JAVA_TYPE.put("timetz","");
|
|
||||||
// SQL_TYPE_TO_JAVA_TYPE.put("tsquery","");
|
|
||||||
// SQL_TYPE_TO_JAVA_TYPE.put("tsvector","");
|
|
||||||
// SQL_TYPE_TO_JAVA_TYPE.put("txid_snapshot","");
|
|
||||||
SQL_TYPE_TO_JAVA_TYPE.put("uuid", "String");
|
|
||||||
// SQL_TYPE_TO_JAVA_TYPE.put("varbit","");
|
|
||||||
SQL_TYPE_TO_JAVA_TYPE.put("varchar", "String");
|
|
||||||
// SQL_TYPE_TO_JAVA_TYPE.put("xml","");
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String toLowerCase(String str) {
|
public static String toLowerCase(String str) {
|
||||||
|
|
||||||
if (StringUtils.isNotEmpty(str)) {
|
if (StringUtils.isNotEmpty(str)) {
|
||||||
@ -154,7 +108,7 @@ public class GeneratorUtil {
|
|||||||
DatabaseUtil instance = DatabaseUtil.getInstance();
|
DatabaseUtil instance = DatabaseUtil.getInstance();
|
||||||
List<String> table = instance.getTable();
|
List<String> table = instance.getTable();
|
||||||
table.forEach(t -> {
|
table.forEach(t -> {
|
||||||
System.out.println("<<<<< table: " + t + " >>>>>");
|
System.out.printf("<<<<< Generate code from table: %s >>>>>%n", t);
|
||||||
try {
|
try {
|
||||||
List<Column> column = instance.getColumn(t);
|
List<Column> column = instance.getColumn(t);
|
||||||
generatorCode(column, t);
|
generatorCode(column, t);
|
||||||
@ -172,22 +126,17 @@ public class GeneratorUtil {
|
|||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public static String getJavaType(String key) {
|
public static String getJavaType(String key) {
|
||||||
return SQL_TYPE_TO_JAVA_TYPE.getOrDefault(key, "Object");
|
DataType type = DataType.find(key);
|
||||||
|
return type == null ? "Object" : type.getJavaType();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 生成实体
|
* 生成所有类型的代码
|
||||||
*/
|
*/
|
||||||
public static void generatorCode(List<Column> list, String tableName) {
|
public static void generatorCode(List<Column> list, String tableName) {
|
||||||
// generatorByTemplate(list,tableName,"po");
|
for (TypeEnum typeEnum : TypeEnum.values()) {
|
||||||
// generatorByTemplate(list,tableName,"entity");
|
generatorByTemplate(list, tableName, typeEnum);
|
||||||
// generatorByTemplate(list,tableName,"resDto");
|
}
|
||||||
// generatorByTemplate(list,tableName,"reqDto");
|
|
||||||
// generatorByTemplate(list,tableName,"repository");
|
|
||||||
// generatorByTemplate(list,tableName,"service");
|
|
||||||
// generatorByTemplate(null,tableName,"mapper");
|
|
||||||
generatorByTemplate(null, tableName, "mapper.xml");
|
|
||||||
// generatorByTemplate(list,tableName,"controller");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -201,153 +150,130 @@ public class GeneratorUtil {
|
|||||||
public static void generatorByTemplate(List<Column> columnList, String table, TypeEnum type) {
|
public static void generatorByTemplate(List<Column> columnList, String table, TypeEnum type) {
|
||||||
// 文件模板路径
|
// 文件模板路径
|
||||||
String templatePath = String.format("src/main/resources/template/%s.ftl", type.getTemplate());
|
String templatePath = String.format("src/main/resources/template/%s.ftl", type.getTemplate());
|
||||||
String destPath = null;
|
String basePackage = CONFIG.getBasePackage();
|
||||||
String projectName = CONFIG.getProjectName();
|
|
||||||
// 去除前缀
|
// 去除前缀
|
||||||
String className = formatTableName(table);
|
String className = formatTableName(table);
|
||||||
// 转换为大驼峰式命名
|
// 转换为大驼峰式命名
|
||||||
className = toUpperCaseFist(toCamelCase(className));
|
className = toUpperCaseFist(toCamelCase(className));
|
||||||
String baseMapperXml = "%s-%s/src/main/resources/mapper/%s%s.xml";
|
// 模板参数设置
|
||||||
String baseClassFile = "%s-%s/src/main/java/%s/%s/%s%s.java";
|
TableModelInfo tableInfo = TableModelInfo.builder()
|
||||||
|
.packages(basePackage)
|
||||||
|
.columns(columnList)
|
||||||
|
.configuration(CONFIG)
|
||||||
|
.className(className)
|
||||||
|
.tableName(table)
|
||||||
|
.build();
|
||||||
|
setTableInfo(columnList, tableInfo);
|
||||||
|
String destPath = getDestFilePath(className, type);
|
||||||
|
if (StringUtils.isEmpty(destPath)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
// 提取文件夹路径
|
||||||
|
String dir = destPath.substring(0, destPath.lastIndexOf("/"));
|
||||||
|
File folder = new File(dir);
|
||||||
|
File file = new File(destPath);
|
||||||
|
if (!folder.exists()) {
|
||||||
|
// 创建目录
|
||||||
|
folder.mkdirs();
|
||||||
|
}
|
||||||
|
if (!file.exists()) {
|
||||||
|
// 创建不存在的文件
|
||||||
|
file.createNewFile();
|
||||||
|
}
|
||||||
|
System.out.println(String.format("模板文件路径:%s 目标文件路径:%s", templatePath, destPath));
|
||||||
|
Configuration freemarker = new Configuration(Configuration.VERSION_2_3_0);
|
||||||
|
Template template = freemarker.getTemplate(templatePath);
|
||||||
|
template.process(tableInfo, new FileWriter(file));
|
||||||
|
|
||||||
|
} catch (IOException | TemplateException e) {
|
||||||
|
System.out.println("文件操作失败!");
|
||||||
|
e.printStackTrace();
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
//System.out.println(String.format("模板文件路径:%s 目标文件路径:%s", templatePath, destPath));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void setTableInfo(List<Column> columnList, TableModelInfo tableInfo) {
|
||||||
|
tableInfo.setHasBigDecimal(Boolean.FALSE);
|
||||||
|
tableInfo.setHasLocalDate(Boolean.FALSE);
|
||||||
|
tableInfo.setHasLocalTime(Boolean.FALSE);
|
||||||
|
tableInfo.setHasLocalDateTime(Boolean.FALSE);
|
||||||
|
if (columnList == null || columnList.size() == 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
for (Column column : columnList) {
|
||||||
|
String javaType = column.getJavaType();
|
||||||
|
if (DataType.DECIMAL.getJavaType().equals(javaType)) {
|
||||||
|
tableInfo.setHasBigDecimal(Boolean.TRUE);
|
||||||
|
} else if (DataType.TIMESTAMP.getSqlType().equals(javaType)) {
|
||||||
|
tableInfo.setHasLocalDateTime(Boolean.TRUE);
|
||||||
|
} else if (DataType.DATE.getJavaType().equals(javaType)) {
|
||||||
|
tableInfo.setHasLocalDate(Boolean.TRUE);
|
||||||
|
} else if (DataType.TIME.getJavaType().equals(javaType)) {
|
||||||
|
tableInfo.setHasLocalTime(Boolean.TRUE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取文件输出路径
|
||||||
|
*
|
||||||
|
* @param className 类名
|
||||||
|
* @param type 类型枚举
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
private static String getDestFilePath(String className, TypeEnum type) {
|
||||||
|
if (TypeEnum.MAPPER_XML.equals(type)) {
|
||||||
|
return String.format("%s-%s/src/main/resources/mapper/%s%s.xml",
|
||||||
|
CONFIG.getProjectName(),
|
||||||
|
type.getModule(),
|
||||||
|
className,
|
||||||
|
toUpperCaseFist(type.getTemplate())
|
||||||
|
);
|
||||||
|
}
|
||||||
|
String packageLocation = "";
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case MAPPER_XML:
|
case CONTROLLER:
|
||||||
destPath = String.format(baseMapperXml, projectName, type.getModule(), className, toUpperCaseFist(type.getTemplate()));
|
packageLocation = CONFIG.getPackageLocation().getController();
|
||||||
|
break;
|
||||||
|
case PO:
|
||||||
|
packageLocation = CONFIG.getPackageLocation().getPo();
|
||||||
|
break;
|
||||||
|
case DOMAIN_SERVICE:
|
||||||
|
//packageLocation = CONFIG.getPackageLocation()
|
||||||
|
break;
|
||||||
|
case APPLICATION_SERVICE:
|
||||||
|
//packageLocation = CONFIG.getPackageLocation()
|
||||||
|
break;
|
||||||
|
case MAPPER:
|
||||||
|
packageLocation = CONFIG.getPackageLocation().getMapper();
|
||||||
|
break;
|
||||||
|
case REPOSITORY:
|
||||||
|
packageLocation = CONFIG.getPackageLocation().getRepository();
|
||||||
|
break;
|
||||||
|
case ENTITY:
|
||||||
|
packageLocation = CONFIG.getPackageLocation().getEntity();
|
||||||
break;
|
break;
|
||||||
case RES_DTO:
|
case RES_DTO:
|
||||||
destPath = String.format(baseClassFile, projectName, type.getModule(),
|
packageLocation = CONFIG.getPackageLocation().getResDto();
|
||||||
package2Path(CONFIG.getPackageName()),
|
break;
|
||||||
package2Path(CONFIG.getPackageLocation().getResDto()),
|
|
||||||
className, toUpperCaseFist(type.getTemplate()));
|
|
||||||
|
|
||||||
case REQ_DTO:
|
case REQ_DTO:
|
||||||
destPath = String.format(baseClassFile, projectName, type.getModule(), package2Path(CONFIG.getPackageName()), package2Path(CONFIG.getPackageLocation().getReqDto()));
|
packageLocation = CONFIG.getPackageLocation().getReqDto();
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
destPath = String.format(baseClassFile, projectName, type.getModule(), package2Path(CONFIG.getPackageName()), package2Path(""));
|
System.out.println("没有匹配的类型");
|
||||||
}
|
}
|
||||||
|
return String.format("%s-%s/src/main/java/%s/%s/%s%s.java",
|
||||||
|
CONFIG.getProjectName(), type.getModule(),
|
||||||
|
package2Path(CONFIG.getBasePackage()),
|
||||||
|
package2Path(packageLocation),
|
||||||
|
className, toUpperCaseFist(type.getTemplate())
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* create template file
|
* 首字母大写并去除扩展名
|
||||||
*
|
|
||||||
* @param type 类型 po entity dto mapper service repository controller
|
|
||||||
*/
|
|
||||||
public static void generatorByTemplate(List<Column> list, String tableName, String type) {
|
|
||||||
String templatePath = String.format("src/main/resources/template/%s.ftl", toLowerCase(type));
|
|
||||||
String moduleName;
|
|
||||||
String secondaryPackage = null;
|
|
||||||
|
|
||||||
String className = tableName;
|
|
||||||
// data
|
|
||||||
Map<String, Object> data = new HashMap<>();
|
|
||||||
// add base data
|
|
||||||
data.put("mapper_package", CONFIG.getMapperPackage());
|
|
||||||
data.put("entity_package", CONFIG.getEntityPackage());
|
|
||||||
data.put("po_package", CONFIG.getPoPackage());
|
|
||||||
data.put("service_package", CONFIG.getServicePackage());
|
|
||||||
data.put("repository_package", CONFIG.getRepositoryPackage());
|
|
||||||
data.put("res_dto_package", CONFIG.getResDtoPackage());
|
|
||||||
data.put("req_dto_package", CONFIG.getReqDtoPackage());
|
|
||||||
data.put("controller_package", CONFIG.getControllerpackage());
|
|
||||||
data.put("package", CONFIG.getPackageName());
|
|
||||||
data.put("author", CONFIG.getAuthor());
|
|
||||||
// 如果有前缀去除
|
|
||||||
if (StringUtils.isNotEmpty(CONFIG.getTablePrefix())) {
|
|
||||||
className = formatTableName(tableName);
|
|
||||||
}
|
|
||||||
// 转换为大驼峰式命名
|
|
||||||
className = toUpperCaseFist(toCamelCase(className));
|
|
||||||
switch (type) {
|
|
||||||
case "po":
|
|
||||||
secondaryPackage = CONFIG.getPoPackage();
|
|
||||||
moduleName = "server";
|
|
||||||
break;
|
|
||||||
case "service":
|
|
||||||
secondaryPackage = CONFIG.getServicePackage();
|
|
||||||
moduleName = "server";
|
|
||||||
break;
|
|
||||||
case "mapper":
|
|
||||||
secondaryPackage = CONFIG.getMapperPackage();
|
|
||||||
moduleName = "server";
|
|
||||||
break;
|
|
||||||
case "mapper.xml":
|
|
||||||
//todo detal
|
|
||||||
SUFFIX = ".xml";
|
|
||||||
moduleName = "server";
|
|
||||||
break;
|
|
||||||
case "repository":
|
|
||||||
secondaryPackage = CONFIG.getRepositoryPackage();
|
|
||||||
moduleName = "server";
|
|
||||||
break;
|
|
||||||
case "entity":
|
|
||||||
secondaryPackage = CONFIG.getMapperPackage();
|
|
||||||
moduleName = "server";
|
|
||||||
break;
|
|
||||||
case "resDto":
|
|
||||||
secondaryPackage = CONFIG.getResDtoPackage();
|
|
||||||
moduleName = "dto";
|
|
||||||
break;
|
|
||||||
case "reqDto":
|
|
||||||
secondaryPackage = CONFIG.getReqDtoPackage();
|
|
||||||
moduleName = "dto";
|
|
||||||
break;
|
|
||||||
case "controller":
|
|
||||||
secondaryPackage = CONFIG.getControllerpackage();
|
|
||||||
moduleName = "facade";
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
System.out.println("=== 没有匹配的类型 ===");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
String dir = "";
|
|
||||||
String filePath = "";
|
|
||||||
// mapper.xml file deal
|
|
||||||
if (Objects.equals(type, "mapper.xml")) {
|
|
||||||
dir = String.format("%s-%s/src/main/resources/mapper/", CONFIG.getProjectName(), moduleName);
|
|
||||||
filePath = dir + className + toUpperCaseFist(type) + SUFFIX;
|
|
||||||
} else {
|
|
||||||
dir = String.format("%s-%s/src/main/java/%s/%s/", CONFIG.getProjectName(), moduleName, package2Path(CONFIG.getPackageName()), package2Path(secondaryPackage));
|
|
||||||
filePath = dir + className + toUpperCaseFist(type) + SUFFIX;
|
|
||||||
}
|
|
||||||
|
|
||||||
File file = new File(filePath);
|
|
||||||
try {
|
|
||||||
// 文件夹不存在创建
|
|
||||||
if (!new File(dir).exists()) {
|
|
||||||
if (new File(dir).mkdirs()) {
|
|
||||||
// System.out.println("PO 文件夹创建成功");
|
|
||||||
}
|
|
||||||
//创建文件
|
|
||||||
if (!file.exists()) {
|
|
||||||
new File(filePath).createNewFile();
|
|
||||||
System.out.println("=== " + type + ":[" + className + " ] 创建成功 ===");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
Configuration configuration = new Configuration(Configuration.VERSION_2_3_0);
|
|
||||||
Template template = configuration.getTemplate(templatePath);
|
|
||||||
data.put("table_name", tableName);
|
|
||||||
data.put("class_name", className);
|
|
||||||
data.put("columns", list);
|
|
||||||
template.process(data, new FileWriter(file));
|
|
||||||
} catch (TemplateNotFoundException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
} catch (ParseException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
} catch (MalformedTemplateNameException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
} catch (TemplateException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 首字母大写
|
|
||||||
*
|
*
|
||||||
* @param str
|
* @param str
|
||||||
* @return
|
* @return
|
||||||
@ -385,8 +311,39 @@ public class GeneratorUtil {
|
|||||||
return packageName.replace(".", "/");
|
return packageName.replace(".", "/");
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String formatTableName(String str) {
|
/**
|
||||||
//todo 待优化
|
* 表名前缀处理
|
||||||
return str.replaceFirst(CONFIG.getTablePrefix(), "");
|
*
|
||||||
|
* @param table 表名
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static String formatTableName(String table) {
|
||||||
|
if (StringUtils.isNotEmpty(CONFIG.getTablePrefix())) {
|
||||||
|
return table.replaceFirst(CONFIG.getTablePrefix(), "");
|
||||||
|
}
|
||||||
|
return table;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
@Builder
|
||||||
|
public static class TableModelInfo {
|
||||||
|
private Boolean hasLocalDate;
|
||||||
|
|
||||||
|
private Boolean hasLocalTime;
|
||||||
|
|
||||||
|
private Boolean hasLocalDateTime;
|
||||||
|
|
||||||
|
private Boolean hasBigDecimal;
|
||||||
|
|
||||||
|
private String packages;
|
||||||
|
|
||||||
|
private List<Column> columns;
|
||||||
|
|
||||||
|
private Config configuration;
|
||||||
|
|
||||||
|
private String className;
|
||||||
|
|
||||||
|
private String tableName;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
0
src/main/resources/template/applicationService.ftl
Normal file
0
src/main/resources/template/applicationService.ftl
Normal file
0
src/main/resources/template/controller.ftl
Normal file
0
src/main/resources/template/controller.ftl
Normal file
0
src/main/resources/template/domainService.ftl
Normal file
0
src/main/resources/template/domainService.ftl
Normal file
@ -1,4 +1,4 @@
|
|||||||
package ${package}.${entity_package};
|
package ${packages}.${configuration.packageLocation.entity};
|
||||||
|
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Builder;
|
import lombok.Builder;
|
||||||
@ -6,18 +6,30 @@ import lombok.Data;
|
|||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
|
<#if hasBigDecimal>
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
</#if>
|
||||||
|
<#if hasLocalDate>
|
||||||
|
import java.time.LocalDate;
|
||||||
|
</#if>
|
||||||
|
<#if hasLocalTime>
|
||||||
|
import java.time.LocalTime;
|
||||||
|
</#if>
|
||||||
|
<#if hasLocalDateTime>
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
</#if>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ${table_name}
|
* ${tableName}
|
||||||
* entity
|
* entity
|
||||||
* @author ${author}
|
* @author ${configuration.author}
|
||||||
* @date ${.now?datetime}
|
* @date ${.now?datetime}
|
||||||
*/
|
*/
|
||||||
@Data
|
@Data
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
@Builder
|
@Builder
|
||||||
public class ${class_name} implements Serializable {
|
public class ${className} implements Serializable {
|
||||||
<#list columns as column>
|
<#list columns as column>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -1,16 +1,16 @@
|
|||||||
package ${package}.${po_package};
|
package ${packages}.${configuration.packageLocation.mapper};
|
||||||
|
|
||||||
import ${package}.${po_package}.${class_name};
|
import ${packages}.${configuration.packageLocation.mapper}.${className};
|
||||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>
|
* <p>
|
||||||
* Mapper 接口
|
* ${className}Mapper 接口
|
||||||
* </p>
|
* </p>
|
||||||
*
|
*
|
||||||
* @author ${author}
|
* @author ${configuration.author}
|
||||||
* @since 2021-04-19
|
* @since 2021-04-19
|
||||||
*/
|
*/
|
||||||
public interface ${class_name}Mapper extends BaseMapper<${class_name}> {
|
public interface ${className}Mapper extends BaseMapper<${className}> {
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -1,5 +1,5 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||||
<mapper namespace="${package}.${po_package}.${class_name}Mapper">
|
<mapper namespace="${packages}.${configuration.packageLocation.po}.${className}Mapper">
|
||||||
|
|
||||||
</mapper>
|
</mapper>
|
||||||
@ -1,4 +1,4 @@
|
|||||||
package ${package}.${po_package};
|
package ${packages}.${configuration.packageLocation.po};
|
||||||
|
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Builder;
|
import lombok.Builder;
|
||||||
@ -6,18 +6,30 @@ import lombok.Data;
|
|||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
|
<#if hasBigDecimal>
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
</#if>
|
||||||
|
<#if hasLocalDate>
|
||||||
|
import java.time.LocalDate;
|
||||||
|
</#if>
|
||||||
|
<#if hasLocalTime>
|
||||||
|
import java.time.LocalTime;
|
||||||
|
</#if>
|
||||||
|
<#if hasLocalDateTime>
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
</#if>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ${table_name}
|
* ${tableName}
|
||||||
* po
|
* po
|
||||||
* @author ${author}
|
* @author ${configuration.author}
|
||||||
* @date ${.now?datetime}
|
* @date ${.now?datetime}
|
||||||
*/
|
*/
|
||||||
@Data
|
@Data
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
@Builder
|
@Builder
|
||||||
public class ${class_name} implements Serializable {
|
public class ${className} implements Serializable {
|
||||||
<#list columns as column>
|
<#list columns as column>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
0
src/main/resources/template/repository.ftl
Normal file
0
src/main/resources/template/repository.ftl
Normal file
@ -1,4 +1,4 @@
|
|||||||
package ${package};
|
package ${packages};
|
||||||
|
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Builder;
|
import lombok.Builder;
|
||||||
@ -6,18 +6,30 @@ import lombok.Data;
|
|||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
|
<#if hasBigDecimal>
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
</#if>
|
||||||
|
<#if hasLocalDate>
|
||||||
|
import java.time.LocalDate;
|
||||||
|
</#if>
|
||||||
|
<#if hasLocalTime>
|
||||||
|
import java.time.LocalTime;
|
||||||
|
</#if>
|
||||||
|
<#if hasLocalDateTime>
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
</#if>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ${table_name}
|
* ${tableName}
|
||||||
* entity
|
* entity
|
||||||
* @author ${author}
|
* @author ${configuration.author}
|
||||||
* @date ${.now?datetime}
|
* @date ${.now?datetime}
|
||||||
*/
|
*/
|
||||||
@Data
|
@Data
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
@Builder
|
@Builder
|
||||||
public class ${class_name} implements Serializable {
|
public class ${className} implements Serializable {
|
||||||
<#list columns as column>
|
<#list columns as column>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
package ${package};
|
package ${packages};
|
||||||
|
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Builder;
|
import lombok.Builder;
|
||||||
@ -6,18 +6,30 @@ import lombok.Data;
|
|||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
|
<#if hasBigDecimal>
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
</#if>
|
||||||
|
<#if hasLocalDate>
|
||||||
|
import java.time.LocalDate;
|
||||||
|
</#if>
|
||||||
|
<#if hasLocalTime>
|
||||||
|
import java.time.LocalTime;
|
||||||
|
</#if>
|
||||||
|
<#if hasLocalDateTime>
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
</#if>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ${table_name}
|
* ${tableName}
|
||||||
* entity
|
* entity
|
||||||
* @author ${author}
|
* @author ${configuration.author}
|
||||||
* @date ${.now?datetime}
|
* @date ${.now?datetime}
|
||||||
*/
|
*/
|
||||||
@Data
|
@Data
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
@Builder
|
@Builder
|
||||||
public class ${class_name} implements Serializable {
|
public class ${className} implements Serializable {
|
||||||
<#list columns as column>
|
<#list columns as column>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user