modify file

This commit is contained in:
luming 2021-04-19 18:05:05 +08:00
parent d0c7fc137b
commit 0a86d79f8d
2 changed files with 96 additions and 26 deletions

View File

@ -2,9 +2,8 @@ package cn.rainss.codegenerator.utils;
import cn.rainss.codegenerator.config.Config;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.core.ParseException;
import freemarker.template.*;
import org.apache.commons.lang3.StringUtils;
import java.io.*;
@ -25,6 +24,7 @@ public class GeneratorUtil {
private final static String SUFFIX = ".java";
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");
@ -171,6 +171,7 @@ public class GeneratorUtil {
/**
* 根据key获取类型
*
* @param key
* @return
*/
@ -191,7 +192,7 @@ public class GeneratorUtil {
className = toUpperCaseFist(className);
Configuration configuration = new Configuration(Configuration.VERSION_2_3_0);
Template template = configuration.getTemplate("src/main/resources/template/po.ftl");
String path = package2Path("/" + CONFIG.getProjectName() + "-server/" + CONFIG.getPackageName() + ".infrastructure.persistence.po/");
String path = CONFIG.getProjectName() + "-server/" + package2Path(CONFIG.getPackageName() + ".infrastructure.persistence.po/");
String filepath = path + className + SUFFIX;
// 文件夹不存在创建
if (!new File(path).exists()) {
@ -203,7 +204,7 @@ public class GeneratorUtil {
}
}
HashMap<String, Object> data = new HashMap<>();
Map<String, Object> data = new HashMap<>();
data.put("package", CONFIG.getPackageName() + ".infrastructure.persistence.po");
data.put("table_name", table);
data.put("class_name", className);
@ -212,8 +213,73 @@ public class GeneratorUtil {
template.process(data, new FileWriter(file));
}
/**
* @param packageName 主包名
* @param secondaryPackageName 二级包名称实体所在的包位置除去主包名后的位置
* @param type 类型 po entity dto mapper service repository controller
*/
public static void generatorByTemplate(List<Column> list,String tableName, String packageName, String secondaryPackageName, String type) {
String templatePath = String.format("src/main/resources/template/%s.ftl",toLowerCase(type));
String moduleName;
//如果有前缀去除
String className = tableName;
if (StringUtils.isNotEmpty(CONFIG.getTablePrefix())) {
className = formatTableName(tableName);
}
// 类名首字母大写
className = toUpperCaseFist(className);
switch (type) {
case "po":
case "service":
case "mapper":
case "repository":
case "entity":
moduleName = "server";
break;
case "dto":
moduleName = "dto";
break;
case "controller":
moduleName = "facade";
break;
default:
System.out.println("=== 没有匹配的类型 ===");
return;
}
String dir = String.format("%s-%s/%s/%s/", CONFIG.getProjectName(), moduleName, package2Path(CONFIG.getPackageName()), package2Path(secondaryPackageName));
String filePath = dir + className + SUFFIX;
try {
// 文件夹不存在创建
if (!new File(dir).exists()) {
if (new File(dir).mkdirs()) {
// System.out.println("PO 文件夹创建成功");
}
//创建文件
if (!new File(filePath).exists()) {
new File(filePath).createNewFile();
System.out.println("=== " + type + ":[" + className + " ] 创建成功 ===");
}
}
Configuration configuration = new Configuration(Configuration.VERSION_2_3_0);
Template template = configuration.getTemplate(templatePath);
} catch (TemplateNotFoundException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
} catch (MalformedTemplateNameException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 首字母大写
*
* @param str
* @return
*/
@ -222,6 +288,7 @@ public class GeneratorUtil {
chars[0] = toUpperCase(chars[0]);
return String.valueOf(chars);
}
/**
* 字符转成大写
*
@ -237,6 +304,7 @@ public class GeneratorUtil {
/**
* 包名转路径
*
* @param packageName
* @return
*/

View File

@ -1,9 +1,10 @@
package ${package};
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.Entity;
import javax.persistence.Table;
import java.io.Serializable;
/**
@ -13,8 +14,9 @@ import java.io.Serializable;
* @date ${.now?datetime}
*/
@Data
@Table(name = "${table_name}")
@Entity(name = "${table_name}")
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class ${class_name} implements Serializable {
<#list columns as column>