package com.rongyichuang.config; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Component; @Component public class DatabaseMigration implements CommandLineRunner { @Autowired private JdbcTemplate jdbcTemplate; @Override public void run(String... args) throws Exception { System.out.println("🔄 开始执行数据库迁移..."); try { // 检查description字段是否已存在 String checkSql = "SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS " + "WHERE TABLE_SCHEMA = 'ryc' AND TABLE_NAME = 't_employee' AND COLUMN_NAME = 'description'"; Integer count = jdbcTemplate.queryForObject(checkSql, Integer.class); if (count == 0) { // 添加description字段到表末尾 String alterSql = "ALTER TABLE t_employee ADD COLUMN description varchar(255)"; jdbcTemplate.execute(alterSql); System.out.println("✅ 成功添加description字段到t_employee表"); } else { System.out.println("ℹ️ description字段已存在,无需添加"); } } catch (Exception e) { System.err.println("❌ 数据库迁移失败: " + e.getMessage()); e.printStackTrace(); } } }