feat: 修改数据库连接

This commit is contained in:
bunny 2025-04-06 15:00:02 +08:00
parent 3c1aab3870
commit ad3c2bcaba
3 changed files with 27 additions and 39 deletions

View File

@ -61,8 +61,8 @@
<artifactId>HikariCP</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot3-starter</artifactId>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- lombok -->

View File

@ -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,8 +18,11 @@ import java.util.Set;
@Component
public class DbInfoUtil {
@Autowired
private DataSource dataSource;
private final DataSource dataSource;
public DbInfoUtil(DataSource dataSource) {
this.dataSource = dataSource;
}
/**
* 获取表的所有主键列名
@ -36,7 +38,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 +53,7 @@ public class DbInfoUtil {
List<TableMetaData> 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 +78,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 +101,7 @@ public class DbInfoUtil {
.refGeneration(refGeneration)
.build();
} else {
throw new RuntimeException("数据表不存在" );
throw new RuntimeException("数据表不存在");
}
return tableMetaData;
@ -123,16 +125,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()) {

View File

@ -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
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}