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");
@ -153,7 +153,7 @@ public class GeneratorUtil {
// System.out.println("<<<<< table" + t + " >>>>>");
try {
List<Column> column = instance.getColumn(t);
generatorPo(column,t);
generatorPo(column, t);
} catch (SQLException throwables) {
throwables.printStackTrace();
} catch (FileNotFoundException e) {
@ -171,57 +171,124 @@ public class GeneratorUtil {
/**
* 根据key获取类型
*
* @param key
* @return
*/
public static String getJavaType(String key) {
return SQL_TYPE_TO_JAVA_TYPE.getOrDefault(key,"Object");
return SQL_TYPE_TO_JAVA_TYPE.getOrDefault(key, "Object");
}
/**
* 生成po实体
*/
public static void generatorPo(List<Column> list,String table) throws IOException, TemplateException {
public static void generatorPo(List<Column> list, String table) throws IOException, TemplateException {
//如果有前缀去除
String className = table;
if (StringUtils.isNotEmpty(CONFIG.getTablePrefix())){
if (StringUtils.isNotEmpty(CONFIG.getTablePrefix())) {
className = formatTableName(table);
}
// 类名首字母大写
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()){
if (new File(path).mkdirs()){
if (!new File(path).exists()) {
if (new File(path).mkdirs()) {
// System.out.println("PO 文件夹创建成功");
}
if(new File(filepath).createNewFile()){
System.out.println("=== PO:["+ className +" ] 创建成功 ===");
if (new File(filepath).createNewFile()) {
System.out.println("=== PO:[" + className + " ] 创建成功 ===");
}
}
HashMap<String, Object> data = new HashMap<>();
data.put("package",CONFIG.getPackageName() + ".infrastructure.persistence.po");
data.put("table_name",table);
data.put("class_name",className);
data.put("columns",list);
Map<String, Object> data = new HashMap<>();
data.put("package", CONFIG.getPackageName() + ".infrastructure.persistence.po");
data.put("table_name", table);
data.put("class_name", className);
data.put("columns", list);
File file = new File(filepath);
template.process(data,new FileWriter(file));
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
*/
public static String toUpperCaseFist(String str){
public static String toUpperCaseFist(String str) {
char[] chars = str.toCharArray();
chars[0] = toUpperCase(chars[0]);
return String.valueOf(chars);
}
/**
* 字符转成大写
*
@ -237,15 +304,16 @@ public class GeneratorUtil {
/**
* 包名转路径
*
* @param packageName
* @return
*/
public static String package2Path(String packageName){
public static String package2Path(String packageName) {
return "src/main/java/" + packageName.replace(".", "/");
}
public static String formatTableName(String str){
public static String formatTableName(String str) {
//todo 待优化
return str.replaceFirst(CONFIG.getTablePrefix(),"");
return str.replaceFirst(CONFIG.getTablePrefix(), "");
}
}

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>