2025-04-04 18:10:55 +08:00
|
|
|
package cn.bunny;
|
|
|
|
|
|
|
|
|
2025-04-04 20:03:39 +08:00
|
|
|
import cn.bunny.dao.entity.ColumnMetaData;
|
|
|
|
import cn.bunny.dao.entity.TableMetaData;
|
2025-04-04 18:10:55 +08:00
|
|
|
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;
|
|
|
|
|
2025-04-05 16:36:23 +08:00
|
|
|
import javax.sql.DataSource;
|
|
|
|
import java.sql.Connection;
|
2025-04-04 18:10:55 +08:00
|
|
|
import java.sql.DatabaseMetaData;
|
|
|
|
import java.sql.ResultSet;
|
|
|
|
import java.sql.SQLException;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
@SpringBootTest
|
|
|
|
public class JDBCTest {
|
|
|
|
|
|
|
|
DatabaseMetaData metaData;
|
2025-04-05 16:36:23 +08:00
|
|
|
|
2025-04-04 18:10:55 +08:00
|
|
|
@Autowired
|
2025-04-05 16:36:23 +08:00
|
|
|
private DataSource dataSource;
|
2025-04-04 18:10:55 +08:00
|
|
|
|
|
|
|
@BeforeEach
|
|
|
|
public void setUp() throws Exception {
|
2025-04-05 16:36:23 +08:00
|
|
|
try (Connection connection = dataSource.getConnection()) {
|
|
|
|
metaData = connection.getMetaData();
|
|
|
|
}
|
2025-04-04 18:10:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void testComment() throws SQLException {
|
|
|
|
String tableName = "sys_i18n";
|
|
|
|
TableMetaData tableMetaData;
|
|
|
|
ResultSet tables = metaData.getTables(null, null, tableName, new String[]{"TABLE"});
|
|
|
|
|
|
|
|
// 获取表的注释信息
|
|
|
|
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" );
|
|
|
|
|
|
|
|
tableMetaData = TableMetaData.builder()
|
|
|
|
.tableName(tableName)
|
2025-04-04 20:03:39 +08:00
|
|
|
.comment(remarks)
|
2025-04-04 18:10:55 +08:00
|
|
|
.tableCat(tableCat)
|
|
|
|
.tableSchem(tableSchem)
|
|
|
|
.tableType(tableType)
|
|
|
|
.typeCat(typeCat)
|
|
|
|
.typeSchem(typeSchem)
|
|
|
|
.typeName(typeName)
|
|
|
|
.selfReferencingColName(selfReferencingColName)
|
|
|
|
.refGeneration(refGeneration)
|
|
|
|
.build();
|
|
|
|
|
|
|
|
System.out.println(tableMetaData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-04-04 20:03:39 +08:00
|
|
|
@Test
|
|
|
|
void testAllTableComment() throws SQLException {
|
|
|
|
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" );
|
|
|
|
|
|
|
|
TableMetaData tableMetaData = TableMetaData.builder()
|
|
|
|
.tableName(tableName).comment(remarks)
|
|
|
|
.tableCat(tableCat)
|
|
|
|
.tableSchem(tableSchem)
|
|
|
|
.tableType(tableType)
|
|
|
|
.typeCat(typeCat)
|
|
|
|
.typeSchem(typeSchem)
|
|
|
|
.typeName(typeName)
|
|
|
|
.selfReferencingColName(selfReferencingColName)
|
|
|
|
.refGeneration(refGeneration)
|
|
|
|
.build();
|
|
|
|
list.add(tableMetaData);
|
|
|
|
}
|
|
|
|
|
|
|
|
System.out.println(list);
|
|
|
|
}
|
|
|
|
|
2025-04-04 18:10:55 +08:00
|
|
|
@Test
|
|
|
|
void testColumnInfo() throws SQLException {
|
|
|
|
List<ColumnMetaData> columns = new ArrayList<>();
|
|
|
|
|
|
|
|
try (ResultSet columnsRs = metaData.getColumns(null, null, "sys_i18n" , null)) {
|
|
|
|
while (columnsRs.next()) {
|
|
|
|
ColumnMetaData column = new ColumnMetaData();
|
|
|
|
column.setColumnName(columnsRs.getString("COLUMN_NAME" ));
|
|
|
|
column.setFieldName(ConvertUtil.convertToFieldName(column.getColumnName()));
|
|
|
|
column.setJdbcType(columnsRs.getString("TYPE_NAME" ));
|
|
|
|
column.setJavaType(ConvertUtil.convertToJavaType(column.getJdbcType()));
|
|
|
|
column.setComment(columnsRs.getString("REMARKS" ));
|
|
|
|
|
|
|
|
columns.add(column);
|
|
|
|
System.out.println(column);
|
|
|
|
}
|
|
|
|
}
|
2025-04-04 20:03:39 +08:00
|
|
|
|
|
|
|
System.out.println(columns);
|
2025-04-04 18:10:55 +08:00
|
|
|
}
|
|
|
|
}
|