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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package com.rongyichuang.carousel;
 
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
 
import javax.sql.DataSource;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
 
@SpringBootTest
@ActiveProfiles("test")
public class DatabaseSchemaTest {
 
    @Autowired
    private DataSource dataSource;
 
    @Test
    public void testCarouselTableSchema() {
        try (Connection connection = dataSource.getConnection()) {
            System.out.println("=== 数据库连接成功 ===");
            System.out.println("数据库URL: " + connection.getMetaData().getURL());
            System.out.println("数据库产品: " + connection.getMetaData().getDatabaseProductName());
            System.out.println("数据库版本: " + connection.getMetaData().getDatabaseProductVersion());
            
            // 检查表是否存在
            DatabaseMetaData metaData = connection.getMetaData();
            ResultSet tables = metaData.getTables(null, null, "t_carousel", null);
            
            if (tables.next()) {
                System.out.println("\n=== t_carousel 表存在 ===");
                
                // 获取表结构
                ResultSet columns = metaData.getColumns(null, null, "t_carousel", null);
                System.out.println("\n=== 表字段信息 ===");
                System.out.printf("%-20s %-15s %-10s %-10s %-15s%n", 
                    "字段名", "数据类型", "长度", "可空", "默认值");
                System.out.println("=".repeat(80));
                
                List<String> actualColumns = new ArrayList<>();
                while (columns.next()) {
                    String columnName = columns.getString("COLUMN_NAME");
                    String dataType = columns.getString("TYPE_NAME");
                    int columnSize = columns.getInt("COLUMN_SIZE");
                    String nullable = columns.getString("IS_NULLABLE");
                    String defaultValue = columns.getString("COLUMN_DEF");
                    
                    actualColumns.add(columnName);
                    System.out.printf("%-20s %-15s %-10d %-10s %-15s%n", 
                        columnName, dataType, columnSize, nullable, 
                        defaultValue != null ? defaultValue : "NULL");
                }
                
                // 对比实体类期望的字段
                System.out.println("\n=== 实体类字段对比 ===");
                String[] expectedColumns = {
                    "id", "title", "content", "sort_order", "state", 
                    "create_time", "update_time", "create_by", "update_by"
                };
                
                for (String expected : expectedColumns) {
                    if (actualColumns.contains(expected)) {
                        System.out.println("✓ " + expected + " - 存在");
                    } else {
                        System.out.println("✗ " + expected + " - 缺失");
                    }
                }
                
                // 检查是否有多余字段
                System.out.println("\n=== 多余字段检查 ===");
                for (String actual : actualColumns) {
                    boolean found = false;
                    for (String expected : expectedColumns) {
                        if (expected.equals(actual)) {
                            found = true;
                            break;
                        }
                    }
                    if (!found) {
                        System.out.println("! " + actual + " - 实体类中未定义");
                    }
                }
                
            } else {
                System.out.println("\n=== t_carousel 表不存在 ===");
                
                // 列出所有表
                System.out.println("\n=== 数据库中的所有表 ===");
                ResultSet allTables = metaData.getTables(null, null, "%", new String[]{"TABLE"});
                while (allTables.next()) {
                    String tableName = allTables.getString("TABLE_NAME");
                    if (tableName.toLowerCase().contains("carousel") || 
                        tableName.toLowerCase().contains("news") ||
                        tableName.toLowerCase().contains("banner")) {
                        System.out.println("- " + tableName);
                    }
                }
            }
            
        } catch (SQLException e) {
            System.err.println("数据库连接或查询失败: " + e.getMessage());
            e.printStackTrace();
        }
    }
    
    @Test
    public void testSimpleQuery() {
        try (Connection connection = dataSource.getConnection()) {
            // 尝试简单查询
            String sql = "SELECT COUNT(*) as count FROM t_carousel WHERE state = 1";
            try (PreparedStatement stmt = connection.prepareStatement(sql);
                 ResultSet rs = stmt.executeQuery()) {
                
                if (rs.next()) {
                    int count = rs.getInt("count");
                    System.out.println("t_carousel 表中有效记录数: " + count);
                }
            }
        } catch (SQLException e) {
            System.err.println("查询失败: " + e.getMessage());
            
            // 如果查询失败,尝试不带条件的查询
            try (Connection connection = dataSource.getConnection()) {
                String sql = "SELECT COUNT(*) as count FROM t_carousel";
                try (PreparedStatement stmt = connection.prepareStatement(sql);
                     ResultSet rs = stmt.executeQuery()) {
                    
                    if (rs.next()) {
                        int count = rs.getInt("count");
                        System.out.println("t_carousel 表总记录数: " + count);
                    }
                }
            } catch (SQLException e2) {
                System.err.println("简单查询也失败: " + e2.getMessage());
            }
        }
    }
}