modify file
This commit is contained in:
parent
d0c7fc137b
commit
0a86d79f8d
@ -2,9 +2,8 @@ package cn.rainss.codegenerator.utils;
|
|||||||
|
|
||||||
|
|
||||||
import cn.rainss.codegenerator.config.Config;
|
import cn.rainss.codegenerator.config.Config;
|
||||||
import freemarker.template.Configuration;
|
import freemarker.core.ParseException;
|
||||||
import freemarker.template.Template;
|
import freemarker.template.*;
|
||||||
import freemarker.template.TemplateException;
|
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
@ -25,6 +24,7 @@ public class GeneratorUtil {
|
|||||||
private final static String SUFFIX = ".java";
|
private final static String SUFFIX = ".java";
|
||||||
|
|
||||||
private final static Config CONFIG = DatabaseUtil.getInstance().getConfig().getGenerate();
|
private final static Config CONFIG = DatabaseUtil.getInstance().getConfig().getGenerate();
|
||||||
|
|
||||||
static {
|
static {
|
||||||
SQL_TYPE_TO_JAVA_TYPE.put("bit", "Boolean");
|
SQL_TYPE_TO_JAVA_TYPE.put("bit", "Boolean");
|
||||||
SQL_TYPE_TO_JAVA_TYPE.put("bool", "Boolean");
|
SQL_TYPE_TO_JAVA_TYPE.put("bool", "Boolean");
|
||||||
@ -153,7 +153,7 @@ public class GeneratorUtil {
|
|||||||
// System.out.println("<<<<< table" + t + " >>>>>");
|
// System.out.println("<<<<< table" + t + " >>>>>");
|
||||||
try {
|
try {
|
||||||
List<Column> column = instance.getColumn(t);
|
List<Column> column = instance.getColumn(t);
|
||||||
generatorPo(column,t);
|
generatorPo(column, t);
|
||||||
} catch (SQLException throwables) {
|
} catch (SQLException throwables) {
|
||||||
throwables.printStackTrace();
|
throwables.printStackTrace();
|
||||||
} catch (FileNotFoundException e) {
|
} catch (FileNotFoundException e) {
|
||||||
@ -171,57 +171,124 @@ public class GeneratorUtil {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据key获取类型
|
* 根据key获取类型
|
||||||
|
*
|
||||||
* @param key
|
* @param key
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public static String getJavaType(String key) {
|
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实体
|
* 生成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;
|
String className = table;
|
||||||
if (StringUtils.isNotEmpty(CONFIG.getTablePrefix())){
|
if (StringUtils.isNotEmpty(CONFIG.getTablePrefix())) {
|
||||||
className = formatTableName(table);
|
className = formatTableName(table);
|
||||||
}
|
}
|
||||||
// 类名首字母大写
|
// 类名首字母大写
|
||||||
className = toUpperCaseFist(className);
|
className = toUpperCaseFist(className);
|
||||||
Configuration configuration = new Configuration(Configuration.VERSION_2_3_0);
|
Configuration configuration = new Configuration(Configuration.VERSION_2_3_0);
|
||||||
Template template = configuration.getTemplate("src/main/resources/template/po.ftl");
|
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;
|
String filepath = path + className + SUFFIX;
|
||||||
// 文件夹不存在创建
|
// 文件夹不存在创建
|
||||||
if(!new File(path).exists()){
|
if (!new File(path).exists()) {
|
||||||
if (new File(path).mkdirs()){
|
if (new File(path).mkdirs()) {
|
||||||
// System.out.println("PO 文件夹创建成功");
|
// System.out.println("PO 文件夹创建成功");
|
||||||
}
|
}
|
||||||
if(new File(filepath).createNewFile()){
|
if (new File(filepath).createNewFile()) {
|
||||||
System.out.println("=== PO:["+ className +" ] 创建成功 ===");
|
System.out.println("=== PO:[" + className + " ] 创建成功 ===");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
HashMap<String, Object> data = new HashMap<>();
|
Map<String, Object> data = new HashMap<>();
|
||||||
data.put("package",CONFIG.getPackageName() + ".infrastructure.persistence.po");
|
data.put("package", CONFIG.getPackageName() + ".infrastructure.persistence.po");
|
||||||
data.put("table_name",table);
|
data.put("table_name", table);
|
||||||
data.put("class_name",className);
|
data.put("class_name", className);
|
||||||
data.put("columns",list);
|
data.put("columns", list);
|
||||||
File file = new File(filepath);
|
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
|
* @param str
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public static String toUpperCaseFist(String str){
|
public static String toUpperCaseFist(String str) {
|
||||||
char[] chars = str.toCharArray();
|
char[] chars = str.toCharArray();
|
||||||
chars[0] = toUpperCase(chars[0]);
|
chars[0] = toUpperCase(chars[0]);
|
||||||
return String.valueOf(chars);
|
return String.valueOf(chars);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 字符转成大写
|
* 字符转成大写
|
||||||
*
|
*
|
||||||
@ -237,15 +304,16 @@ public class GeneratorUtil {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 包名转路径
|
* 包名转路径
|
||||||
|
*
|
||||||
* @param packageName
|
* @param packageName
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public static String package2Path(String packageName){
|
public static String package2Path(String packageName) {
|
||||||
return "src/main/java/" + packageName.replace(".", "/");
|
return "src/main/java/" + packageName.replace(".", "/");
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String formatTableName(String str){
|
public static String formatTableName(String str) {
|
||||||
//todo 待优化
|
//todo 待优化
|
||||||
return str.replaceFirst(CONFIG.getTablePrefix(),"");
|
return str.replaceFirst(CONFIG.getTablePrefix(), "");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,9 +1,10 @@
|
|||||||
package ${package};
|
package ${package};
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Builder;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
import javax.persistence.Entity;
|
|
||||||
import javax.persistence.Table;
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -13,8 +14,9 @@ import java.io.Serializable;
|
|||||||
* @date ${.now?datetime}
|
* @date ${.now?datetime}
|
||||||
*/
|
*/
|
||||||
@Data
|
@Data
|
||||||
@Table(name = "${table_name}")
|
@AllArgsConstructor
|
||||||
@Entity(name = "${table_name}")
|
@NoArgsConstructor
|
||||||
|
@Builder
|
||||||
public class ${class_name} implements Serializable {
|
public class ${class_name} implements Serializable {
|
||||||
<#list columns as column>
|
<#list columns as column>
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user