From 3e91d53f89c98c4b650103c52c0a4dad2e106dc4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=86=AF=E8=BE=89?= Date: Thu, 25 Jun 2026 15:43:36 +0800 Subject: [PATCH] =?UTF-8?q?sql=E8=AF=AD=E6=B3=95=E5=85=BC=E5=AE=B9?= =?UTF-8?q?=E9=97=AE=E9=A2=98=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../impl/InterviewInvitationServiceImpl.java | 40 ++++++++++++++----- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/ruoyi-bussiness/src/main/java/com/ruoyi/cms/service/impl/InterviewInvitationServiceImpl.java b/ruoyi-bussiness/src/main/java/com/ruoyi/cms/service/impl/InterviewInvitationServiceImpl.java index 4606e79..51be6ea 100644 --- a/ruoyi-bussiness/src/main/java/com/ruoyi/cms/service/impl/InterviewInvitationServiceImpl.java +++ b/ruoyi-bussiness/src/main/java/com/ruoyi/cms/service/impl/InterviewInvitationServiceImpl.java @@ -100,22 +100,42 @@ public class InterviewInvitationServiceImpl implements InterviewInvitationServic log.info("interview_invitation 表创建成功(序列自增)!"); } else { log.info("interview_invitation 表结构正常,检查新增列..."); - // 为已有数据库添加新列(使用 IF NOT EXISTS,兼容 PostgreSQL / HighGo) - try { - stmt.execute("ALTER TABLE interview_invitation ADD COLUMN IF NOT EXISTS company_name varchar(200) NULL"); - } catch (Exception e) { log.warn("添加 company_name 列失败: {}", e.getMessage()); } - try { - stmt.execute("ALTER TABLE interview_invitation ADD COLUMN IF NOT EXISTS job_name varchar(200) NULL"); - } catch (Exception e) { log.warn("添加 job_name 列失败: {}", e.getMessage()); } - try { - stmt.execute("ALTER TABLE interview_invitation ADD COLUMN IF NOT EXISTS interviewer_name varchar(100) NULL"); - } catch (Exception e) { log.warn("添加 interviewer_name 列失败: {}", e.getMessage()); } + // 为已有数据库添加新列:先查 information_schema 判断列是否存在,存在则跳过 + // 不使用 IF NOT EXISTS / DO $$ 语法,因为 Druid 1.2.23 StatFilter 无法解析这些 PG 扩展语法 + addColumnIfNotExists(stmt, "interview_invitation", "company_name", "varchar(200) NULL"); + addColumnIfNotExists(stmt, "interview_invitation", "job_name", "varchar(200) NULL"); + addColumnIfNotExists(stmt, "interview_invitation", "interviewer_name", "varchar(100) NULL"); } } catch (Exception e) { log.error("初始化表失败: {}", e.getMessage()); } } + /** + * 幂等添加列:先查 information_schema 判断列是否存在,不存在才执行 ALTER TABLE ADD COLUMN + * 不使用 IF NOT EXISTS / DO $$ 语法,避免 Druid StatFilter mergeSql 报错 + */ + private void addColumnIfNotExists(Statement stmt, String tableName, String columnName, String columnDef) { + try { + String checkSql = "SELECT COUNT(*) FROM information_schema.columns WHERE table_name='" + tableName + "' AND column_name='" + columnName + "'"; + java.sql.ResultSet rs = stmt.executeQuery(checkSql); + boolean exists = false; + if (rs.next()) { + exists = rs.getInt(1) > 0; + } + rs.close(); + if (!exists) { + String alterSql = "ALTER TABLE " + tableName + " ADD COLUMN " + columnName + " " + columnDef; + stmt.execute(alterSql); + log.info("成功添加列: {}.{}", tableName, columnName); + } else { + log.info("列已存在,跳过: {}.{}", tableName, columnName); + } + } catch (Exception e) { + log.warn("添加列 {}.{} 失败: {}", tableName, columnName, e.getMessage()); + } + } + @Override public List getInterviewInvitationList(InterviewInvitation interviewInvitation) { return interviewInvitationMapper.getInterviewInvitationList(interviewInvitation);