当前位置 博文首页 > zx1323的博客:springBoot项目 时间格式化 踩坑备忘

    zx1323的博客:springBoot项目 时间格式化 踩坑备忘

    作者:[db:作者] 时间:2021-09-01 19:21

    时区

    springBoot配置

    spring:
      jackson:
        date-format: yyyy-MM-dd HH:mm:ss
        time-zone: GMT+8

    数据库连接配置
    &serverTimezone=Asia/Shanghai

    fastjson 序列化时间类型字段的格式
    @JSONField(format="yyyy-MM-dd HH:mm:ss")

    https://blog.csdn.net/moshowgame/article/details/84139443
    Springboot 3种全局时间格式化?https://blog.csdn.net/weixin_38004638/article/details/108433289

    mybatis-plus注解
    @TableField(fill = FieldFill.INSERT)
    @TableField(fill = FieldFill.INSERT_UPDATE)

    mybatis-plus handle类

    @Component
    public class MyBatisPlusMetaObjectHandler implements MetaObjectHandler {
        @Override
        public void insertFill(MetaObject metaObject) {
            this.setFieldValByName("create_time",new Date(),metaObject);
            this.setFieldValByName("update_time",new Date(),metaObject);
        }
    
        @Override
        public void updateFill(MetaObject metaObject) {
            this.setFieldValByName("update_time",new Date(),metaObject);
        }
    }
    

    表设计

    字段初始默认值
    CURRENT_TIMESTAMP
    设置非null, update_time字段勾选"根据当前时间戳更新"

    参考:?https://blog.csdn.net/weixin_45007916/article/details/106793657

    //===================================================

    MySQL 中存储时间的最佳实践 https://zhuanlan.zhihu.com/p/386702884

    cs