在插入数据库以及从数据库查询出的时间数据发现比当前时间少了8个小时,需要增加一些配置来解决这个问题。
application.properties
# 数据库配置
spring.datasource.url=jdbc:mysql://localhost:3306/bookstore?useSSL=false&useUnicode=true&characterEncoding=utf-8&useLegacyDatetimeCode=false&serverTimezone=UTC
# 统一时区
spring.jackson.time-zone=GMT+8
实体层配置
// Entity => Vip
// Date返回类型的getter方法
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
public Date getVipCreateTime() {
return vipCreateTime;
}
启动类
package com.bookstore;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import javax.annotation.PostConstruct;
import javax.servlet.MultipartConfigElement;
import java.util.TimeZone;
/**
* <p>后台启动类</p>
* ====================
* @version 1.0
* @author noTalent
*/
@MapperScan("com.bookstore.mapper")
@SpringBootApplication
public class BookstoreApplication {
public static void main(String[] args) {
SpringApplication.run(BookstoreApplication.class, args);
}
/**
* 服务器时区设置
* 解决服务端时差问题
*/
@PostConstruct
void setDefaultTimezone() {
TimeZone.setDefault(TimeZone.getTimeZone("GMT+8"));
}
}