为了确保在任务管理的增删改查操作中,create_time 和 update_time 字段能够正确更新,我们对以下文件进行了修改:
sql/task_tables.sqlcreate_time 字段添加默认值:DEFAULT CURRENT_TIMESTAMPupdate_time 字段添加默认值和自动更新:DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMPsql/fix_update_time.sqlruoyi-system/src/main/resources/mapper/system/SysTaskMapper.xmlinsertSysTask)updateTime 不为 null 时才包含该字段update_time 字段,使用 now() 函数设置当前时间updateSysTask)updateTimeassignTask)now() 函数#{updateTime} 参数,由 Service 层控制updateTaskStatus)now() 函数#{updateTime} 参数,由 Service 层控制deleteSysTaskByTaskId 和 deleteSysTaskByTaskIds)update_time = now() 确保删除时也更新时间ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysTaskServiceImpl.javainsertSysTask)task.setUpdateTime(DateUtils.getNowDate()) 确保创建时也设置更新时间updateSysTask)updateTimeassignTask)task.setUpdateTime(DateUtils.getNowDate()) 确保分配时更新时间changeTaskStatus)task.setUpdateTime(DateUtils.getNowDate()) 确保状态变更时更新时间create_time:设置为当前时间update_time:设置为当前时间create_by:设置为当前用户update_by:设置为当前用户update_time:设置为当前时间update_by:设置为当前用户update_time:设置为当前时间(通过 SQL 的 now() 函数)update_time:设置为当前时间update_by:设置为当前用户update_time:设置为当前时间update_by:设置为当前用户actual_start_time 或 actual_end_timesql/task_tables.sql 创建表sql/fix_update_time.sql 脚本修复现有表结构-- 查看表结构
SHOW CREATE TABLE sys_task;
-- 验证字段定义
DESCRIBE sys_task;
create_time 和 update_time 是否正确设置update_time 是否正确更新update_time 是否正确更新update_time 是否正确更新update_time 是否正确更新now() 函数对性能影响很小sql/task_tables.sql - 数据库表结构定义sql/fix_update_time.sql - 数据库修复脚本ruoyi-system/src/main/resources/mapper/system/SysTaskMapper.xml - MyBatis 映射文件ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysTaskServiceImpl.java - Service 实现类