0. Main Contents:
0.1 Troubleshooting
- LocalDateTime, LocalDate, dateFormat in configuration files does not take effect
- – SpringBoot+Mybatis: “SpringBoot+Mybatis:” “SpringBoot+Mybatis:” “SpringBoot+Mybatis:” “SpringBoot+Mybatis:” “SpringBoot+Mybatis:” “SpringBoot+Mybatis:” “SpringBoot+Mybatis:”
0.1 Operating Environment
- This machine is MacOS 10.14
- Mysql 5.7
- Server CentOS 7
- SpringBoot 2.1.7. RELEASE
- Mybatis: Starter version 2.1.0
1. Correct “posture” for LocalDateTime in SpringBoot
- I like to say the right thing first
1.1 Configuration File
spring:
jackson:
# timeZone: GMT+8 invalid parameter
# dateFormat: YYYY-MM-DD HH: MM :ss is also invalid
Copy the code
1.2 depend on
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.7</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.9.7</version>
</dependency>
Copy the code
- The first bag above is not said
- The second package is used to support JSR310 datatype, which we understand is used for Jackson to support Local time
1.3 configuration class
@Configuration
public class LocalDateConfig {
/** * Date Format string */
private static final String DATE_FORMAT = "yyyy-MM-dd";
/** * DateTime Format string */
private static final String DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
/** * Time Format string */
private static final String TIME_FORMAT = "HH:mm:ss";
/** * Add custom *@return Jackson2ObjectMapperBuilderCustomizer
*/
@Bean
public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer(a) {
return builder -> builder
.serializerByType(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(DATETIME_FORMAT)))
.serializerByType(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern(DATE_FORMAT)))
.serializerByType(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern(TIME_FORMAT)))
.deserializerByType(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(DATETIME_FORMAT)))
.deserializerByType(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern(DATE_FORMAT)))
.deserializerByType(LocalTime.class, newLocalTimeDeserializer(DateTimeFormatter.ofPattern(TIME_FORMAT))); }}Copy the code
- Set up
- LocalDateTime: yyyY-MM-DD HH: MM: SS
Mysql > select * from SpringBoot;
- Simply add the time zone to the connection address, but it’s not GMT+8
- It’s like this:
spring:
datasource:
druid:
url: jdbc:mysql://server.address:3306/ilss_blog? useSSL=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
Copy the code
-
You read that right, it’s Asia/Shanghai
-
The optional value can be in /usr/share/zoneinfo, but you can’t write Asia directly or Shanghai directly. You need to write Asia/Shanghai from the relative path of zoneinfo
-
Isn’t it easy? Go ahead and try it.
-
Thank you to those who don’t know which blogs you read.
About me
- Majoring in Computer Science and technology, general university, Hangzhou.
- Graduated in 20 years, mainly engaged in back-end development of Java technology stack.
- GitHub: github.com/imyiren
- Blog : imyi.ren