feat: 修改数据库连接
This commit is contained in:
parent
74de80bf68
commit
679f418387
|
@ -1,12 +1,12 @@
|
|||
<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">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>generator-code-server</artifactId>
|
||||
<version>3.4.3</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>generator-code-server</artifactId>
|
||||
<version>3.4.3</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
<artifactId>generator-code</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
@ -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 -->
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<List<TableInfoVo>> getAllTableMetaData() {
|
||||
List<TableInfoVo> list = tableService.getAllTableMetaData();
|
||||
return Result.success(list);
|
||||
}
|
||||
|
||||
@Operation(summary = "获取表属性" , description = "获取表属性" )
|
||||
@GetMapping("getTableMetaData" )
|
||||
@Operation(summary = "获取表属性", description = "获取表属性")
|
||||
@GetMapping("getTableMetaData")
|
||||
public Result<TableInfoVo> getTableMetaData(String tableName) {
|
||||
TableInfoVo tableMetaData = tableService.getTableMetaData(tableName);
|
||||
return Result.success(tableMetaData);
|
||||
}
|
||||
|
||||
@Operation(summary = "获取列属性" , description = "获取列属性" )
|
||||
@GetMapping("getColumnInfo" )
|
||||
@Operation(summary = "获取列属性", description = "获取列属性")
|
||||
@GetMapping("getColumnInfo")
|
||||
public Result<List<ColumnMetaData>> getColumnInfo(String tableName) {
|
||||
List<ColumnMetaData> columnInfo = tableService.getColumnInfo(tableName);
|
||||
return Result.success(columnInfo);
|
||||
|
|
|
@ -12,7 +12,6 @@ import org.springframework.stereotype.Service;
|
|||
import java.util.List;
|
||||
|
||||
@Service
|
||||
|
||||
public class TableServiceImpl implements TableService {
|
||||
|
||||
private final DbInfoUtil dbInfoUtil;
|
||||
|
|
|
@ -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<String> getPrimaryKeyColumns(String tableName) throws SQLException {
|
||||
|
||||
Set<String> 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<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 +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()) {
|
||||
|
|
|
@ -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}
|
|
@ -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<TableMetaData> 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<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()) {
|
||||
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);
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in New Issue