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

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

@ -23,6 +23,7 @@ public class TableController {
this.tableService = tableService; this.tableService = tableService;
} }
@Operation(summary = "获取所有表", description = "获取所有表") @Operation(summary = "获取所有表", description = "获取所有表")
@GetMapping("getAllTableMetaData") @GetMapping("getAllTableMetaData")
public Result<List<TableInfoVo>> getAllTableMetaData() { public Result<List<TableInfoVo>> getAllTableMetaData() {

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,8 +18,11 @@ 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()) {

View File

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

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 {