Spring-Data-Rest中时间的数据类型
Data type of time in Spring-Data-Rest
我想存储时间 我应该在 spring 框架中使用什么数据类型?
我使用的数据库是 MySQL
@Entity
public class ShopDetail {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String address;
private Double latitude;
private Double longitude;
private float rating;
private Time openingTime;
private Time closingTime;
}
您可以使用日期时间格式。
Spring 3.2 Date time format
您可以在实体中使用 Java 8 种时间类型。只需将此依赖项添加到您的项目中:
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-java8</artifactId>
</dependency>
根据这篇文章The 5 laws of API dates and times我建议在 REST 项目中使用ZonedDateTime
。
也关注这个topic...
如果你用的是Java 8,大部分业务逻辑,实体字段可能会用到Instant-基本class in java.time
API代表 UTC 时间轴中的一个时刻。
如果需要时区数据,可以考虑使用:
ZonedDateTime is an immutable representation of a date-time with a time-zone. This
class stores all date and time fields
另一种选择LocalDateTime:
A date-time without a time-zone in the ISO-8601 calendar system, such
as 2007-12-03T10:15:30
但是你会依赖默认的系统时区,如果系统时区发生变化,这可能会带来矛盾的结果。
检查 or 以获得明确的解释。
我想存储时间 我应该在 spring 框架中使用什么数据类型? 我使用的数据库是 MySQL
@Entity
public class ShopDetail {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String address;
private Double latitude;
private Double longitude;
private float rating;
private Time openingTime;
private Time closingTime;
}
您可以使用日期时间格式。
Spring 3.2 Date time format
您可以在实体中使用 Java 8 种时间类型。只需将此依赖项添加到您的项目中:
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-java8</artifactId>
</dependency>
根据这篇文章The 5 laws of API dates and times我建议在 REST 项目中使用ZonedDateTime
。
也关注这个topic...
如果你用的是Java 8,大部分业务逻辑,实体字段可能会用到Instant-基本class in java.time
API代表 UTC 时间轴中的一个时刻。
如果需要时区数据,可以考虑使用:
ZonedDateTime is an immutable representation of a date-time with a time-zone. This class stores all date and time fields
另一种选择LocalDateTime:
A date-time without a time-zone in the ISO-8601 calendar system, such as 2007-12-03T10:15:30
但是你会依赖默认的系统时区,如果系统时区发生变化,这可能会带来矛盾的结果。
检查