mybatis類型處理器JSR310標準詳解
類型處理器JSR310標準
首先什么是JSR310標準,其實就是新出的一些日期類型等的標準在,mybatisplus3.4之后會自己識別,但是在之前的版本就需要手動進行引入。
<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>
引入之后,mybatis中的日期類型關系就可以使用JSR310的類型進行對應了,
LocalDate映射數(shù)據(jù)庫中的date類型LocalTime來映射數(shù)據(jù)庫中的time類型LocalDateTime字段來映射數(shù)據(jù)庫中的datetime類型
使Jackson和Mybatis支持JSR310標準
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來映射數(shù)據(jù)庫中的date類型字段了,可以用LocalTime來映射數(shù)據(jù)庫中的time類型字段了,可以用LocalDateTime字段來映射數(shù)據(jù)庫中的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' : {.....}這樣的格式
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
RabbitMQ開啟SSL與SpringBoot連接測試的配置方法
本文基于 CentOS 7 + Git + OpenSSL + yum 安裝的 RabbitMQ,需要讀者提交安裝好。其他方式也可變通參考本文。對RabbitMQ開啟SSL與SpringBoot連接測試相關知識感興趣的朋友一起看看吧2022-01-01
基于SpringBoot與Mybatis實現(xiàn)SpringMVC Web項目
這篇文章主要介紹了基于SpringBoot與Mybatis實現(xiàn)SpringMVC Web項目的相關資料,需要的朋友可以參考下2017-04-04
Spring中的@Value和@PropertySource注解詳解
這篇文章主要介紹了Spring中的@Value和@PropertySource注解詳解,@PropertySource:讀取外部配置文件中的key-value保存到運行的環(huán)境變量中,本文提供了部分實現(xiàn)代碼,需要的朋友可以參考下2023-11-11
SpringMVC中的HttpServletRequestWrapper使用解析
這篇文章主要介紹了SpringMVC中的HttpServletRequestWrapper使用解析,HttpServletRequestWrapper 采用裝飾者模式對HttpServletRequest進行包裝,我們可以通過繼承HttpServletRequestWrapper類去重寫getParameterValues,getParameter等方法,需要的朋友可以參考下2024-01-01

