【引言】
记录两个最近遇到的问题:
1. SpringBoot集成MybatisPlus连接Mysql问题
(1) 问题描述
之前整合了SpringBoot+Mybatis的项目,最近想在项目中实践一些东西,启动项目,报错如下:
java.sql.SQLException: The server time zone value '�й���ʱ��' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:73)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:76)
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:835)
at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:455)
at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:240)
at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:199)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at com.baomidou.mybatisplus.generator.config.DataSourceConfig.getConn(DataSourceConfig.java:183)
从第一句中,可以定位问题,连接数据库需要指定一个时区。
(2) 问题复现
项目中,连接数据库的配置如下:
# DataSource Config
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true
spring.datasource.username=root
spring.datasource.password=123456
(3) 解决方案
修改数据库连接配置,指定时区,如下:
# DataSource Config
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true**&serverTimezone=UTC**
spring.datasource.username=root
spring.datasource.password=123456
2. LocalDate反序列化问题
(1) 问题描述
一个简单的修改某字段日期的接口,前端传值为日期格式的字符串’2020-03-25’,后端使用对象接收,报错如下:
org.springframework.http.converter.HttpMessageConversionException:
Type definition error: [simple type, class java.time.LocalDate];
nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException:
Cannot construct instance of `java.time.LocalDate`
(no Creators, like default construct, exist):
no String-argument constructor/factory method to deserialize from String value ('2020-03-25')
从最后一句中,我们可以定位问题,日期字段反序列失败。
(2) 问题复现
定义实体类伪代码如下:
@Data
public class infoReq{
/**
* 编号
*/
private Long id;
/**
* 日期
*/
private LocalDate payDate;
}
前端传值如下:
{
"payDate": "2020-03-25",
"id": "22"
}
(3) 解决方案
解决问题的办法很简单,在实体对象字段上增加以下注解即可:
@Data
public class infoReq{
/**
* 编号
*/
private Long id;
/**
* 日期
*/
@JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
@JsonSerialize(using = LocalDateSerializer.class)
@JsonDeserialize(using = LocalDateDeserializer.class)
private LocalDate payDate;
}