|
|
|
@ -34,7 +34,6 @@ public class GenerateCode {
|
|
|
|
|
* 获取当前数据库下的表
|
|
|
|
|
*
|
|
|
|
|
* @return List<String>
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public static List<String> getTableNames(Connection conn) throws Exception {
|
|
|
|
|
List<String> tables = new ArrayList<>();
|
|
|
|
@ -55,9 +54,8 @@ public class GenerateCode {
|
|
|
|
|
/**
|
|
|
|
|
* 获得某表的注释信息+列信息
|
|
|
|
|
*
|
|
|
|
|
* @param table
|
|
|
|
|
* @return
|
|
|
|
|
* @throws Exception
|
|
|
|
|
* @param table 表
|
|
|
|
|
* @return 对象
|
|
|
|
|
*/
|
|
|
|
|
public static JSONObject getStructure(Connection conn, String table) throws Exception {
|
|
|
|
|
//表结构的描述JSON对象
|
|
|
|
@ -81,7 +79,7 @@ public class GenerateCode {
|
|
|
|
|
}
|
|
|
|
|
//类名
|
|
|
|
|
String beanNameWithoutT = dsKit.toCamelCase(dsKit.capitalizeFirstLetter(table));
|
|
|
|
|
List<String> fields = new ArrayList<>();
|
|
|
|
|
//List<String> fields = new ArrayList<>();
|
|
|
|
|
//字段信息
|
|
|
|
|
rs = stmt.executeQuery("show full columns from " + table);
|
|
|
|
|
while (rs.next()) {
|
|
|
|
@ -89,7 +87,7 @@ public class GenerateCode {
|
|
|
|
|
String Field = rs.getString("Field");
|
|
|
|
|
String Comment = rs.getString("Comment");
|
|
|
|
|
commentMap.put(Field, Comment);
|
|
|
|
|
fields.add(Field);
|
|
|
|
|
//fields.add(Field);
|
|
|
|
|
|
|
|
|
|
if (Type.contains("(")) {
|
|
|
|
|
Type = Type.substring(0, Type.indexOf("("));
|
|
|
|
@ -201,22 +199,22 @@ public class GenerateCode {
|
|
|
|
|
Map<String, String> fields = (Map<String, String>) jo.get("fields");
|
|
|
|
|
Map<String, String> fieldsComment = (Map<String, String>) jo.get("fieldsComment");
|
|
|
|
|
|
|
|
|
|
String parasWithoutPrimaryKey = "";//带数据类型+字段名称
|
|
|
|
|
String parasWithoutPrimaryKeyAndDataType = "";//不带数据类型,只有字段名称
|
|
|
|
|
StringBuilder parasWithoutPrimaryKey = new StringBuilder();//带数据类型+字段名称
|
|
|
|
|
StringBuilder parasWithoutPrimaryKeyAndDataType = new StringBuilder();//不带数据类型,只有字段名称
|
|
|
|
|
for (Map.Entry<String, String> entry : fields.entrySet()) {
|
|
|
|
|
if (!entry.getKey().equals("create_time") && !entry.getKey().equals("b_use")) {
|
|
|
|
|
parasWithoutPrimaryKey += entry.getValue() + " " + entry.getKey() + " , ";//这里必须是反着来的,否则数据类型会重复
|
|
|
|
|
parasWithoutPrimaryKeyAndDataType += entry.getKey() + " , ";
|
|
|
|
|
parasWithoutPrimaryKey.append(entry.getValue()).append(" ").append(entry.getKey()).append(" , ");//这里必须是反着来的,否则数据类型会重复
|
|
|
|
|
parasWithoutPrimaryKeyAndDataType.append(entry.getKey()).append(" , ");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//移除最后一个逗号
|
|
|
|
|
parasWithoutPrimaryKey = parasWithoutPrimaryKey.substring(0, parasWithoutPrimaryKey.length() - 1);
|
|
|
|
|
if (parasWithoutPrimaryKey.endsWith(",")) {//去掉最后的逗号
|
|
|
|
|
parasWithoutPrimaryKey = parasWithoutPrimaryKey.substring(0, parasWithoutPrimaryKey.length() - 1);
|
|
|
|
|
parasWithoutPrimaryKey = new StringBuilder(parasWithoutPrimaryKey.substring(0, parasWithoutPrimaryKey.length() - 1));
|
|
|
|
|
if (parasWithoutPrimaryKey.toString().endsWith(",")) {//去掉最后的逗号
|
|
|
|
|
parasWithoutPrimaryKey = new StringBuilder(parasWithoutPrimaryKey.substring(0, parasWithoutPrimaryKey.length() - 1));
|
|
|
|
|
}
|
|
|
|
|
parasWithoutPrimaryKeyAndDataType = parasWithoutPrimaryKeyAndDataType.substring(0, parasWithoutPrimaryKeyAndDataType.length() - 1);
|
|
|
|
|
if (parasWithoutPrimaryKeyAndDataType.endsWith(",")) {
|
|
|
|
|
parasWithoutPrimaryKeyAndDataType = parasWithoutPrimaryKeyAndDataType.substring(0, parasWithoutPrimaryKeyAndDataType.length() - 1);
|
|
|
|
|
parasWithoutPrimaryKeyAndDataType = new StringBuilder(parasWithoutPrimaryKeyAndDataType.substring(0, parasWithoutPrimaryKeyAndDataType.length() - 1));
|
|
|
|
|
if (parasWithoutPrimaryKeyAndDataType.toString().endsWith(",")) {
|
|
|
|
|
parasWithoutPrimaryKeyAndDataType = new StringBuilder(parasWithoutPrimaryKeyAndDataType.substring(0, parasWithoutPrimaryKeyAndDataType.length() - 1));
|
|
|
|
|
}
|
|
|
|
|
//后缀名称
|
|
|
|
|
String beanName = jo.getStr("beanNameWithoutT");
|
|
|
|
@ -245,21 +243,21 @@ public class GenerateCode {
|
|
|
|
|
paraList.add(record);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (parasWithoutPrimaryKey.endsWith(" ")) {
|
|
|
|
|
parasWithoutPrimaryKey = parasWithoutPrimaryKey.substring(0, parasWithoutPrimaryKey.length() - 1);
|
|
|
|
|
if (parasWithoutPrimaryKey.toString().endsWith(" ")) {
|
|
|
|
|
parasWithoutPrimaryKey = new StringBuilder(parasWithoutPrimaryKey.substring(0, parasWithoutPrimaryKey.length() - 1));
|
|
|
|
|
}
|
|
|
|
|
dataModel.put("paraList", paraList);//用于注释,显示@para 参数名 参数含义
|
|
|
|
|
dataModel.put("daoName", PropKit.get("daoName"));//显示dao的名称,比如ym
|
|
|
|
|
dataModel.put("beanName", beanName);//实体Bean的名称,用于构建方法名
|
|
|
|
|
dataModel.put("parasWithoutPrimaryKey", parasWithoutPrimaryKey);//不包括主键的参数列表,用于构建方法的参数
|
|
|
|
|
dataModel.put("parasWithoutPrimaryKeyAndDataType", parasWithoutPrimaryKeyAndDataType);//不包括数据类型的parasWithoutPrimaryKey
|
|
|
|
|
dataModel.put("parasWithoutPrimaryKey", parasWithoutPrimaryKey.toString());//不包括主键的参数列表,用于构建方法的参数
|
|
|
|
|
dataModel.put("parasWithoutPrimaryKeyAndDataType", parasWithoutPrimaryKeyAndDataType.toString());//不包括数据类型的parasWithoutPrimaryKey
|
|
|
|
|
dataModel.put("primary_key", primary_key);//主键名称
|
|
|
|
|
dataModel.put("primary_key_type", primary_key_type);//主键类型
|
|
|
|
|
dataModel.put("tableName", tableName);//表名
|
|
|
|
|
|
|
|
|
|
//更新时的填充参数
|
|
|
|
|
StringBuilder updateParas = new StringBuilder();
|
|
|
|
|
for (String s : parasWithoutPrimaryKeyAndDataType.split(",")) {
|
|
|
|
|
for (String s : parasWithoutPrimaryKeyAndDataType.toString().split(",")) {
|
|
|
|
|
if (s.equals("create_time")) {
|
|
|
|
|
updateParas.append(s).append("=now(),");
|
|
|
|
|
} else {
|
|
|
|
@ -286,7 +284,7 @@ public class GenerateCode {
|
|
|
|
|
// 1、生成Controller层代码
|
|
|
|
|
try (Writer out = new OutputStreamWriter(
|
|
|
|
|
new FileOutputStream(codePath + "/" + tableName + "_Controller.txt"),
|
|
|
|
|
"UTF-8")) { // 显式指定编码
|
|
|
|
|
StandardCharsets.UTF_8)) { // 显式指定编码
|
|
|
|
|
Template template = cfg.getTemplate("Controller.ftl");
|
|
|
|
|
template.process(dataModel, out);
|
|
|
|
|
System.out.println(dsKit.getCurrentTimeStr() + " 1、Controller层代码生成完毕。");
|
|
|
|
@ -297,7 +295,7 @@ public class GenerateCode {
|
|
|
|
|
// 2、生成Model层代码
|
|
|
|
|
try (Writer out = new OutputStreamWriter(
|
|
|
|
|
new FileOutputStream(codePath + "/" + tableName + "_Model.txt"),
|
|
|
|
|
"UTF-8")) { // 显式指定编码
|
|
|
|
|
StandardCharsets.UTF_8)) { // 显式指定编码
|
|
|
|
|
Template template = cfg.getTemplate("Model.ftl");
|
|
|
|
|
template.process(dataModel, out);
|
|
|
|
|
System.out.println(dsKit.getCurrentTimeStr() + " 2、Model层代码生成完毕。");
|
|
|
|
|