springboot mybatis里localdatetime序列化问题的解决
问题起因
主要是使用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"
}
]
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
相关文章
springboot+Oauth2实现自定义AuthenticationManager和认证path
本篇文章主要介绍了springboot+Oauth2实现自定义AuthenticationManager和认证path,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2017-09-09
ConditionalOnProperty注解的作用和使用方式
在SpringBoot项目开发中,@ConditionalOnProperty注解允许根据配置文件中的属性值来控制配置类是否生效,该注解通过属性name和havingValue来判断配置是否注入,如果application.properties中的对应属性值为空或不匹配havingValue设定值2024-09-09
Spring Security 使用 OncePerRequestFilter
OncePerRequestFilter是一个过滤器,每个请求都会执行一次;一般开发中主要是做检查是否已登录、Token是否过期和授权等操作,而每个操作都是一个过滤器,下面介绍Spring Security 使用 OncePerRequestFilter 过滤器校验登录过期、请求日志等操作方法,感兴趣的朋友一起看看吧2024-06-06


最新评论