lrj
2 天以前 c61d4fe27c97d2ecc907756aa571a4ef14a7b9b6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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());
        }
    }
}