mybatis類型處理器JSR310標(biāo)準(zhǔn)詳解
類型處理器JSR310標(biāo)準(zhǔn)
首先什么是JSR310標(biāo)準(zhǔn),其實(shí)就是新出的一些日期類型等的標(biāo)準(zhǔn)在,mybatisplus3.4之后會(huì)自己識(shí)別,但是在之前的版本就需要手動(dòng)進(jìn)行引入。
<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中的日期類型關(guān)系就可以使用JSR310的類型進(jìn)行對(duì)應(yīng)了,
LocalDate
映射數(shù)據(jù)庫(kù)中的date類型LocalTime
來(lái)映射數(shù)據(jù)庫(kù)中的time類型LocalDateTime
字段來(lái)映射數(shù)據(jù)庫(kù)中的datetime類型
使Jackson和Mybatis支持JSR310標(biāo)準(zhǔn)
1、首先要確保Jackson和Mybatis正確地整合進(jìn)項(xiàng)目了
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來(lái)映射數(shù)據(jù)庫(kù)中的date類型字段了,可以用LocalTime來(lái)映射數(shù)據(jù)庫(kù)中的time類型字段了,可以用LocalDateTime字段來(lái)映射數(shù)據(jù)庫(kù)中的datetime類型字段了
4、為L(zhǎng)ocalDate/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. }
至此,這些私有域會(huì)被轉(zhuǎn)化成一個(gè)類似 'time_field' : '12:01:00'這樣格式,而不是'time_field' : {.....}這樣的格式
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
RabbitMQ開(kāi)啟SSL與SpringBoot連接測(cè)試的配置方法
本文基于 CentOS 7 + Git + OpenSSL + yum 安裝的 RabbitMQ,需要讀者提交安裝好。其他方式也可變通參考本文。對(duì)RabbitMQ開(kāi)啟SSL與SpringBoot連接測(cè)試相關(guān)知識(shí)感興趣的朋友一起看看吧2022-01-01Spring Boot 如何將 Word 轉(zhuǎn)換為 PDF
這篇文章主要介紹了Spring Boot將Word轉(zhuǎn)換為 PDF,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-08-08基于SpringBoot與Mybatis實(shí)現(xiàn)SpringMVC Web項(xiàng)目
這篇文章主要介紹了基于SpringBoot與Mybatis實(shí)現(xiàn)SpringMVC Web項(xiàng)目的相關(guān)資料,需要的朋友可以參考下2017-04-04Spring中的@Value和@PropertySource注解詳解
這篇文章主要介紹了Spring中的@Value和@PropertySource注解詳解,@PropertySource:讀取外部配置文件中的key-value保存到運(yùn)行的環(huán)境變量中,本文提供了部分實(shí)現(xiàn)代碼,需要的朋友可以參考下2023-11-11Java Lambda表達(dá)式的方法引用和構(gòu)造器引用實(shí)例分析
這篇文章主要介紹了Java Lambda表達(dá)式的方法引用和構(gòu)造器引用,結(jié)合實(shí)例形式分析了Lambda表達(dá)式的方法引用和構(gòu)造器引用相關(guān)原理、用法及操作注意事項(xiàng),需要的朋友可以參考下2019-09-09SpringMVC中的HttpServletRequestWrapper使用解析
這篇文章主要介紹了SpringMVC中的HttpServletRequestWrapper使用解析,HttpServletRequestWrapper 采用裝飾者模式對(duì)HttpServletRequest進(jìn)行包裝,我們可以通過(guò)繼承HttpServletRequestWrapper類去重寫(xiě)getParameterValues,getParameter等方法,需要的朋友可以參考下2024-01-01