lrj
4 天以前 4fa9591629721797386fc11836e3a9deb69cd58c
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
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();
        }
    }
}