LocalDateTime整合到SpringBoot + MyBatis中
Posted lyjlyjlyj
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LocalDateTime整合到SpringBoot + MyBatis中相关的知识,希望对你有一定的参考价值。
啥也不说先贴两个代码:
一:MVC层配置
@Configuration public class JacksonConfig { /** 默认日期时间格式 */ public static final String DEFAULT_DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss"; /** 默认日期格式 */ public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd"; /** 默认时间格式 */ public static final String DEFAULT_TIME_FORMAT = "HH:mm:ss"; @Bean public ObjectMapper objectMapper(){ ObjectMapper objectMapper = new ObjectMapper(); return initObjectMapper(objectMapper); } public static ObjectMapper initObjectMapper(ObjectMapper om) { om.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); om.disable(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE); JavaTimeModule javaTimeModule = new JavaTimeModule(); javaTimeModule.addSerializer(LocalDateTime.class,new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT))); javaTimeModule.addSerializer(LocalDate.class,new LocalDateSerializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT))); javaTimeModule.addSerializer(LocalTime.class,new LocalTimeSerializer(DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMAT))); javaTimeModule.addDeserializer(LocalDateTime.class,new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT))); javaTimeModule.addDeserializer(LocalDate.class,new LocalDateDeserializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT))); javaTimeModule.addDeserializer(LocalTime.class,new LocalTimeDeserializer(DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMAT))); om.registerModule(javaTimeModule).registerModule(new ParameterNamesModule()); return om; } }
二、关于数据层MyBatis需要配置pom新增依赖:
<!--mybatis对LocalDateTime的支持--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-typehandlers-jsr310</artifactId> <version>1.0.1</version> </dependency> <!--json对LocalDateTime的支持--> <dependency> <groupId>com.fasterxml.jackson.datatype</groupId> <artifactId>jackson-datatype-jsr310</artifactId> <version>2.9.9</version> </dependency>
以上是关于LocalDateTime整合到SpringBoot + MyBatis中的主要内容,如果未能解决你的问题,请参考以下文章
springboo整合elasticSearch8 java client api
springboo整合elasticSearch8 java client api
Swagger2 常用使用 及 SpringBoo 整合 Swagger2