数据库日期时间类型
数据库表
CREATE TABLE `Student` (
`StudentId` bigint unsigned NOT NULL PRIMARY KEY AUTO_INCREMENT,
`Name` varchar(100) NOT NULL,
`CreateTime` date NOT NULL,
PRIMARY KEY (`StudentId`),
KEY `StudentName` (`Name`)
) ENGINE=InnoDB;
实体类
package com.gxz.entities;
import java.sql.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table
public class Student {
private long id;
private String name;
private Date createTime;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "StudentId")
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
}
持久化
Student student = new Student();
student.setName("张三");
student.setCreateTime(new Date(new SimpleDateFormat("yyyy-MM-dd").parse("2016-01-01").getTime()));
manager.persist(student);
查看数据库表student的数据,如下所示。Student student = new Student();
student.setName("张三");
student.setCreateTime(new Date(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2016-01-01 01:01:01").getTime()));
manager.persist(student);
持久化后,查看数据库表student的数据,如下所示。 CREATE TABLE `Student` (
`StudentId` bigint unsigned NOT NULL PRIMARY KEY AUTO_INCREMENT,
`Name` varchar(100) NOT NULL,
`CreateTime` datetime NOT NULL,
KEY `StudentName` (`Name`)
) ENGINE=InnoDB;
持久化的时间代码如下所示。student.setCreateTime(new Date(new SimpleDateFormat("yyyy-MM-dd").parse("2016-01-01").getTime()));
student.setCreateTime(new Date(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2016-01-01 01:01:01").getTime()));
数据库表
实体类
package com.gxz.entities;
import java.sql.Timestamp;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table
public class Student {
private long id;
private String name;
private Timestamp createTime;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "StudentId")
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Timestamp getCreateTime() {
return createTime;
}
public void setCreateTime(Timestamp createTime) {
this.createTime = createTime;
}
}
持久化
Student student = new Student();
student.setName("张三");
student.setCreateTime(new Timestamp(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2016-01-02 02:01:01").getTime()));
manager.persist(student);
持久化后,查看数据库表student的数据,如下所示。 数据库表
CREATE TABLE `Student` (
`StudentId` bigint unsigned NOT NULL PRIMARY KEY AUTO_INCREMENT,
`Name` varchar(100) NOT NULL,
`CreateTime` time NOT NULL,
KEY `StudentName` (`Name`)
) ENGINE=InnoDB;
实体类
package com.gxz.entities;
import java.sql.Time;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table
public class Student {
private long id;
private String name;
private Time createTime;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "StudentId")
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Time getCreateTime() {
return createTime;
}
public void setCreateTime(Time createTime) {
this.createTime = createTime;
}
}
持久化
Student student = new Student();
student.setName("张三");
student.setCreateTime(new Time(new SimpleDateFormat("HH:mm:ss").parse("02:01:01").getTime()));
manager.persist(student);
持久化后,查看数据库表student的数据,如下所示。实体类属性是java.util.Calendar或java.util.Date
数据库表(DateTime类型)
实体类(Calendar类型)
package com.gxz.entities;
import java.util.Calendar;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
@Entity
@Table
public class Student {
private long id;
private String name;
private Calendar createTime;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "StudentId")
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Temporal(TemporalType.TIMESTAMP)
public Calendar getCreateTime() {
return createTime;
}
public void setCreateTime(Calendar createTime) {
this.createTime = createTime;
}
}
持久化(Calendar类型)
Student student = new Student();
student.setName("张三");
Calendar calendar = Calendar.getInstance();
calendar.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2016-05-12 12:25:23"));
student.setCreateTime(calendar);
持久化后,查看数据库表student的数据,如下所示。 数据库表(Date类型)
实体类(java.util.Date类型)
package com.gxz.entities;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
@Entity
@Table
public class Student {
private long id;
private String name;
private Date createTime;
@Temporal(TemporalType.DATE)
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "StudentId")
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
持久化(java.util.Date类型)
Student student = new Student();
student.setName("张三");
student.setCreateTime(new SimpleDateFormat("yyyy-MM-dd").parse("2016-05-13"));
manager.persist(student);
日期时间JPA映射