SPRING BOOT:Hibernate實現的JPA中使用joda-time
joda-time
在java8出現之前很長時間作為Java中處理日期時間的事實標准,但是想要在JPA實體類中使用joda-time需要做以下配置
dependencies
在pom.xml
文件中添加
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
</dependency>
<dependency>
<groupId>org.jadira.usertype</groupId>
<artifactId>usertype.extended</artifactId>
<version>5.0.0.GA</version>
</dependency>
application.properties
在src/main/resources/application.properties
配置文件中添加
spring.jpa.properties.jadira.usertype.autoRegisterUserTypes = true
JPA實體類
// 映射為 DATETIME 類型字段 (MySQL)
private DateTime createTime;
// 映射為 DATE 類型字段 (MySQL)
private LocalDate birthdayDate;
綁定json數據
在pom.xml
中添加
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-joda</artifactId>
</dependency>
在controller中接收json請求
@RequestMapping("/add")
@ResponseBody
public User addUser(@RequestBody User user) {
// ....
}
發送一個Content-Type為application/json的請求, addUser方法將接收一個User實例
{"createTime":"1970-01-01T00:00:00","birthdayDate":"1970-01-01"}
完整代碼:http://git.oschina.net/kevinlieu/spring-boot-jpa-joda-time