springboot localdatetime序列化 springboot mybatis里localdatetime序列化问题的解决

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

springboot localdatetime序列化 springboot mybatis里localdatetime序列化问题的解决

张占岭   2021-03-30 我要评论

问题起因

主要是使用mybatis作为ORM之后,返回的对象为Map,然后对于数据库的datetime,datestamp类型返回为时间戳而不是标准的时间,这个问题解决方案有两种,大叔分析一下:

1.在mapper的select里,使用mysql这些数据库的函数,dateformat进行转化,缺点,单元测试里使用h2数据库时会找不到这些函数

2.在ObjectMapper反序列化时统一进行处理,这种方式更好,与具体数据库解耦了

实现

>引用依赖包

 'org.mybatis:mybatis-typehandlers-jsr310:1.0.2',
 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.9.2'

>添加组件类

/**
 * 序列化localdatetime处理.
 */
@Component
public class JacksonConfig {

 /**
 * 注入时间处理.
 *
 * @return
 */
 @Bean
 @Primary
 public ObjectMapper objectMapper() {
 ObjectMapper mapper = new ObjectMapper();
 mapper.registerModule(new JSR310Module());
 mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"));
 return mapper;
 }

}

>成功解决问题

{
 "pageCurrent": 1,
 "pageSize": 10,
 "pageTotal": 1,
 "data": [
  {
   "freeDays": 8,
   "city": "",
   "leadingPerson": "",
   "contactPerson": "zhangsan",
   "source": 1,
   "customerName": "i-counting",
   "intention": 1,
   "province": "",
   "appointmentTime": "2018-09-20T00:00:00.000Z",
   "createTime": "2018-09-27T06:33:49.000Z",
   "telephoneStatus": 1,
   "id": 10000,
   "contactPhone": "135"
  }
 ]
}

猜您喜欢

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们