|
|
|
@ -1,8 +1,10 @@
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
@ -50,9 +52,13 @@ public class GenerateCode {
|
|
|
|
|
* @return
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public static void getStructure(Connection conn, String table) throws Exception {
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
|
public static JSONObject getStructure(Connection conn, String table) throws Exception {
|
|
|
|
|
//表结构的描述JSON对象
|
|
|
|
|
JSONObject jo = new JSONObject();
|
|
|
|
|
//字段,不包含主键
|
|
|
|
|
Map<String, String> map = new LinkedHashMap<>();
|
|
|
|
|
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
|
//表注释
|
|
|
|
|
String comment = "";
|
|
|
|
|
Statement stmt = conn.createStatement();
|
|
|
|
@ -93,7 +99,13 @@ public class GenerateCode {
|
|
|
|
|
}
|
|
|
|
|
Type = Type.toUpperCase();
|
|
|
|
|
|
|
|
|
|
// System.out.println(rs.getString("Key"));//是不是主键 PRI是主键
|
|
|
|
|
//记录主键
|
|
|
|
|
if (rs.getString("Key").equals("PRI")) {
|
|
|
|
|
jo.put("key", Field);
|
|
|
|
|
} else {//否则记录到map中
|
|
|
|
|
map.put(Field, dataTypeMap.get(Type));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sb.append(" // " + rs.getString("Comment") + "\n");
|
|
|
|
|
sb.append(" @Getter\n");
|
|
|
|
|
sb.append(" @Setter\n");
|
|
|
|
@ -118,6 +130,8 @@ public class GenerateCode {
|
|
|
|
|
//关闭连接
|
|
|
|
|
rs.close();
|
|
|
|
|
stmt.close();
|
|
|
|
|
jo.put("fields", map);
|
|
|
|
|
return jo;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -178,8 +192,14 @@ public class GenerateCode {
|
|
|
|
|
String tableNames = "";
|
|
|
|
|
//表描述
|
|
|
|
|
for (String table : tables) {
|
|
|
|
|
getStructure(conn, table);
|
|
|
|
|
JSONObject jo = getStructure(conn, table);
|
|
|
|
|
tableNames += table + ",";
|
|
|
|
|
|
|
|
|
|
if (StrKit.isBlank(jo.getStr("key"))) {
|
|
|
|
|
dsKit.print("表" + table + "没有主键,请检查!");
|
|
|
|
|
}
|
|
|
|
|
//生成代码
|
|
|
|
|
autoCode(jo);
|
|
|
|
|
}
|
|
|
|
|
//去掉最后的逗号
|
|
|
|
|
tableNames = tableNames.substring(0, tableNames.length() - 1);
|
|
|
|
@ -191,4 +211,12 @@ public class GenerateCode {
|
|
|
|
|
|
|
|
|
|
System.out.println(dsKit.getCurrentTimeStr() + " 恭喜,表" + tableNames + "代码已成功生成!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 功能:生成代码
|
|
|
|
|
* @param jo
|
|
|
|
|
*/
|
|
|
|
|
public void autoCode(JSONObject jo){
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|