|
|
|
@ -0,0 +1,473 @@
|
|
|
|
|
package com.dsideal.Tools;
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.io.FileUtil;
|
|
|
|
|
import cn.hutool.json.JSONObject;
|
|
|
|
|
import com.dsideal.Utils.dsKit;
|
|
|
|
|
import com.jfinal.kit.PropKit;
|
|
|
|
|
import com.jfinal.kit.StrKit;
|
|
|
|
|
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
|
|
|
|
|
import com.jfinal.plugin.hikaricp.HikariCpPlugin;
|
|
|
|
|
|
|
|
|
|
import javax.sql.DataSource;
|
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.FileWriter;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.io.Writer;
|
|
|
|
|
import java.sql.Connection;
|
|
|
|
|
import java.sql.ResultSet;
|
|
|
|
|
import java.sql.Statement;
|
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
|
|
import com.jfinal.plugin.activerecord.Record;
|
|
|
|
|
import freemarker.template.Configuration;
|
|
|
|
|
import freemarker.template.Template;
|
|
|
|
|
import freemarker.template.TemplateException;
|
|
|
|
|
import freemarker.template.Version;
|
|
|
|
|
|
|
|
|
|
public class GenerateCodeWithFreeMaker {
|
|
|
|
|
|
|
|
|
|
// JavaBean保存的位置
|
|
|
|
|
public static String beanPath;
|
|
|
|
|
|
|
|
|
|
// mysql与java的数据类型映射
|
|
|
|
|
public static Map<String, String> dataTypeMap = new HashMap<>();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取当前数据库下的表
|
|
|
|
|
*
|
|
|
|
|
* @return List<String>
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public static List<String> getTableNames(Connection conn) throws Exception {
|
|
|
|
|
List<String> tables = new ArrayList<>();
|
|
|
|
|
Statement stmt = conn.createStatement();
|
|
|
|
|
ResultSet rs = stmt.executeQuery("show tables");
|
|
|
|
|
while (rs.next()) {
|
|
|
|
|
String tableName = rs.getString(1);
|
|
|
|
|
if (tableName.equals(PropKit.get("tableName"))) {
|
|
|
|
|
tables.add(tableName);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
rs.close();
|
|
|
|
|
stmt.close();
|
|
|
|
|
return tables;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获得某表的注释信息+列信息
|
|
|
|
|
*
|
|
|
|
|
* @param table
|
|
|
|
|
* @return
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public static JSONObject getStructure(Connection conn, String table) throws Exception {
|
|
|
|
|
//表结构的描述JSON对象
|
|
|
|
|
JSONObject jo = new JSONObject();
|
|
|
|
|
//字段,不包含主键
|
|
|
|
|
Map<String, String> dataTypeMap = new LinkedHashMap<>();
|
|
|
|
|
//字段描述
|
|
|
|
|
Map<String, String> commentMap = new LinkedHashMap<>();
|
|
|
|
|
|
|
|
|
|
//表注释
|
|
|
|
|
String comment = "";
|
|
|
|
|
Statement stmt = conn.createStatement();
|
|
|
|
|
ResultSet rs = stmt.executeQuery("show create table " + table);
|
|
|
|
|
if (rs != null && rs.next()) {
|
|
|
|
|
String createDDL = rs.getString(2);
|
|
|
|
|
int index = createDDL.indexOf("COMMENT='");
|
|
|
|
|
if (index >= 0) {
|
|
|
|
|
comment = createDDL.substring(index + 9);
|
|
|
|
|
comment = comment.substring(0, comment.length() - 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//类名
|
|
|
|
|
String beanNameWithoutT = dsKit.toCamelCase(dsKit.capitalizeFirstLetter(table));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
List<String> fields = new ArrayList<>();
|
|
|
|
|
//字段信息
|
|
|
|
|
rs = stmt.executeQuery("show full columns from " + table);
|
|
|
|
|
while (rs.next()) {
|
|
|
|
|
String Type = rs.getString("Type");
|
|
|
|
|
String Field = rs.getString("Field");
|
|
|
|
|
String Comment = rs.getString("Comment");
|
|
|
|
|
commentMap.put(Field, Comment);
|
|
|
|
|
fields.add(Field);
|
|
|
|
|
|
|
|
|
|
if (Type.contains("(")) {
|
|
|
|
|
Type = Type.substring(0, Type.indexOf("("));
|
|
|
|
|
}
|
|
|
|
|
Type = Type.toUpperCase();
|
|
|
|
|
|
|
|
|
|
//记录主键
|
|
|
|
|
if (rs.getString("Key").equals("PRI")) {
|
|
|
|
|
jo.put("key", Field);
|
|
|
|
|
jo.put("key_type", GenerateCodeWithFreeMaker.dataTypeMap.get(Type));
|
|
|
|
|
} else {//否则记录到map中
|
|
|
|
|
dataTypeMap.put(Field, GenerateCodeWithFreeMaker.dataTypeMap.get(Type));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//关闭连接
|
|
|
|
|
rs.close();
|
|
|
|
|
stmt.close();
|
|
|
|
|
jo.put("fields", dataTypeMap);
|
|
|
|
|
jo.put("fieldsComment", commentMap);
|
|
|
|
|
jo.put("beanNameWithoutT", beanNameWithoutT);
|
|
|
|
|
jo.put("comment", comment);
|
|
|
|
|
jo.put("table", table);
|
|
|
|
|
return jo;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 功能:初始化数据库类型映射
|
|
|
|
|
*/
|
|
|
|
|
public static void initMap() {
|
|
|
|
|
//mysql与java的数据类型映射
|
|
|
|
|
dataTypeMap.put("INT", "int");
|
|
|
|
|
dataTypeMap.put("BIGINT", "long");
|
|
|
|
|
dataTypeMap.put("DOUBLE", "double");
|
|
|
|
|
dataTypeMap.put("FLOAT", "float");
|
|
|
|
|
dataTypeMap.put("TINYINT", "int");
|
|
|
|
|
dataTypeMap.put("DATE", "java.util.Date");
|
|
|
|
|
dataTypeMap.put("TIMESTAMP", "java.util.Date");
|
|
|
|
|
dataTypeMap.put("BIGINT UNSIGNED", "long");
|
|
|
|
|
dataTypeMap.put("INT UNSIGNED", "long");
|
|
|
|
|
dataTypeMap.put("MEDIUMINT", "int");
|
|
|
|
|
dataTypeMap.put("MEDIUMINT UNSIGNED", "long");
|
|
|
|
|
dataTypeMap.put("SMALLINT", "int");
|
|
|
|
|
dataTypeMap.put("SMALLINT UNSIGNED", "long");
|
|
|
|
|
dataTypeMap.put("TINYINT UNSIGNED", "long");
|
|
|
|
|
dataTypeMap.put("LONGTEXT", "String");
|
|
|
|
|
dataTypeMap.put("VARCHAR", "String");
|
|
|
|
|
dataTypeMap.put("CHAR", "String");
|
|
|
|
|
dataTypeMap.put("TEXT", "String");
|
|
|
|
|
dataTypeMap.put("DATETIME", "java.util.Date");
|
|
|
|
|
dataTypeMap.put("DECIMAL", "BigDecimal");
|
|
|
|
|
dataTypeMap.put("BOOLEAN", "boolean");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) throws Exception {
|
|
|
|
|
//加载配置文件
|
|
|
|
|
PropKit.use("application.properties");
|
|
|
|
|
beanPath = PropKit.get("beanPath");
|
|
|
|
|
if (!FileUtil.exist(beanPath)) {
|
|
|
|
|
FileUtil.mkdir(beanPath);
|
|
|
|
|
}
|
|
|
|
|
initMap();
|
|
|
|
|
|
|
|
|
|
// 配置Druid数据源插件
|
|
|
|
|
HikariCpPlugin hpPlugin = new HikariCpPlugin(PropKit.get("jdbcUrl"), PropKit.get("user"),
|
|
|
|
|
PropKit.get("password").trim(), PropKit.get("driverClassName"));
|
|
|
|
|
hpPlugin.start();
|
|
|
|
|
|
|
|
|
|
// 配置ActiveRecord插件
|
|
|
|
|
ActiveRecordPlugin arp = new ActiveRecordPlugin(hpPlugin);
|
|
|
|
|
hpPlugin.start();
|
|
|
|
|
arp.start();
|
|
|
|
|
DataSource dataSource = hpPlugin.getDataSource();
|
|
|
|
|
// 数据库连接
|
|
|
|
|
Connection conn = dataSource.getConnection();
|
|
|
|
|
|
|
|
|
|
//开始生成
|
|
|
|
|
List<String> tables = getTableNames(conn);
|
|
|
|
|
|
|
|
|
|
String tableNames = "";
|
|
|
|
|
//表描述
|
|
|
|
|
for (String table : tables) {
|
|
|
|
|
JSONObject jo = getStructure(conn, table);
|
|
|
|
|
tableNames += table + ",";
|
|
|
|
|
|
|
|
|
|
if (StrKit.isBlank(jo.getStr("key"))) {
|
|
|
|
|
dsKit.print("表" + table + "没有主键,请检查!");
|
|
|
|
|
}
|
|
|
|
|
//生成代码
|
|
|
|
|
autoCodeFreeMaker(jo);
|
|
|
|
|
}
|
|
|
|
|
if (StrKit.isBlank(tableNames)) {
|
|
|
|
|
dsKit.print("没有找到表,请检查!");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
//去掉最后的逗号
|
|
|
|
|
tableNames = tableNames.substring(0, tableNames.length() - 1);
|
|
|
|
|
|
|
|
|
|
//关闭数据库
|
|
|
|
|
conn.close();
|
|
|
|
|
arp.stop();
|
|
|
|
|
hpPlugin.stop();
|
|
|
|
|
|
|
|
|
|
System.out.println(dsKit.getCurrentTimeStr() + " 恭喜,表" + tableNames + "代码已成功生成!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 功能:生成代码
|
|
|
|
|
*
|
|
|
|
|
* @param jo
|
|
|
|
|
*/
|
|
|
|
|
public static void autoCode(JSONObject jo) {
|
|
|
|
|
//不带主键的其它字段,拼接成java的函数声明内容
|
|
|
|
|
// 迭代map将按照插入顺序
|
|
|
|
|
Map<String, String> fields = (Map<String, String>) jo.get("fields");
|
|
|
|
|
Map<String, String> fieldsComment = (Map<String, String>) jo.get("fieldsComment");
|
|
|
|
|
|
|
|
|
|
String fullParameters = "";//带数据类型+字段名称
|
|
|
|
|
String parameters = "";//不带数据类型,只有字段名称
|
|
|
|
|
for (Map.Entry<String, String> entry : fields.entrySet()) {
|
|
|
|
|
if (!entry.getKey().equals("create_time") && !entry.getKey().equals("b_use")) {
|
|
|
|
|
fullParameters += entry.getValue() + " " + entry.getKey() + " , ";//这里必须是反着来的,否则数据类型会重复
|
|
|
|
|
parameters += entry.getKey() + " , ";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//移除最后一个逗号
|
|
|
|
|
fullParameters = fullParameters.substring(0, fullParameters.length() - 1);
|
|
|
|
|
if (fullParameters.endsWith(",")) {//去掉最后的逗号
|
|
|
|
|
fullParameters = fullParameters.substring(0, fullParameters.length() - 1);
|
|
|
|
|
}
|
|
|
|
|
parameters = parameters.substring(0, parameters.length() - 1);
|
|
|
|
|
if (parameters.endsWith(",")) {
|
|
|
|
|
parameters = parameters.substring(0, parameters.length() - 1);
|
|
|
|
|
}
|
|
|
|
|
//后缀名称
|
|
|
|
|
String beanNameWithoutT = jo.getStr("beanNameWithoutT");
|
|
|
|
|
if (beanNameWithoutT.startsWith("T")) {
|
|
|
|
|
beanNameWithoutT = beanNameWithoutT.substring(1);
|
|
|
|
|
}
|
|
|
|
|
//表的描述
|
|
|
|
|
String comment = jo.getStr("comment");
|
|
|
|
|
|
|
|
|
|
//主键
|
|
|
|
|
String key = jo.getStr("key");
|
|
|
|
|
String key_type = jo.getStr("key_type");
|
|
|
|
|
//表名
|
|
|
|
|
String table = jo.getStr("table");
|
|
|
|
|
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
|
//sb.append("//-------------------下面是Controller部分-------------------\n");
|
|
|
|
|
sb.append("/* 下面代码开始维护" + comment + " */\n");
|
|
|
|
|
sb.append("\n");
|
|
|
|
|
//1、增加
|
|
|
|
|
sb.append(" /**增加" + comment + "\n");
|
|
|
|
|
sb.append(" *\n");
|
|
|
|
|
//迭代
|
|
|
|
|
for (Map.Entry<String, String> entry : fields.entrySet()) {
|
|
|
|
|
if (!entry.getKey().equals("create_time")) {
|
|
|
|
|
sb.append(" * @param " + entry.getKey() + " " + fieldsComment.get(entry.getKey()) + "\n");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
sb.append(" */\n");
|
|
|
|
|
sb.append(" @Before({POST.class})\n");
|
|
|
|
|
sb.append(" @JwtCheckInterface({})\n");
|
|
|
|
|
sb.append(" public void add" + beanNameWithoutT + "(" + fullParameters + "){\n");
|
|
|
|
|
sb.append(" " + PropKit.get("daoName") + ".add" + beanNameWithoutT + "(" + parameters + ");\n");
|
|
|
|
|
sb.append(" renderJson(RetKit.renderSuccess());\n");
|
|
|
|
|
sb.append(" }\n");
|
|
|
|
|
|
|
|
|
|
//2、删除
|
|
|
|
|
sb.append("\n");
|
|
|
|
|
sb.append(" //按ID删除" + comment + "\n");
|
|
|
|
|
sb.append(" @Before({POST.class})\n");
|
|
|
|
|
sb.append(" @JwtCheckInterface({})\n");
|
|
|
|
|
sb.append(" @IsNumericInterface({\"" + key + "\"})\n");
|
|
|
|
|
sb.append(" public void del" + beanNameWithoutT + "ById(" + key_type + " " + key + "){\n");
|
|
|
|
|
sb.append(" " + PropKit.get("daoName") + ".del" + beanNameWithoutT + "ById(" + key + ");\n");
|
|
|
|
|
sb.append(" renderJson(RetKit.renderSuccess());\n");
|
|
|
|
|
sb.append(" }\n");
|
|
|
|
|
|
|
|
|
|
//3、修改
|
|
|
|
|
sb.append("\n");
|
|
|
|
|
sb.append(" /**按ID修改" + comment + "\n");
|
|
|
|
|
sb.append(" *\n");
|
|
|
|
|
//迭代
|
|
|
|
|
for (Map.Entry<String, String> entry : fields.entrySet()) {
|
|
|
|
|
if (!entry.getKey().equals("create_time")) {
|
|
|
|
|
sb.append(" * @param " + entry.getKey() + " " + fieldsComment.get(entry.getKey()) + "\n");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
sb.append(" */\n");
|
|
|
|
|
sb.append(" @Before({POST.class})\n");
|
|
|
|
|
sb.append(" @JwtCheckInterface({})\n");
|
|
|
|
|
sb.append(" //@IsNumericInterface({\"" + key + "\"})\n");
|
|
|
|
|
sb.append(" public void update" + beanNameWithoutT + "ById(" + key_type + " " + key + "," + fullParameters + "){\n");
|
|
|
|
|
sb.append(" " + PropKit.get("daoName") + ".update" + beanNameWithoutT + "ById(" + key + "," + parameters + ");\n");
|
|
|
|
|
sb.append(" renderJson(RetKit.renderSuccess());\n");
|
|
|
|
|
sb.append(" }\n");
|
|
|
|
|
sb.append("\n");
|
|
|
|
|
|
|
|
|
|
//4、单条查询
|
|
|
|
|
sb.append(" //按ID查询" + comment + "\n");
|
|
|
|
|
sb.append(" @Before({GET.class})\n");
|
|
|
|
|
sb.append(" @JwtCheckInterface({})\n");
|
|
|
|
|
sb.append(" @IsNumericInterface({\"" + key + "\"})\n");
|
|
|
|
|
sb.append(" public void get" + beanNameWithoutT + "ById(" + key_type + " " + key + "){\n");
|
|
|
|
|
sb.append(" renderJson(RetKit.renderSuccess(" + PropKit.get("daoName") + ".get" + beanNameWithoutT + "ById(" + key + ")));\n");
|
|
|
|
|
sb.append(" }\n");
|
|
|
|
|
sb.append("\n");
|
|
|
|
|
|
|
|
|
|
//5、分页查询
|
|
|
|
|
sb.append(" //分页查询" + comment + "\n");
|
|
|
|
|
sb.append(" @Before({GET.class})\n");
|
|
|
|
|
sb.append(" @JwtCheckInterface({})\n");
|
|
|
|
|
sb.append(" public void get" + beanNameWithoutT + "List(int pageNum, int pageSize){\n");
|
|
|
|
|
sb.append(" Page<Record> listPage = " + PropKit.get("daoName") + ".get" + beanNameWithoutT + "List(pageNum, pageSize);\n");
|
|
|
|
|
sb.append(" renderJson(RetKit.renderSuccess(listPage));\n");
|
|
|
|
|
sb.append(" }\n");
|
|
|
|
|
sb.append("\n");
|
|
|
|
|
|
|
|
|
|
FileUtil.writeUtf8String(sb.toString(), beanPath + "/" + beanNameWithoutT + "Controller.txt");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sb.append("/* 下面代码开始维护" + comment + " */\n");
|
|
|
|
|
sb.append("\n");
|
|
|
|
|
sb = new StringBuilder();
|
|
|
|
|
//1、增加
|
|
|
|
|
sb.append(" /**增加" + comment + "\n");
|
|
|
|
|
sb.append(" *\n");
|
|
|
|
|
//迭代
|
|
|
|
|
for (Map.Entry<String, String> entry : fields.entrySet()) {
|
|
|
|
|
if (!entry.getKey().equals("create_time")) {
|
|
|
|
|
sb.append(" * @param " + entry.getKey() + " " + fieldsComment.get(entry.getKey()) + "\n");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
sb.append(" */\n");
|
|
|
|
|
sb.append(" public void add" + beanNameWithoutT + "(" + fullParameters + "){\n");
|
|
|
|
|
sb.append(" Record record = new Record();\n");
|
|
|
|
|
for (Map.Entry<String, String> entry : fields.entrySet()) {
|
|
|
|
|
if (!entry.getKey().equals("create_time")) {
|
|
|
|
|
sb.append(" record.set(\"" + entry.getKey() + "\"," + entry.getKey() + ");\n");
|
|
|
|
|
} else {
|
|
|
|
|
sb.append(" record.set(\"" + entry.getKey() + "\", DateTime.now());\n");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
sb.append(" Db.save(\"" + table + "\",\"" + key + "\",record);\n");
|
|
|
|
|
sb.append("}\n");
|
|
|
|
|
//2、删除
|
|
|
|
|
sb.append("\n");
|
|
|
|
|
sb.append(" //删除" + comment + "\n");
|
|
|
|
|
sb.append(" public void del" + beanNameWithoutT + "ById(" + key_type + " " + key + "){\n");
|
|
|
|
|
sb.append(" String sql=\"delete from " + table + " where " + key + "=?\";\n");
|
|
|
|
|
sb.append(" Db.update(sql," + key + ");\n");
|
|
|
|
|
sb.append(" }\n");
|
|
|
|
|
sb.append("\n");
|
|
|
|
|
|
|
|
|
|
//3、修改
|
|
|
|
|
sb.append(" /**修改" + comment + "\n");
|
|
|
|
|
sb.append(" *\n");
|
|
|
|
|
//迭代
|
|
|
|
|
for (Map.Entry<String, String> entry : fields.entrySet()) {
|
|
|
|
|
if (!entry.getKey().equals("create_time")) {
|
|
|
|
|
sb.append(" * @param " + entry.getKey() + " " + fieldsComment.get(entry.getKey()) + "\n");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
sb.append(" */\n");
|
|
|
|
|
|
|
|
|
|
sb.append(" public void update" + beanNameWithoutT + "ById(" + key_type + " " + key + "," + fullParameters + "){\n");
|
|
|
|
|
sb.append(" String sql=\"update " + table + " set ");
|
|
|
|
|
for (Map.Entry<String, String> entry : fields.entrySet()) {
|
|
|
|
|
if (!entry.getKey().equals("create_time")) {
|
|
|
|
|
sb.append(entry.getKey() + "= ?,");
|
|
|
|
|
} else {
|
|
|
|
|
sb.append(entry.getKey() + "= DateTime.now(),");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
sb = sb.deleteCharAt(sb.length() - 1);
|
|
|
|
|
sb.append(" where " + key + "=?\";\n");
|
|
|
|
|
sb.append(" Db.update(sql,");
|
|
|
|
|
for (Map.Entry<String, String> entry : fields.entrySet()) {
|
|
|
|
|
if (!entry.getKey().equals("create_time")) {
|
|
|
|
|
sb.append(entry.getKey() + " , ");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
sb.append(key);
|
|
|
|
|
sb.append(");\n");
|
|
|
|
|
sb.append(" }\n");
|
|
|
|
|
//4、单条查询
|
|
|
|
|
sb.append("\n");
|
|
|
|
|
sb.append(" //单条查询" + comment + "\n");
|
|
|
|
|
sb.append(" public Record get" + beanNameWithoutT + "ById(" + key_type + " " + key + "){\n");
|
|
|
|
|
sb.append(" String sql=\"select * from " + table + " where " + key + "=?\";\n");
|
|
|
|
|
sb.append(" return Db.findFirst(sql," + key + ");\n");
|
|
|
|
|
sb.append(" }\n");
|
|
|
|
|
sb.append("\n");
|
|
|
|
|
|
|
|
|
|
//5、分页查询
|
|
|
|
|
sb.append(" //分页查询" + comment + "\n");
|
|
|
|
|
sb.append(" public Page<Record> get" + beanNameWithoutT + "List(int pageNum, int pageSize){\n");
|
|
|
|
|
sb.append(" String sql1=\"select * \";\n");
|
|
|
|
|
sb.append(" String sql2=\" from " + table + " \";\n");
|
|
|
|
|
sb.append(" return Db.paginate(pageNum, pageSize, sql1,sql2);\n");
|
|
|
|
|
sb.append(" }\n");
|
|
|
|
|
sb.append("\n");
|
|
|
|
|
FileUtil.writeUtf8String(sb.toString(), beanPath + "/" + beanNameWithoutT + "Model.txt");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void autoCodeFreeMaker(JSONObject jo) throws IOException, TemplateException {
|
|
|
|
|
//不带主键的其它字段,拼接成java的函数声明内容
|
|
|
|
|
// 迭代map将按照插入顺序
|
|
|
|
|
Map<String, String> fields = (Map<String, String>) jo.get("fields");
|
|
|
|
|
Map<String, String> fieldsComment = (Map<String, String>) jo.get("fieldsComment");
|
|
|
|
|
|
|
|
|
|
String fullParameters = "";//带数据类型+字段名称
|
|
|
|
|
String parameters = "";//不带数据类型,只有字段名称
|
|
|
|
|
for (Map.Entry<String, String> entry : fields.entrySet()) {
|
|
|
|
|
if (!entry.getKey().equals("create_time") && !entry.getKey().equals("b_use")) {
|
|
|
|
|
fullParameters += entry.getValue() + " " + entry.getKey() + " , ";//这里必须是反着来的,否则数据类型会重复
|
|
|
|
|
parameters += entry.getKey() + " , ";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//移除最后一个逗号
|
|
|
|
|
fullParameters = fullParameters.substring(0, fullParameters.length() - 1);
|
|
|
|
|
if (fullParameters.endsWith(",")) {//去掉最后的逗号
|
|
|
|
|
fullParameters = fullParameters.substring(0, fullParameters.length() - 1);
|
|
|
|
|
}
|
|
|
|
|
parameters = parameters.substring(0, parameters.length() - 1);
|
|
|
|
|
if (parameters.endsWith(",")) {
|
|
|
|
|
parameters = parameters.substring(0, parameters.length() - 1);
|
|
|
|
|
}
|
|
|
|
|
//后缀名称
|
|
|
|
|
String beanNameWithoutT = jo.getStr("beanNameWithoutT");
|
|
|
|
|
if (beanNameWithoutT.startsWith("T")) {
|
|
|
|
|
beanNameWithoutT = beanNameWithoutT.substring(1);
|
|
|
|
|
}
|
|
|
|
|
//表的描述
|
|
|
|
|
String comment = jo.getStr("comment");
|
|
|
|
|
//主键
|
|
|
|
|
String key = jo.getStr("key");
|
|
|
|
|
String key_type = jo.getStr("key_type");
|
|
|
|
|
//表名
|
|
|
|
|
String table = jo.getStr("table");
|
|
|
|
|
|
|
|
|
|
Map<String, Object> dataModel = new HashMap<>();
|
|
|
|
|
dataModel.put("comment", comment);
|
|
|
|
|
List<Record> params = new ArrayList<>();
|
|
|
|
|
//迭代
|
|
|
|
|
for (Map.Entry<String, String> entry : fields.entrySet()) {
|
|
|
|
|
if (!entry.getKey().equals("create_time")) {
|
|
|
|
|
Record record = new Record();
|
|
|
|
|
record.set("key", entry.getKey());
|
|
|
|
|
record.set("comment", fieldsComment.get(entry.getKey()));
|
|
|
|
|
params.add(record);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
dataModel.put("params", params);
|
|
|
|
|
dataModel.put("daoName", PropKit.get("daoName"));
|
|
|
|
|
dataModel.put("beanNameWithoutT", beanNameWithoutT);
|
|
|
|
|
dataModel.put("fullParameters", fullParameters);
|
|
|
|
|
dataModel.put("parameters", parameters);
|
|
|
|
|
|
|
|
|
|
// 创建一个 Configuration 实例
|
|
|
|
|
Configuration cfg = new Configuration(new Version(2, 3, 33));
|
|
|
|
|
// 设置模板目录
|
|
|
|
|
String path = System.getProperty("user.dir") + "/dsUtils/src/main/resources";
|
|
|
|
|
cfg.setDirectoryForTemplateLoading(new File(path));
|
|
|
|
|
// 设置字符编码
|
|
|
|
|
cfg.setDefaultEncoding("UTF-8");
|
|
|
|
|
// 加载模板
|
|
|
|
|
Template template = cfg.getTemplate("Controller.ftl");
|
|
|
|
|
|
|
|
|
|
// 渲染模板到文件
|
|
|
|
|
Writer out = new FileWriter("c:/output_users.txt");
|
|
|
|
|
template.process(dataModel, out);
|
|
|
|
|
out.close();
|
|
|
|
|
}
|
|
|
|
|
}
|