From 679f41838764928592b48573a2afdd5f0de2c0d5 Mon Sep 17 00:00:00 2001 From: bunny <1319900154@qq.com> Date: Sun, 6 Apr 2025 15:02:58 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BF=AE=E6=94=B9=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=E8=BF=9E=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- generator-code/pom.xml | 16 ++--- .../bunny/GeneratorCodeMainApplication.java | 2 - .../java/cn/bunny/config/Knife4jConfig.java | 16 ++--- .../main/java/cn/bunny/config/VmsHolder.java | 2 +- .../cn/bunny/controller/TableController.java | 17 +++--- .../bunny/service/impl/TableServiceImpl.java | 1 - .../main/java/cn/bunny/utils/DbInfoUtil.java | 39 ++++++------ .../src/main/resources/application.yml | 24 ++------ .../src/test/java/cn/bunny/JDBCTest.java | 59 ++++++++++--------- .../java/cn/bunny/utils/DbInfoUtilTest.java | 11 +++- 10 files changed, 91 insertions(+), 96 deletions(-) diff --git a/generator-code/pom.xml b/generator-code/pom.xml index 91626e8..9d6ed83 100644 --- a/generator-code/pom.xml +++ b/generator-code/pom.xml @@ -1,12 +1,12 @@ 4.0.0 - - org.springframework.boot - generator-code-server - 3.4.3 - ../pom.xml - + + org.springframework.boot + generator-code-server + 3.4.3 + ../pom.xml + generator-code jar @@ -61,8 +61,8 @@ HikariCP - com.baomidou - dynamic-datasource-spring-boot3-starter + org.springframework.boot + spring-boot-starter-jdbc diff --git a/generator-code/src/main/java/cn/bunny/GeneratorCodeMainApplication.java b/generator-code/src/main/java/cn/bunny/GeneratorCodeMainApplication.java index 670c0c8..c74b41e 100644 --- a/generator-code/src/main/java/cn/bunny/GeneratorCodeMainApplication.java +++ b/generator-code/src/main/java/cn/bunny/GeneratorCodeMainApplication.java @@ -2,9 +2,7 @@ package cn.bunny; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.transaction.annotation.EnableTransactionManagement; -@EnableTransactionManagement @SpringBootApplication public class GeneratorCodeMainApplication { public static void main(String[] args) { diff --git a/generator-code/src/main/java/cn/bunny/config/Knife4jConfig.java b/generator-code/src/main/java/cn/bunny/config/Knife4jConfig.java index 6279a4c..ab02850 100644 --- a/generator-code/src/main/java/cn/bunny/config/Knife4jConfig.java +++ b/generator-code/src/main/java/cn/bunny/config/Knife4jConfig.java @@ -16,22 +16,22 @@ public class Knife4jConfig { @Bean public OpenAPI openAPI() { // 作者等信息 - Contact contact = new Contact().name("Bunny" ).email("1319900154@qq.com" ).url("http://localhost:9999" ); + Contact contact = new Contact().name("Bunny").email("1319900154@qq.com").url("http://localhost:9999"); // 使用协议 - License license = new License().name("MIT" ).url("https://mit-license.org" ); + License license = new License().name("MIT").url("https://mit-license.org"); // 相关信息 - Info info = new Info().title("Bunny-Admin" ) + Info info = new Info().title("Bunny-Admin") .contact(contact).license(license) - .description("Bunny代码生成器" ) - .summary("Bunny的代码生成器" ) - .termsOfService("http://localhost:9999" ) - .version("v1.0.0" ); + .description("Bunny代码生成器") + .summary("Bunny的代码生成器") + .termsOfService("http://localhost:9999") + .version("v1.0.0"); return new OpenAPI().info(info).externalDocs(new ExternalDocumentation()); } @Bean public GroupedOpenApi all() { - return GroupedOpenApi.builder().group("全部请求接口" ).pathsToMatch("/api/**" ).build(); + return GroupedOpenApi.builder().group("全部请求接口").pathsToMatch("/api/**").build(); } } diff --git a/generator-code/src/main/java/cn/bunny/config/VmsHolder.java b/generator-code/src/main/java/cn/bunny/config/VmsHolder.java index d73b513..0e7651e 100644 --- a/generator-code/src/main/java/cn/bunny/config/VmsHolder.java +++ b/generator-code/src/main/java/cn/bunny/config/VmsHolder.java @@ -12,7 +12,7 @@ public class VmsHolder { @PostConstruct public void init() { Properties prop = new Properties(); - prop.put("file.resource.loader.class" , "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader" ); + prop.put("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); Velocity.init(prop); } } diff --git a/generator-code/src/main/java/cn/bunny/controller/TableController.java b/generator-code/src/main/java/cn/bunny/controller/TableController.java index 8b5de72..48d1765 100644 --- a/generator-code/src/main/java/cn/bunny/controller/TableController.java +++ b/generator-code/src/main/java/cn/bunny/controller/TableController.java @@ -12,9 +12,9 @@ import org.springframework.web.bind.annotation.RestController; import java.util.List; -@Tag(name = "数据库表控制器" , description = "数据库表信息接口" ) +@Tag(name = "数据库表控制器", description = "数据库表信息接口") @RestController -@RequestMapping("/api/table" ) +@RequestMapping("/api/table") public class TableController { private final TableService tableService; @@ -23,22 +23,23 @@ public class TableController { this.tableService = tableService; } - @Operation(summary = "获取所有表" , description = "获取所有表" ) - @GetMapping("getAllTableMetaData" ) + + @Operation(summary = "获取所有表", description = "获取所有表") + @GetMapping("getAllTableMetaData") public Result> getAllTableMetaData() { List list = tableService.getAllTableMetaData(); return Result.success(list); } - @Operation(summary = "获取表属性" , description = "获取表属性" ) - @GetMapping("getTableMetaData" ) + @Operation(summary = "获取表属性", description = "获取表属性") + @GetMapping("getTableMetaData") public Result getTableMetaData(String tableName) { TableInfoVo tableMetaData = tableService.getTableMetaData(tableName); return Result.success(tableMetaData); } - @Operation(summary = "获取列属性" , description = "获取列属性" ) - @GetMapping("getColumnInfo" ) + @Operation(summary = "获取列属性", description = "获取列属性") + @GetMapping("getColumnInfo") public Result> getColumnInfo(String tableName) { List columnInfo = tableService.getColumnInfo(tableName); return Result.success(columnInfo); diff --git a/generator-code/src/main/java/cn/bunny/service/impl/TableServiceImpl.java b/generator-code/src/main/java/cn/bunny/service/impl/TableServiceImpl.java index 7203704..3d25424 100644 --- a/generator-code/src/main/java/cn/bunny/service/impl/TableServiceImpl.java +++ b/generator-code/src/main/java/cn/bunny/service/impl/TableServiceImpl.java @@ -12,7 +12,6 @@ import org.springframework.stereotype.Service; import java.util.List; @Service - public class TableServiceImpl implements TableService { private final DbInfoUtil dbInfoUtil; diff --git a/generator-code/src/main/java/cn/bunny/utils/DbInfoUtil.java b/generator-code/src/main/java/cn/bunny/utils/DbInfoUtil.java index 24708e0..72ed2fe 100644 --- a/generator-code/src/main/java/cn/bunny/utils/DbInfoUtil.java +++ b/generator-code/src/main/java/cn/bunny/utils/DbInfoUtil.java @@ -3,7 +3,6 @@ package cn.bunny.utils; import cn.bunny.dao.entity.ColumnMetaData; import cn.bunny.dao.entity.TableMetaData; import org.apache.commons.lang3.StringUtils; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import javax.sql.DataSource; @@ -19,9 +18,12 @@ import java.util.Set; @Component public class DbInfoUtil { - @Autowired - private DataSource dataSource; + private final DataSource dataSource; + public DbInfoUtil(DataSource dataSource) { + this.dataSource = dataSource; + } + /** * 获取表的所有主键列名 * @@ -29,6 +31,7 @@ public class DbInfoUtil { * @return 主键列名的集合 */ public Set getPrimaryKeyColumns(String tableName) throws SQLException { + Set primaryKeys = new HashSet<>(); try (Connection connection = dataSource.getConnection()) { @@ -36,7 +39,7 @@ public class DbInfoUtil { ResultSet pkResultSet = metaData.getPrimaryKeys(null, null, tableName); while (pkResultSet.next()) { - primaryKeys.add(pkResultSet.getString("COLUMN_NAME" ).toLowerCase()); + primaryKeys.add(pkResultSet.getString("COLUMN_NAME").toLowerCase()); } return primaryKeys; @@ -51,7 +54,7 @@ public class DbInfoUtil { List list = new ArrayList<>(); while (tables.next()) { - String tableName = tables.getString("TABLE_NAME" ); + String tableName = tables.getString("TABLE_NAME"); TableMetaData tableMetaData = tableInfo(tableName); list.add(tableMetaData); } @@ -76,15 +79,15 @@ public class DbInfoUtil { // 获取表的注释信息 if (tables.next()) { - String remarks = tables.getString("REMARKS" ); - String tableCat = tables.getString("TABLE_CAT" ); - String tableSchem = tables.getString("TABLE_SCHEM" ); - String tableType = tables.getString("TABLE_TYPE" ); - String typeCat = tables.getString("TYPE_CAT" ); - String typeSchem = tables.getString("TYPE_SCHEM" ); - String typeName = tables.getString("TYPE_NAME" ); - String selfReferencingColName = tables.getString("SELF_REFERENCING_COL_NAME" ); - String refGeneration = tables.getString("REF_GENERATION" ); + String remarks = tables.getString("REMARKS"); + String tableCat = tables.getString("TABLE_CAT"); + String tableSchem = tables.getString("TABLE_SCHEM"); + String tableType = tables.getString("TABLE_TYPE"); + String typeCat = tables.getString("TYPE_CAT"); + String typeSchem = tables.getString("TYPE_SCHEM"); + String typeName = tables.getString("TYPE_NAME"); + String selfReferencingColName = tables.getString("SELF_REFERENCING_COL_NAME"); + String refGeneration = tables.getString("REF_GENERATION"); tableMetaData = TableMetaData.builder() .tableName(tableName) @@ -99,7 +102,7 @@ public class DbInfoUtil { .refGeneration(refGeneration) .build(); } else { - throw new RuntimeException("数据表不存在" ); + throw new RuntimeException("数据表不存在"); } return tableMetaData; @@ -123,16 +126,16 @@ public class DbInfoUtil { try (ResultSet columnsRs = metaData.getColumns(null, null, tableName, null)) { while (columnsRs.next()) { ColumnMetaData column = new ColumnMetaData(); - String columnName = columnsRs.getString("COLUMN_NAME" ); + String columnName = columnsRs.getString("COLUMN_NAME"); String javaType = ConvertUtil.convertToJavaType(column.getJdbcType()); column.setColumnName(columnName); column.setFieldName(ConvertUtil.convertToFieldName(column.getColumnName())); - column.setJdbcType(columnsRs.getString("TYPE_NAME" )); + column.setJdbcType(columnsRs.getString("TYPE_NAME")); column.setJavaType(javaType); column.setJavascriptType(StringUtils.uncapitalize(javaType)); - column.setComment(columnsRs.getString("REMARKS" )); + column.setComment(columnsRs.getString("REMARKS")); // 确保 primaryKeyColumns 不为空 if (!primaryKeyColumns.isEmpty()) { diff --git a/generator-code/src/main/resources/application.yml b/generator-code/src/main/resources/application.yml index 8c68250..d3cb52a 100644 --- a/generator-code/src/main/resources/application.yml +++ b/generator-code/src/main/resources/application.yml @@ -10,22 +10,8 @@ spring: check-template-location: false datasource: - dynamic: - primary: master #设置默认的数据源或者数据源组,默认值即为master - strict: false #严格匹配数据源,默认false. true未匹配到指定数据源时抛异常,false使用默认数据源 - grace-destroy: false #是否优雅关闭数据源,默认为false,设置为true时,关闭数据源时如果数据源中还存在活跃连接,至多等待10s后强制关闭 - datasource: - master: - type: com.zaxxer.hikari.HikariDataSource - driver-class-name: com.mysql.cj.jdbc.Driver - url: jdbc:mysql://${bunny.master.host}:${bunny.master.port}/${bunny.master.database}?serverTimezone=GMT%2B8&useSSL=false&characterEncoding=utf-8&allowPublicKeyRetrieval=true - username: ${bunny.master.username} - password: ${bunny.master.password} - connect: - type: com.zaxxer.hikari.HikariDataSource - driver-class-name: org.sqlite.JDBC - url: ${bunny.connect.url} - username: ${bunny.connect.username} - password: ${bunny.connect.password} - aop: - enabled: true \ No newline at end of file + type: com.zaxxer.hikari.HikariDataSource + driver-class-name: com.mysql.cj.jdbc.Driver + url: jdbc:mysql://${bunny.master.host}:${bunny.master.port}/${bunny.master.database}?serverTimezone=GMT%2B8&useSSL=false&characterEncoding=utf-8&allowPublicKeyRetrieval=true + username: ${bunny.master.username} + password: ${bunny.master.password} \ No newline at end of file diff --git a/generator-code/src/test/java/cn/bunny/JDBCTest.java b/generator-code/src/test/java/cn/bunny/JDBCTest.java index 468ce7f..9b1ec74 100644 --- a/generator-code/src/test/java/cn/bunny/JDBCTest.java +++ b/generator-code/src/test/java/cn/bunny/JDBCTest.java @@ -6,7 +6,6 @@ import cn.bunny.dao.entity.TableMetaData; import cn.bunny.utils.ConvertUtil; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import javax.sql.DataSource; @@ -21,9 +20,13 @@ import java.util.List; public class JDBCTest { DatabaseMetaData metaData; - - @Autowired - private DataSource dataSource; + + private final DataSource dataSource; + + public JDBCTest(DataSource dataSource) { + this.dataSource = dataSource; + } + @BeforeEach public void setUp() throws Exception { @@ -40,15 +43,15 @@ public class JDBCTest { // 获取表的注释信息 if (tables.next()) { - String remarks = tables.getString("REMARKS" ); - String tableCat = tables.getString("TABLE_CAT" ); - String tableSchem = tables.getString("TABLE_SCHEM" ); - String tableType = tables.getString("TABLE_TYPE" ); - String typeCat = tables.getString("TYPE_CAT" ); - String typeSchem = tables.getString("TYPE_SCHEM" ); - String typeName = tables.getString("TYPE_NAME" ); - String selfReferencingColName = tables.getString("SELF_REFERENCING_COL_NAME" ); - String refGeneration = tables.getString("REF_GENERATION" ); + String remarks = tables.getString("REMARKS"); + String tableCat = tables.getString("TABLE_CAT"); + String tableSchem = tables.getString("TABLE_SCHEM"); + String tableType = tables.getString("TABLE_TYPE"); + String typeCat = tables.getString("TYPE_CAT"); + String typeSchem = tables.getString("TYPE_SCHEM"); + String typeName = tables.getString("TYPE_NAME"); + String selfReferencingColName = tables.getString("SELF_REFERENCING_COL_NAME"); + String refGeneration = tables.getString("REF_GENERATION"); tableMetaData = TableMetaData.builder() .tableName(tableName) @@ -69,20 +72,20 @@ public class JDBCTest { @Test void testAllTableComment() throws SQLException { - ResultSet tables = metaData.getTables(null, null, "%" , new String[]{"TABLE"}); + ResultSet tables = metaData.getTables(null, null, "%", new String[]{"TABLE"}); List list = new ArrayList<>(); while (tables.next()) { - String tableName = tables.getString("TABLE_NAME" ); - String remarks = tables.getString("REMARKS" ); - String tableCat = tables.getString("TABLE_CAT" ); - String tableSchem = tables.getString("TABLE_SCHEM" ); - String tableType = tables.getString("TABLE_TYPE" ); - String typeCat = tables.getString("TYPE_CAT" ); - String typeSchem = tables.getString("TYPE_SCHEM" ); - String typeName = tables.getString("TYPE_NAME" ); - String selfReferencingColName = tables.getString("SELF_REFERENCING_COL_NAME" ); - String refGeneration = tables.getString("REF_GENERATION" ); + String tableName = tables.getString("TABLE_NAME"); + String remarks = tables.getString("REMARKS"); + String tableCat = tables.getString("TABLE_CAT"); + String tableSchem = tables.getString("TABLE_SCHEM"); + String tableType = tables.getString("TABLE_TYPE"); + String typeCat = tables.getString("TYPE_CAT"); + String typeSchem = tables.getString("TYPE_SCHEM"); + String typeName = tables.getString("TYPE_NAME"); + String selfReferencingColName = tables.getString("SELF_REFERENCING_COL_NAME"); + String refGeneration = tables.getString("REF_GENERATION"); TableMetaData tableMetaData = TableMetaData.builder() .tableName(tableName).comment(remarks) @@ -105,14 +108,14 @@ public class JDBCTest { void testColumnInfo() throws SQLException { List columns = new ArrayList<>(); - try (ResultSet columnsRs = metaData.getColumns(null, null, "sys_i18n" , null)) { + try (ResultSet columnsRs = metaData.getColumns(null, null, "sys_i18n", null)) { while (columnsRs.next()) { ColumnMetaData column = new ColumnMetaData(); - column.setColumnName(columnsRs.getString("COLUMN_NAME" )); + column.setColumnName(columnsRs.getString("COLUMN_NAME")); column.setFieldName(ConvertUtil.convertToFieldName(column.getColumnName())); - column.setJdbcType(columnsRs.getString("TYPE_NAME" )); + column.setJdbcType(columnsRs.getString("TYPE_NAME")); column.setJavaType(ConvertUtil.convertToJavaType(column.getJdbcType())); - column.setComment(columnsRs.getString("REMARKS" )); + column.setComment(columnsRs.getString("REMARKS")); columns.add(column); System.out.println(column); diff --git a/generator-code/src/test/java/cn/bunny/utils/DbInfoUtilTest.java b/generator-code/src/test/java/cn/bunny/utils/DbInfoUtilTest.java index 9757da0..fcdacea 100644 --- a/generator-code/src/test/java/cn/bunny/utils/DbInfoUtilTest.java +++ b/generator-code/src/test/java/cn/bunny/utils/DbInfoUtilTest.java @@ -3,7 +3,6 @@ package cn.bunny.utils; import cn.bunny.dao.entity.ColumnMetaData; import cn.bunny.dao.entity.TableMetaData; import org.junit.jupiter.api.Test; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import java.sql.SQLException; @@ -14,8 +13,14 @@ class DbInfoUtilTest { String tableName = "sys_i18n"; - @Autowired - private DbInfoUtil dbInfoUtil; + // @Autowired + // private DbInfoUtil dbInfoUtil; + + private final DbInfoUtil dbInfoUtil; + + public DbInfoUtilTest(DbInfoUtil dbInfoUtil) { + this.dbInfoUtil = dbInfoUtil; + } @Test void tableInfo() throws SQLException {