全局配置统一时间格式

在做项目时,数据库查到的时间都是格林尼治时间,从而导致前端收到的时间非常冗杂,所以修改时间格式尤为重要!

第一种方法

在实体类上注解@JsonFormat可以解决,都是一个一个的弄非常麻烦

下面是全局时间格式的配置:

首先修改yml

  jackson:
    time-zone: GMT+8
    date-format: yyyy-MM-dd HH:mm:ss
    default-property-inclusion: non_null # JSON处理时忽略非空字段

在spring后加上以上配置

在自己的WebMvcConfigurer实现类中加入以下方法即可

 
    @Value("${spring.jackson.date-format}")
    private String pattern;
/**
     * 时间处理
     * 使用此方法, 以下 spring-boot: jackson时间格式化 配置 将会失效
     * spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
     * spring.jackson.time-zone=GMT+8
     * 原因: 会覆盖 @EnableAutoConfiguration 关于 WebMvcAutoConfiguration 的配置
     * */
    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();

        //localDateTime格式化
        JavaTimeModule module = new JavaTimeModule();
        LocalDateTimeDeserializer dateTimeDeserializer = new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(pattern));
        LocalDateTimeSerializer dateTimeSerializer = new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(pattern));
        module.addDeserializer(LocalDateTime.class, dateTimeDeserializer);
        module.addSerializer(LocalDateTime.class, dateTimeSerializer);
        ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json().modules(module)
                .featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS).build();

        //date时间格式化
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        objectMapper.setDateFormat(new SimpleDateFormat(pattern));

        // 设置格式化内容
        converter.setObjectMapper(objectMapper);
        converters.add(0, converter);

    }

 

随便测试查询带返回时间的接口可以看到所有时间都变为了统一的格式

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
FieldFill 是 MyBatis-Plus 中的一个实用功能,可以自动填充实体类中指定字段的值,例如创建时间、更新时间等。全局配置 FieldFill 可以在项目启动时统一配置,避免在每个实体类中单独配置,可以提高开发效率。 在 MyBatis-Plus 的配置文件中,可以通过以下方式进行全局配置: 1. 继承 GlobalConfig 类,重写 fill() 方法,进行全局配置,例如: ``` @Configuration public class MybatisPlusConfig { @Bean public GlobalConfig globalConfig() { GlobalConfig globalConfig = new GlobalConfig(); globalConfig.setMetaObjectHandler(new MyMetaObjectHandler()); return globalConfig; } public class MyMetaObjectHandler extends MetaObjectHandler { @Override public void insertFill(MetaObject metaObject) { //设置创建时间和更新时间 this.strictInsertFill(metaObject, "createTime", LocalDateTime::now, LocalDateTime.class); this.strictInsertFill(metaObject, "updateTime", LocalDateTime::now, LocalDateTime.class); } @Override public void updateFill(MetaObject metaObject) { //设置更新时间 this.strictUpdateFill(metaObject, "updateTime", LocalDateTime::now, LocalDateTime.class); } } } ``` 2. 使用 MybatisPlusInterceptor,进行全局配置,例如: ``` @Configuration public class MybatisPlusConfig { @Bean public MybatisPlusInterceptor mybatisPlusInterceptor() { MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); interceptor.addInnerInterceptor(new MyMetaObjectHandler()); return interceptor; } public class MyMetaObjectHandler extends AbstractSqlParserHandler implements MetaObjectHandler { @Override public void insertFill(MetaObject metaObject) { //设置创建时间和更新时间 this.strictInsertFill(metaObject, "createTime", LocalDateTime::now, LocalDateTime.class); this.strictInsertFill(metaObject, "updateTime", LocalDateTime::now, LocalDateTime.class); } @Override public void updateFill(MetaObject metaObject) { //设置更新时间 this.strictUpdateFill(metaObject, "updateTime", LocalDateTime::now, LocalDateTime.class); } } } ``` 以上两种方式都是通过自定义 MetaObjectHandler,并将其设置到 GlobalConfig 或 MybatisPlusInterceptor 中,实现全局 FieldFill 配置

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天辰尽落

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值