package com.rongyichuang; 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 java.util.List; import java.util.Map; /** * 数据库连接测试 */ @SpringBootTest public class DatabaseConnectionTest { @Autowired private JdbcTemplate jdbcTemplate; @Test public void testDatabaseConnection() { try { // 测试数据库连接 String sql = "SELECT 1 as test"; Integer result = jdbcTemplate.queryForObject(sql, Integer.class); System.out.println("数据库连接测试成功,结果: " + result); // 查询所有表 String tablesSql = "SHOW TABLES"; List> tables = jdbcTemplate.queryForList(tablesSql); System.out.println("数据库中的表:"); for (Map table : tables) { System.out.println("- " + table.values().iterator().next()); } } catch (Exception e) { System.err.println("数据库连接测试失败: " + e.getMessage()); e.printStackTrace(); } } @Test public void testTableStructure() { try { // 获取表结构信息 String[] tables = {"t_activity", "t_judge", "t_player", "t_rating_scheme", "t_carousel", "t_employee", "t_media", "t_tag"}; for (String tableName : tables) { try { String sql = "DESCRIBE " + tableName; List> columns = jdbcTemplate.queryForList(sql); System.out.println("\n表 " + tableName + " 的结构:"); for (Map column : columns) { System.out.println(" " + column.get("Field") + " - " + column.get("Type") + (column.get("Null").equals("NO") ? " NOT NULL" : "") + (column.get("Key").equals("PRI") ? " PRIMARY KEY" : "")); } } catch (Exception e) { System.out.println("表 " + tableName + " 不存在或查询失败: " + e.getMessage()); } } } catch (Exception e) { System.err.println("查询表结构失败: " + e.getMessage()); e.printStackTrace(); } } }