feat: 修改数据库连接

This commit is contained in:
bunny 2025-04-06 15:02:58 +08:00
parent 74de80bf68
commit 679f418387
10 changed files with 91 additions and 96 deletions

View File

@ -1,12 +1,12 @@
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<parent> <parent>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>generator-code-server</artifactId> <artifactId>generator-code-server</artifactId>
<version>3.4.3</version> <version>3.4.3</version>
<relativePath>../pom.xml</relativePath> <relativePath>../pom.xml</relativePath>
</parent> </parent>
<artifactId>generator-code</artifactId> <artifactId>generator-code</artifactId>
<packaging>jar</packaging> <packaging>jar</packaging>
@ -61,8 +61,8 @@
<artifactId>HikariCP</artifactId> <artifactId>HikariCP</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.baomidou</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>dynamic-datasource-spring-boot3-starter</artifactId> <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency> </dependency>
<!-- lombok --> <!-- lombok -->

View File

@ -2,9 +2,7 @@ package cn.bunny;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@EnableTransactionManagement
@SpringBootApplication @SpringBootApplication
public class GeneratorCodeMainApplication { public class GeneratorCodeMainApplication {
public static void main(String[] args) { public static void main(String[] args) {

View File

@ -16,22 +16,22 @@ public class Knife4jConfig {
@Bean @Bean
public OpenAPI openAPI() { 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) .contact(contact).license(license)
.description("Bunny代码生成器" ) .description("Bunny代码生成器")
.summary("Bunny的代码生成器" ) .summary("Bunny的代码生成器")
.termsOfService("http://localhost:9999" ) .termsOfService("http://localhost:9999")
.version("v1.0.0" ); .version("v1.0.0");
return new OpenAPI().info(info).externalDocs(new ExternalDocumentation()); return new OpenAPI().info(info).externalDocs(new ExternalDocumentation());
} }
@Bean @Bean
public GroupedOpenApi all() { public GroupedOpenApi all() {
return GroupedOpenApi.builder().group("全部请求接口" ).pathsToMatch("/api/**" ).build(); return GroupedOpenApi.builder().group("全部请求接口").pathsToMatch("/api/**").build();
} }
} }

View File

@ -12,7 +12,7 @@ public class VmsHolder {
@PostConstruct @PostConstruct
public void init() { public void init() {
Properties prop = new Properties(); 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); Velocity.init(prop);
} }
} }

View File

@ -12,9 +12,9 @@ import org.springframework.web.bind.annotation.RestController;
import java.util.List; import java.util.List;
@Tag(name = "数据库表控制器" , description = "数据库表信息接口" ) @Tag(name = "数据库表控制器", description = "数据库表信息接口")
@RestController @RestController
@RequestMapping("/api/table" ) @RequestMapping("/api/table")
public class TableController { public class TableController {
private final TableService tableService; private final TableService tableService;
@ -23,22 +23,23 @@ public class TableController {
this.tableService = tableService; this.tableService = tableService;
} }
@Operation(summary = "获取所有表" , description = "获取所有表" )
@GetMapping("getAllTableMetaData" ) @Operation(summary = "获取所有表", description = "获取所有表")
@GetMapping("getAllTableMetaData")
public Result<List<TableInfoVo>> getAllTableMetaData() { public Result<List<TableInfoVo>> getAllTableMetaData() {
List<TableInfoVo> list = tableService.getAllTableMetaData(); List<TableInfoVo> list = tableService.getAllTableMetaData();
return Result.success(list); return Result.success(list);
} }
@Operation(summary = "获取表属性" , description = "获取表属性" ) @Operation(summary = "获取表属性", description = "获取表属性")
@GetMapping("getTableMetaData" ) @GetMapping("getTableMetaData")
public Result<TableInfoVo> getTableMetaData(String tableName) { public Result<TableInfoVo> getTableMetaData(String tableName) {
TableInfoVo tableMetaData = tableService.getTableMetaData(tableName); TableInfoVo tableMetaData = tableService.getTableMetaData(tableName);
return Result.success(tableMetaData); return Result.success(tableMetaData);
} }
@Operation(summary = "获取列属性" , description = "获取列属性" ) @Operation(summary = "获取列属性", description = "获取列属性")
@GetMapping("getColumnInfo" ) @GetMapping("getColumnInfo")
public Result<List<ColumnMetaData>> getColumnInfo(String tableName) { public Result<List<ColumnMetaData>> getColumnInfo(String tableName) {
List<ColumnMetaData> columnInfo = tableService.getColumnInfo(tableName); List<ColumnMetaData> columnInfo = tableService.getColumnInfo(tableName);
return Result.success(columnInfo); return Result.success(columnInfo);

View File

@ -12,7 +12,6 @@ import org.springframework.stereotype.Service;
import java.util.List; import java.util.List;
@Service @Service
public class TableServiceImpl implements TableService { public class TableServiceImpl implements TableService {
private final DbInfoUtil dbInfoUtil; private final DbInfoUtil dbInfoUtil;

View File

@ -3,7 +3,6 @@ package cn.bunny.utils;
import cn.bunny.dao.entity.ColumnMetaData; import cn.bunny.dao.entity.ColumnMetaData;
import cn.bunny.dao.entity.TableMetaData; import cn.bunny.dao.entity.TableMetaData;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import javax.sql.DataSource; import javax.sql.DataSource;
@ -19,9 +18,12 @@ import java.util.Set;
@Component @Component
public class DbInfoUtil { public class DbInfoUtil {
@Autowired private final DataSource dataSource;
private DataSource dataSource;
public DbInfoUtil(DataSource dataSource) {
this.dataSource = dataSource;
}
/** /**
* 获取表的所有主键列名 * 获取表的所有主键列名
* *
@ -29,6 +31,7 @@ public class DbInfoUtil {
* @return 主键列名的集合 * @return 主键列名的集合
*/ */
public Set<String> getPrimaryKeyColumns(String tableName) throws SQLException { public Set<String> getPrimaryKeyColumns(String tableName) throws SQLException {
Set<String> primaryKeys = new HashSet<>(); Set<String> primaryKeys = new HashSet<>();
try (Connection connection = dataSource.getConnection()) { try (Connection connection = dataSource.getConnection()) {
@ -36,7 +39,7 @@ public class DbInfoUtil {
ResultSet pkResultSet = metaData.getPrimaryKeys(null, null, tableName); ResultSet pkResultSet = metaData.getPrimaryKeys(null, null, tableName);
while (pkResultSet.next()) { while (pkResultSet.next()) {
primaryKeys.add(pkResultSet.getString("COLUMN_NAME" ).toLowerCase()); primaryKeys.add(pkResultSet.getString("COLUMN_NAME").toLowerCase());
} }
return primaryKeys; return primaryKeys;
@ -51,7 +54,7 @@ public class DbInfoUtil {
List<TableMetaData> list = new ArrayList<>(); List<TableMetaData> list = new ArrayList<>();
while (tables.next()) { while (tables.next()) {
String tableName = tables.getString("TABLE_NAME" ); String tableName = tables.getString("TABLE_NAME");
TableMetaData tableMetaData = tableInfo(tableName); TableMetaData tableMetaData = tableInfo(tableName);
list.add(tableMetaData); list.add(tableMetaData);
} }
@ -76,15 +79,15 @@ public class DbInfoUtil {
// 获取表的注释信息 // 获取表的注释信息
if (tables.next()) { if (tables.next()) {
String remarks = tables.getString("REMARKS" ); String remarks = tables.getString("REMARKS");
String tableCat = tables.getString("TABLE_CAT" ); String tableCat = tables.getString("TABLE_CAT");
String tableSchem = tables.getString("TABLE_SCHEM" ); String tableSchem = tables.getString("TABLE_SCHEM");
String tableType = tables.getString("TABLE_TYPE" ); String tableType = tables.getString("TABLE_TYPE");
String typeCat = tables.getString("TYPE_CAT" ); String typeCat = tables.getString("TYPE_CAT");
String typeSchem = tables.getString("TYPE_SCHEM" ); String typeSchem = tables.getString("TYPE_SCHEM");
String typeName = tables.getString("TYPE_NAME" ); String typeName = tables.getString("TYPE_NAME");
String selfReferencingColName = tables.getString("SELF_REFERENCING_COL_NAME" ); String selfReferencingColName = tables.getString("SELF_REFERENCING_COL_NAME");
String refGeneration = tables.getString("REF_GENERATION" ); String refGeneration = tables.getString("REF_GENERATION");
tableMetaData = TableMetaData.builder() tableMetaData = TableMetaData.builder()
.tableName(tableName) .tableName(tableName)
@ -99,7 +102,7 @@ public class DbInfoUtil {
.refGeneration(refGeneration) .refGeneration(refGeneration)
.build(); .build();
} else { } else {
throw new RuntimeException("数据表不存在" ); throw new RuntimeException("数据表不存在");
} }
return tableMetaData; return tableMetaData;
@ -123,16 +126,16 @@ public class DbInfoUtil {
try (ResultSet columnsRs = metaData.getColumns(null, null, tableName, null)) { try (ResultSet columnsRs = metaData.getColumns(null, null, tableName, null)) {
while (columnsRs.next()) { while (columnsRs.next()) {
ColumnMetaData column = new ColumnMetaData(); ColumnMetaData column = new ColumnMetaData();
String columnName = columnsRs.getString("COLUMN_NAME" ); String columnName = columnsRs.getString("COLUMN_NAME");
String javaType = ConvertUtil.convertToJavaType(column.getJdbcType()); String javaType = ConvertUtil.convertToJavaType(column.getJdbcType());
column.setColumnName(columnName); column.setColumnName(columnName);
column.setFieldName(ConvertUtil.convertToFieldName(column.getColumnName())); column.setFieldName(ConvertUtil.convertToFieldName(column.getColumnName()));
column.setJdbcType(columnsRs.getString("TYPE_NAME" )); column.setJdbcType(columnsRs.getString("TYPE_NAME"));
column.setJavaType(javaType); column.setJavaType(javaType);
column.setJavascriptType(StringUtils.uncapitalize(javaType)); column.setJavascriptType(StringUtils.uncapitalize(javaType));
column.setComment(columnsRs.getString("REMARKS" )); column.setComment(columnsRs.getString("REMARKS"));
// 确保 primaryKeyColumns 不为空 // 确保 primaryKeyColumns 不为空
if (!primaryKeyColumns.isEmpty()) { if (!primaryKeyColumns.isEmpty()) {

View File

@ -10,22 +10,8 @@ spring:
check-template-location: false check-template-location: false
datasource: datasource:
dynamic: type: com.zaxxer.hikari.HikariDataSource
primary: master #设置默认的数据源或者数据源组,默认值即为master driver-class-name: com.mysql.cj.jdbc.Driver
strict: false #严格匹配数据源,默认false. true未匹配到指定数据源时抛异常,false使用默认数据源 url: jdbc:mysql://${bunny.master.host}:${bunny.master.port}/${bunny.master.database}?serverTimezone=GMT%2B8&useSSL=false&characterEncoding=utf-8&allowPublicKeyRetrieval=true
grace-destroy: false #是否优雅关闭数据源默认为false设置为true时关闭数据源时如果数据源中还存在活跃连接至多等待10s后强制关闭 username: ${bunny.master.username}
datasource: password: ${bunny.master.password}
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

View File

@ -6,7 +6,6 @@ import cn.bunny.dao.entity.TableMetaData;
import cn.bunny.utils.ConvertUtil; import cn.bunny.utils.ConvertUtil;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import javax.sql.DataSource; import javax.sql.DataSource;
@ -21,9 +20,13 @@ import java.util.List;
public class JDBCTest { public class JDBCTest {
DatabaseMetaData metaData; DatabaseMetaData metaData;
@Autowired private final DataSource dataSource;
private DataSource dataSource;
public JDBCTest(DataSource dataSource) {
this.dataSource = dataSource;
}
@BeforeEach @BeforeEach
public void setUp() throws Exception { public void setUp() throws Exception {
@ -40,15 +43,15 @@ public class JDBCTest {
// 获取表的注释信息 // 获取表的注释信息
if (tables.next()) { if (tables.next()) {
String remarks = tables.getString("REMARKS" ); String remarks = tables.getString("REMARKS");
String tableCat = tables.getString("TABLE_CAT" ); String tableCat = tables.getString("TABLE_CAT");
String tableSchem = tables.getString("TABLE_SCHEM" ); String tableSchem = tables.getString("TABLE_SCHEM");
String tableType = tables.getString("TABLE_TYPE" ); String tableType = tables.getString("TABLE_TYPE");
String typeCat = tables.getString("TYPE_CAT" ); String typeCat = tables.getString("TYPE_CAT");
String typeSchem = tables.getString("TYPE_SCHEM" ); String typeSchem = tables.getString("TYPE_SCHEM");
String typeName = tables.getString("TYPE_NAME" ); String typeName = tables.getString("TYPE_NAME");
String selfReferencingColName = tables.getString("SELF_REFERENCING_COL_NAME" ); String selfReferencingColName = tables.getString("SELF_REFERENCING_COL_NAME");
String refGeneration = tables.getString("REF_GENERATION" ); String refGeneration = tables.getString("REF_GENERATION");
tableMetaData = TableMetaData.builder() tableMetaData = TableMetaData.builder()
.tableName(tableName) .tableName(tableName)
@ -69,20 +72,20 @@ public class JDBCTest {
@Test @Test
void testAllTableComment() throws SQLException { void testAllTableComment() throws SQLException {
ResultSet tables = metaData.getTables(null, null, "%" , new String[]{"TABLE"}); ResultSet tables = metaData.getTables(null, null, "%", new String[]{"TABLE"});
List<TableMetaData> list = new ArrayList<>(); List<TableMetaData> list = new ArrayList<>();
while (tables.next()) { while (tables.next()) {
String tableName = tables.getString("TABLE_NAME" ); String tableName = tables.getString("TABLE_NAME");
String remarks = tables.getString("REMARKS" ); String remarks = tables.getString("REMARKS");
String tableCat = tables.getString("TABLE_CAT" ); String tableCat = tables.getString("TABLE_CAT");
String tableSchem = tables.getString("TABLE_SCHEM" ); String tableSchem = tables.getString("TABLE_SCHEM");
String tableType = tables.getString("TABLE_TYPE" ); String tableType = tables.getString("TABLE_TYPE");
String typeCat = tables.getString("TYPE_CAT" ); String typeCat = tables.getString("TYPE_CAT");
String typeSchem = tables.getString("TYPE_SCHEM" ); String typeSchem = tables.getString("TYPE_SCHEM");
String typeName = tables.getString("TYPE_NAME" ); String typeName = tables.getString("TYPE_NAME");
String selfReferencingColName = tables.getString("SELF_REFERENCING_COL_NAME" ); String selfReferencingColName = tables.getString("SELF_REFERENCING_COL_NAME");
String refGeneration = tables.getString("REF_GENERATION" ); String refGeneration = tables.getString("REF_GENERATION");
TableMetaData tableMetaData = TableMetaData.builder() TableMetaData tableMetaData = TableMetaData.builder()
.tableName(tableName).comment(remarks) .tableName(tableName).comment(remarks)
@ -105,14 +108,14 @@ public class JDBCTest {
void testColumnInfo() throws SQLException { void testColumnInfo() throws SQLException {
List<ColumnMetaData> columns = new ArrayList<>(); List<ColumnMetaData> 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()) { while (columnsRs.next()) {
ColumnMetaData column = new ColumnMetaData(); ColumnMetaData column = new ColumnMetaData();
column.setColumnName(columnsRs.getString("COLUMN_NAME" )); column.setColumnName(columnsRs.getString("COLUMN_NAME"));
column.setFieldName(ConvertUtil.convertToFieldName(column.getColumnName())); 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.setJavaType(ConvertUtil.convertToJavaType(column.getJdbcType()));
column.setComment(columnsRs.getString("REMARKS" )); column.setComment(columnsRs.getString("REMARKS"));
columns.add(column); columns.add(column);
System.out.println(column); System.out.println(column);

View File

@ -3,7 +3,6 @@ package cn.bunny.utils;
import cn.bunny.dao.entity.ColumnMetaData; import cn.bunny.dao.entity.ColumnMetaData;
import cn.bunny.dao.entity.TableMetaData; import cn.bunny.dao.entity.TableMetaData;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import java.sql.SQLException; import java.sql.SQLException;
@ -14,8 +13,14 @@ class DbInfoUtilTest {
String tableName = "sys_i18n"; String tableName = "sys_i18n";
@Autowired // @Autowired
private DbInfoUtil dbInfoUtil; // private DbInfoUtil dbInfoUtil;
private final DbInfoUtil dbInfoUtil;
public DbInfoUtilTest(DbInfoUtil dbInfoUtil) {
this.dbInfoUtil = dbInfoUtil;
}
@Test @Test
void tableInfo() throws SQLException { void tableInfo() throws SQLException {