这篇文章主要讲解了“Mybatis映射实体怎么解决LocalDateTime格式转换问题”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Mybatis映射实体怎么解决LocalDateTime格式转换问题”吧!
应用场景: Mybatis映射成实体, 返给前端 LocalDateTime 格式数据显示不友好
解决方案: 通过 实体注解 和 mapperObject Bean 解决
步骤一: 实体注解:
@JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MM-dd HH:mm:ss")@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
package com.rry.miniprogram.operation.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonFormat;
import org.springframework.format.annotation.DateTimeFormat;
import java.time.LocalDateTime;
import java.io.Serializable;
/**
* <p>
* 小程序ext参数配置
* </p>
*
* @author bchen
* @since 2021-04-26
*/
public class MExtjs implements Serializable {
private static final long serialVersionUID = 1L;
@TableId(value = "extjs_id", type = IdType.AUTO)
private Long extjsId;
private String remark;
@JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MM-dd HH:mm:ss")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime createTime;
@JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MM-dd HH:mm:ss")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime updateTime;
public Long getExtjsId() {
return extjsId;
}
public void setExtjsId(Long extjsId) {
this.extjsId = extjsId;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
public LocalDateTime getCreateTime() {
return createTime;
}
public void setCreateTime(LocalDateTime createTime) {
this.createTime = createTime;
}
public LocalDateTime getUpdateTime() {
return updateTime;
}
public void setUpdateTime(LocalDateTime updateTime) {
this.updateTime = updateTime;
}
}
步骤二: 设置 mapperObject
/** * 使用此方法, 全局处理实体 LocalDateTime 返回实体问题 需要在配置类: @Configuration * */@Bean(name = "mapperObject")public ObjectMapper getObjectMapper() {
ObjectMapper om = new ObjectMapper();
JavaTimeModule javaTimeModule = new JavaTimeModule();
javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
javaTimeModule.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern("HH:mm:ss")));
om.registerModule(javaTimeModule);return om;
}
经测试, 解决问题;
感谢各位的阅读,以上就是“Mybatis映射实体怎么解决LocalDateTime格式转换问题”的内容了,经过本文的学习后,相信大家对Mybatis映射实体怎么解决LocalDateTime格式转换问题这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://my.oschina.net/u/3681868/blog/5042373