feat: 代码生成中添加表注释

This commit is contained in:
rainerosion 2023-01-31 15:03:45 +08:00
parent 93ac0f98e9
commit 32f0b5d710
8 changed files with 60 additions and 14 deletions

View File

@ -1,13 +1,18 @@
package cn.rainss.codegenerator.utils; package cn.rainss.codegenerator.utils;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.Yaml;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.sql.*; import java.sql.*;
import java.util.HashMap;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map;
/** /**
* 数据库连接 * 数据库连接
@ -93,11 +98,15 @@ public class DatabaseUtil {
* @throws FileNotFoundException * @throws FileNotFoundException
* @throws ClassNotFoundException * @throws ClassNotFoundException
*/ */
public List<String> getTable() throws SQLException, FileNotFoundException, ClassNotFoundException { public List<TableInfo> getTable() throws SQLException, FileNotFoundException, ClassNotFoundException {
LinkedList<String> tables = new LinkedList<>(); LinkedList<TableInfo> tables = new LinkedList<>();
ResultSet resultSet = getMetaData().getTables(null, null, "%", new String[]{"TABLE"}); ResultSet resultSet = getMetaData().getTables(null, null, "%", new String[]{"TABLE"});
while (resultSet.next()) { while (resultSet.next()) {
tables.add(resultSet.getString("TABLE_NAME")); TableInfo item = TableInfo.builder()
.name(resultSet.getString("TABLE_NAME"))
.remarks(resultSet.getString("REMARKS"))
.build();
tables.add(item);
} }
return tables; return tables;
} }
@ -133,4 +142,22 @@ public class DatabaseUtil {
} }
return columns; return columns;
} }
/**
* 表信息
*/
@Getter
@Setter
@Builder
public static class TableInfo {
/**
* 表名
*/
private String name;
/**
* 注释
*/
private String remarks;
}
} }

View File

@ -23,6 +23,7 @@ import java.sql.SQLException;
import java.text.MessageFormat; import java.text.MessageFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Objects; import java.util.Objects;
/** /**
@ -111,7 +112,7 @@ public class GeneratorUtil {
public static void Generator() throws SQLException, FileNotFoundException, ClassNotFoundException { public static void Generator() throws SQLException, FileNotFoundException, ClassNotFoundException {
System.out.println("=== initialization Generator ==="); System.out.println("=== initialization Generator ===");
DatabaseUtil instance = DatabaseUtil.getInstance(); DatabaseUtil instance = DatabaseUtil.getInstance();
List<String> table = instance.getTable(); List<DatabaseUtil.TableInfo> table = instance.getTable();
if (table.size() == 0) { if (table.size() == 0) {
System.out.print("没有需要生成的数据表"); System.out.print("没有需要生成的数据表");
return; return;
@ -119,7 +120,7 @@ public class GeneratorUtil {
table.forEach(t -> { table.forEach(t -> {
System.out.printf("<<<<< Generate code from table: %s >>>>>%n", t); System.out.printf("<<<<< Generate code from table: %s >>>>>%n", t);
try { try {
List<Column> column = instance.getColumn(t); List<Column> column = instance.getColumn(t.getName());
generatorCode(column, t); generatorCode(column, t);
} catch (ClassNotFoundException | SQLException | IOException e) { } catch (ClassNotFoundException | SQLException | IOException e) {
e.printStackTrace(); e.printStackTrace();
@ -143,9 +144,9 @@ public class GeneratorUtil {
/** /**
* 生成所有类型的代码 * 生成所有类型的代码
*/ */
public static void generatorCode(List<Column> list, String tableName) { public static void generatorCode(List<Column> list, DatabaseUtil.TableInfo table) {
for (TypeEnum typeEnum : TypeEnum.values()) { for (TypeEnum typeEnum : TypeEnum.values()) {
generatorByTemplate(list, tableName, typeEnum); generatorByTemplate(list, table, typeEnum);
} }
} }
@ -154,10 +155,11 @@ public class GeneratorUtil {
* 生成文件 * 生成文件
* *
* @param columnList * @param columnList
* @param table 表名 * @param tableInformation 表名
* @param type 类型 * @param type 类型
*/ */
public static void generatorByTemplate(List<Column> columnList, String table, TypeEnum type) { public static void generatorByTemplate(List<Column> columnList, DatabaseUtil.TableInfo tableInformation, TypeEnum type) {
String table = tableInformation.getName();
// 文件模板路径 // 文件模板路径
String templatePath = String.format("src/main/resources/template/%s.ftl", type.getTemplate()); String templatePath = String.format("src/main/resources/template/%s.ftl", type.getTemplate());
String ftlTemplate = String.format("%s.ftl", type.getTemplate()); String ftlTemplate = String.format("%s.ftl", type.getTemplate());
@ -173,6 +175,7 @@ public class GeneratorUtil {
.configuration(CONFIG) .configuration(CONFIG)
.className(className) .className(className)
.tableName(table) .tableName(table)
.tableRemarks(tableInformation.getRemarks())
.build(); .build();
setTableInfo(columnList, tableInfo); setTableInfo(columnList, tableInfo);
String destPath = getDestFilePath(className, type); String destPath = getDestFilePath(className, type);
@ -423,6 +426,8 @@ public class GeneratorUtil {
private String className; private String className;
private String tableName; private String tableName;
private String tableRemarks;
} }
@Setter @Setter

View File

@ -0,0 +1,14 @@
package ${packages}.${configuration.packageLocation.controller};
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import springfox.documentation.annotations.ApiIgnore;
@Controller
@Api(tags = {"${tableRemarks}"})
class HomeController {
@RequestMapping(value = {"/", "/swagger"})
public String index() {
return "redirect:doc.html";
}
}

View File

@ -20,7 +20,7 @@ import java.time.LocalDateTime;
</#if> </#if>
/** /**
* ${tableName} * ${tableRemarks} ${tableName}
* entity * entity
* @author ${configuration.author} * @author ${configuration.author}
* @date ${.now?datetime} * @date ${.now?datetime}

View File

@ -5,7 +5,7 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
/** /**
* <p> * <p>
* ${className}Mapper 接口 * ${tableRemarks} ${className}Mapper 接口
* </p> * </p>
* *
* @author ${configuration.author} * @author ${configuration.author}

View File

@ -20,7 +20,7 @@ import java.time.LocalDateTime;
</#if> </#if>
/** /**
* ${tableName} * ${tableRemarks} ${tableName}
* po * po
* @author ${configuration.author} * @author ${configuration.author}
* @date ${.now?datetime} * @date ${.now?datetime}

View File

@ -20,7 +20,7 @@ import java.time.LocalDateTime;
</#if> </#if>
/** /**
* ${tableName} * ${tableRemarks} ${tableName}
* entity * entity
* @author ${configuration.author} * @author ${configuration.author}
* @date ${.now?datetime} * @date ${.now?datetime}

View File

@ -20,7 +20,7 @@ import java.time.LocalDateTime;
</#if> </#if>
/** /**
* ${tableName} * ${tableRemarks} ${tableName}
* entity * entity
* @author ${configuration.author} * @author ${configuration.author}
* @date ${.now?datetime} * @date ${.now?datetime}