1、首先要確保Jackson和Mybatis正確地整合進項目了
2、添加額外的依賴
<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-typehandlers-jsr310</artifactId> <version>1.0.1</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.datatype</groupId> <artifactId>jackson-datatype-jsr310</artifactId> <version>2.9.2</version> </dependency>
3、至此,Po類中的域,可以用LocalDate來映射數據庫中的date類型字段了,可以用LocalTime來映射數據庫中的time類型字段了,可以用LocalDateTime字段來映射數據庫中的datetime類型字段了
4、為LocalDate/LocalTime/LocalDateTime類型的私用域添加@JsonFormat主鍵,如下所示
public class TimeEntity { private Integer id; @JsonFormat(pattern = "yyyy-MM-dd") private LocalDate date_field; @JsonFormat(pattern = "HH:mm:ss") private LocalTime time_field; @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") private LocalDateTime datetime_field; // Getters and setters ignore. }
至此,這些私有域會被轉化成一個類似 'time_field' : '12:01:00'這樣格式,而不是'time_field' : {.....}這樣的格式
