From 32f0b5d710d8469138ebd66f3178366716837207 Mon Sep 17 00:00:00 2001 From: rainerosion Date: Tue, 31 Jan 2023 15:03:45 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BB=A3=E7=A0=81=E7=94=9F=E6=88=90?= =?UTF-8?q?=E4=B8=AD=E6=B7=BB=E5=8A=A0=E8=A1=A8=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../codegenerator/utils/DatabaseUtil.java | 33 +++++++++++++++++-- .../codegenerator/utils/GeneratorUtil.java | 17 ++++++---- src/main/resources/template/controller.ftl | 14 ++++++++ src/main/resources/template/entity.ftl | 2 +- src/main/resources/template/mapper.ftl | 2 +- src/main/resources/template/po.ftl | 2 +- src/main/resources/template/reqDto.ftl | 2 +- src/main/resources/template/resDto.ftl | 2 +- 8 files changed, 60 insertions(+), 14 deletions(-) diff --git a/src/main/java/cn/rainss/codegenerator/utils/DatabaseUtil.java b/src/main/java/cn/rainss/codegenerator/utils/DatabaseUtil.java index d2b3fe8..77c2b86 100644 --- a/src/main/java/cn/rainss/codegenerator/utils/DatabaseUtil.java +++ b/src/main/java/cn/rainss/codegenerator/utils/DatabaseUtil.java @@ -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 getTable() throws SQLException, FileNotFoundException, ClassNotFoundException { - LinkedList tables = new LinkedList<>(); + public List getTable() throws SQLException, FileNotFoundException, ClassNotFoundException { + LinkedList 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; + } } diff --git a/src/main/java/cn/rainss/codegenerator/utils/GeneratorUtil.java b/src/main/java/cn/rainss/codegenerator/utils/GeneratorUtil.java index ba49406..c1e90f8 100644 --- a/src/main/java/cn/rainss/codegenerator/utils/GeneratorUtil.java +++ b/src/main/java/cn/rainss/codegenerator/utils/GeneratorUtil.java @@ -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 table = instance.getTable(); + List 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 = instance.getColumn(t); + List 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 list, String tableName) { + public static void generatorCode(List 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 columnList, String table, TypeEnum type) { + public static void generatorByTemplate(List 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 diff --git a/src/main/resources/template/controller.ftl b/src/main/resources/template/controller.ftl index e69de29..9aa78e5 100644 --- a/src/main/resources/template/controller.ftl +++ b/src/main/resources/template/controller.ftl @@ -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"; + } +} \ No newline at end of file diff --git a/src/main/resources/template/entity.ftl b/src/main/resources/template/entity.ftl index 02cc8e7..afec412 100644 --- a/src/main/resources/template/entity.ftl +++ b/src/main/resources/template/entity.ftl @@ -20,7 +20,7 @@ import java.time.LocalDateTime; /** -* ${tableName} +* ${tableRemarks} ${tableName} * entity * @author ${configuration.author} * @date ${.now?datetime} diff --git a/src/main/resources/template/mapper.ftl b/src/main/resources/template/mapper.ftl index 96e8c73..7b284cd 100644 --- a/src/main/resources/template/mapper.ftl +++ b/src/main/resources/template/mapper.ftl @@ -5,7 +5,7 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper; /** *

-* ${className}Mapper 接口 +* ${tableRemarks} ${className}Mapper 接口 *

* * @author ${configuration.author} diff --git a/src/main/resources/template/po.ftl b/src/main/resources/template/po.ftl index 66441c9..3f0728f 100644 --- a/src/main/resources/template/po.ftl +++ b/src/main/resources/template/po.ftl @@ -20,7 +20,7 @@ import java.time.LocalDateTime; /** -* ${tableName} +* ${tableRemarks} ${tableName} * po * @author ${configuration.author} * @date ${.now?datetime} diff --git a/src/main/resources/template/reqDto.ftl b/src/main/resources/template/reqDto.ftl index 7252fed..a1fbdf9 100644 --- a/src/main/resources/template/reqDto.ftl +++ b/src/main/resources/template/reqDto.ftl @@ -20,7 +20,7 @@ import java.time.LocalDateTime; /** -* ${tableName} +* ${tableRemarks} ${tableName} * entity * @author ${configuration.author} * @date ${.now?datetime} diff --git a/src/main/resources/template/resDto.ftl b/src/main/resources/template/resDto.ftl index 7252fed..a1fbdf9 100644 --- a/src/main/resources/template/resDto.ftl +++ b/src/main/resources/template/resDto.ftl @@ -20,7 +20,7 @@ import java.time.LocalDateTime; /** -* ${tableName} +* ${tableRemarks} ${tableName} * entity * @author ${configuration.author} * @date ${.now?datetime}