package com.rongyichuang.judge;
|
|
import org.junit.jupiter.api.Test;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.jdbc.core.JdbcTemplate;
|
|
import javax.sql.DataSource;
|
import java.util.List;
|
import java.util.Map;
|
|
/**
|
* 数据库表结构检查测试类
|
* 用于查看实际的表结构和字段名
|
*/
|
@SpringBootTest
|
public class DatabaseSchemaInspectionTest {
|
|
@Autowired
|
private DataSource dataSource;
|
|
private JdbcTemplate jdbcTemplate;
|
|
@Test
|
void inspectTableStructures() {
|
jdbcTemplate = new JdbcTemplate(dataSource);
|
|
System.out.println("=== 检查数据库表结构 ===");
|
|
// 检查所有相关表的结构
|
String[] tables = {"t_media", "t_judge", "t_judge_tag", "t_tag"};
|
|
for (String tableName : tables) {
|
inspectTable(tableName);
|
}
|
|
System.out.println("=== 表结构检查完成 ===");
|
}
|
|
private void inspectTable(String tableName) {
|
System.out.println("\n--- 表 " + tableName + " 的结构 ---");
|
|
try {
|
String sql = "DESCRIBE " + tableName;
|
List<Map<String, Object>> columns = jdbcTemplate.queryForList(sql);
|
|
System.out.println("字段列表:");
|
for (Map<String, Object> column : columns) {
|
System.out.println(" 字段名: " + column.get("Field") +
|
", 类型: " + column.get("Type") +
|
", 是否为空: " + column.get("Null") +
|
", 键: " + column.get("Key") +
|
", 默认值: " + column.get("Default"));
|
}
|
|
// 查看表中的数据样本
|
String dataSql = "SELECT * FROM " + tableName + " LIMIT 3";
|
List<Map<String, Object>> sampleData = jdbcTemplate.queryForList(dataSql);
|
|
System.out.println("数据样本 (前3条):");
|
if (sampleData.isEmpty()) {
|
System.out.println(" (无数据)");
|
} else {
|
for (int i = 0; i < sampleData.size(); i++) {
|
System.out.println(" 记录 " + (i + 1) + ": " + sampleData.get(i));
|
}
|
}
|
|
} catch (Exception e) {
|
System.out.println("检查表 " + tableName + " 时出错: " + e.getMessage());
|
}
|
}
|
}
|