|
|
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.sql.*;
|
|
|
import java.util.*;
|
|
|
|
|
|
public class GenerateCode {
|
|
|
|
|
|
// JavaBean保存的位置
|
|
|
public static String beanPath;
|
|
|
// JavaBean包名
|
|
|
public static String beanPackage;
|
|
|
|
|
|
// 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> map = 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");
|
|
|
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", dataTypeMap.get(Type));
|
|
|
} else {//否则记录到map中
|
|
|
map.put(Field, dataTypeMap.get(Type));
|
|
|
}
|
|
|
}
|
|
|
//关闭连接
|
|
|
rs.close();
|
|
|
stmt.close();
|
|
|
jo.put("fields", map);
|
|
|
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");
|
|
|
beanPackage = PropKit.get("beanPackage");
|
|
|
beanPath = PropKit.get("beanPath");
|
|
|
beanPath = beanPath + "/" + beanPackage.replace(".", "/");
|
|
|
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 + "没有主键,请检查!");
|
|
|
}
|
|
|
//生成代码
|
|
|
autoCode(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");
|
|
|
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);
|
|
|
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("\n");
|
|
|
//1、增加
|
|
|
sb.append(" //增加" + comment + "\n");
|
|
|
sb.append(" @Before({POST.class})\n");
|
|
|
sb.append(" //@JwtCheckInterface({})\n");
|
|
|
sb.append(" //@EmptyInterface({\"\"})\n");
|
|
|
sb.append(" //@IsNumericInterface({})\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(" //@EmptyInterface({\"\"})\n");
|
|
|
sb.append(" //@IsNumericInterface({\"id\"})\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(" @Before({POST.class})\n");
|
|
|
sb.append(" //@JwtCheckInterface({})\n");
|
|
|
sb.append(" //@EmptyInterface({\"\"})\n");
|
|
|
sb.append(" //@IsNumericInterface({\"id\"})\n");
|
|
|
sb.append(" public void update" + beanNameWithoutT + "ById(" + key_type + " " + key + "," + fullParameters + "){\n");
|
|
|
sb.append(" " + PropKit.get("daoName") + ".update" + beanNameWithoutT + "ById(" + 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(" //@EmptyInterface({\"\"})\n");
|
|
|
sb.append(" //@IsNumericInterface({\"id\"})\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(" //@EmptyInterface({\"\"})\n");
|
|
|
sb.append(" public void get" + beanNameWithoutT + "List(int pageNum, int pageSize){\n");
|
|
|
sb.append(" Page<Record> listPage = " + PropKit.get("daoName") + ".get" + beanNameWithoutT + "List(stage_id, pageNum, pageSize);\n");
|
|
|
sb.append(" renderJson(RetKit.renderSuccess(listPage));\n");
|
|
|
sb.append(" }\n");
|
|
|
sb.append("\n");
|
|
|
|
|
|
sb.append("-------------------下面是Model部分-------------------\n");
|
|
|
sb.append("\n");
|
|
|
|
|
|
//1、增加
|
|
|
sb.append("public boolean add" + beanNameWithoutT + "(" + fullParameters + "){\n");
|
|
|
sb.append(" Record record = new Record();\n");
|
|
|
for (Map.Entry<String, String> entry : fields.entrySet()) {
|
|
|
sb.append(" record.set(\"" + entry.getKey() + "\"," +entry.getKey() + ");\n");
|
|
|
}
|
|
|
sb.append(" Db.save(\""+table+"\",\""+key+"\",record);\n");
|
|
|
sb.append("}\n");
|
|
|
//2、删除
|
|
|
|
|
|
//3、修改
|
|
|
|
|
|
//4、单条查询
|
|
|
|
|
|
//5、分页查询
|
|
|
|
|
|
System.out.println(sb);
|
|
|
|
|
|
System.out.println(beanPath + "/" + beanNameWithoutT + ".txt");
|
|
|
FileUtil.writeUtf8String(sb.toString(), beanPath + "/" + beanNameWithoutT + ".txt");
|
|
|
}
|
|
|
}
|