feat: 代码生成中添加表注释
This commit is contained in:
parent
93ac0f98e9
commit
32f0b5d710
@ -1,13 +1,18 @@
|
||||
package cn.rainss.codegenerator.utils;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.yaml.snakeyaml.Yaml;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.sql.*;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 数据库连接
|
||||
@ -93,11 +98,15 @@ public class DatabaseUtil {
|
||||
* @throws FileNotFoundException
|
||||
* @throws ClassNotFoundException
|
||||
*/
|
||||
public List<String> getTable() throws SQLException, FileNotFoundException, ClassNotFoundException {
|
||||
LinkedList<String> tables = new LinkedList<>();
|
||||
public List<TableInfo> getTable() throws SQLException, FileNotFoundException, ClassNotFoundException {
|
||||
LinkedList<TableInfo> tables = new LinkedList<>();
|
||||
ResultSet resultSet = getMetaData().getTables(null, null, "%", new String[]{"TABLE"});
|
||||
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;
|
||||
}
|
||||
@ -133,4 +142,22 @@ public class DatabaseUtil {
|
||||
}
|
||||
return columns;
|
||||
}
|
||||
|
||||
/**
|
||||
* 表信息
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
@Builder
|
||||
public static class TableInfo {
|
||||
/**
|
||||
* 表名
|
||||
*/
|
||||
private String name;
|
||||
|
||||
/**
|
||||
* 注释
|
||||
*/
|
||||
private String remarks;
|
||||
}
|
||||
}
|
||||
|
||||
@ -23,6 +23,7 @@ import java.sql.SQLException;
|
||||
import java.text.MessageFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
@ -111,7 +112,7 @@ public class GeneratorUtil {
|
||||
public static void Generator() throws SQLException, FileNotFoundException, ClassNotFoundException {
|
||||
System.out.println("=== initialization Generator ===");
|
||||
DatabaseUtil instance = DatabaseUtil.getInstance();
|
||||
List<String> table = instance.getTable();
|
||||
List<DatabaseUtil.TableInfo> table = instance.getTable();
|
||||
if (table.size() == 0) {
|
||||
System.out.print("没有需要生成的数据表");
|
||||
return;
|
||||
@ -119,7 +120,7 @@ public class GeneratorUtil {
|
||||
table.forEach(t -> {
|
||||
System.out.printf("<<<<< Generate code from table: %s >>>>>%n", t);
|
||||
try {
|
||||
List<Column> column = instance.getColumn(t);
|
||||
List<Column> column = instance.getColumn(t.getName());
|
||||
generatorCode(column, t);
|
||||
} catch (ClassNotFoundException | SQLException | IOException e) {
|
||||
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()) {
|
||||
generatorByTemplate(list, tableName, typeEnum);
|
||||
generatorByTemplate(list, table, typeEnum);
|
||||
}
|
||||
}
|
||||
|
||||
@ -154,10 +155,11 @@ public class GeneratorUtil {
|
||||
* 生成文件
|
||||
*
|
||||
* @param columnList 列
|
||||
* @param table 表名
|
||||
* @param tableInformation 表名
|
||||
* @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 ftlTemplate = String.format("%s.ftl", type.getTemplate());
|
||||
@ -173,6 +175,7 @@ public class GeneratorUtil {
|
||||
.configuration(CONFIG)
|
||||
.className(className)
|
||||
.tableName(table)
|
||||
.tableRemarks(tableInformation.getRemarks())
|
||||
.build();
|
||||
setTableInfo(columnList, tableInfo);
|
||||
String destPath = getDestFilePath(className, type);
|
||||
@ -423,6 +426,8 @@ public class GeneratorUtil {
|
||||
private String className;
|
||||
|
||||
private String tableName;
|
||||
|
||||
private String tableRemarks;
|
||||
}
|
||||
|
||||
@Setter
|
||||
|
||||
@ -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";
|
||||
}
|
||||
}
|
||||
@ -20,7 +20,7 @@ import java.time.LocalDateTime;
|
||||
</#if>
|
||||
|
||||
/**
|
||||
* ${tableName}
|
||||
* ${tableRemarks} ${tableName}
|
||||
* entity
|
||||
* @author ${configuration.author}
|
||||
* @date ${.now?datetime}
|
||||
|
||||
@ -5,7 +5,7 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* ${className}Mapper 接口
|
||||
* ${tableRemarks} ${className}Mapper 接口
|
||||
* </p>
|
||||
*
|
||||
* @author ${configuration.author}
|
||||
|
||||
@ -20,7 +20,7 @@ import java.time.LocalDateTime;
|
||||
</#if>
|
||||
|
||||
/**
|
||||
* ${tableName}
|
||||
* ${tableRemarks} ${tableName}
|
||||
* po
|
||||
* @author ${configuration.author}
|
||||
* @date ${.now?datetime}
|
||||
|
||||
@ -20,7 +20,7 @@ import java.time.LocalDateTime;
|
||||
</#if>
|
||||
|
||||
/**
|
||||
* ${tableName}
|
||||
* ${tableRemarks} ${tableName}
|
||||
* entity
|
||||
* @author ${configuration.author}
|
||||
* @date ${.now?datetime}
|
||||
|
||||
@ -20,7 +20,7 @@ import java.time.LocalDateTime;
|
||||
</#if>
|
||||
|
||||
/**
|
||||
* ${tableName}
|
||||
* ${tableRemarks} ${tableName}
|
||||
* entity
|
||||
* @author ${configuration.author}
|
||||
* @date ${.now?datetime}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user