在建立MySQL表使,发帖时间用的datetime类型。并且用系统函数now()来实现。在Java实体类中用的java.util.date类型。发现这是犯愁了,众所周至,java中时间类型一直是让人头疼的一个问题。
硬着头皮上,终于发现原来hibernate已经帮我们解决了问题。
MySQL简表语句如下:
create database bbs;
use bbs;
create table article
(
id int primary key auto_increment,
pid int,
rootid int,
title varchar(255),
cont text,
pdate datetime,
isleaf int #1-not leaf 0-leaf
);
insert into article values (null, 0, 1, '蚂蚁大战大象', '蚂蚁大战大象', now(), 1);
insert into article values (null, 1, 1, '大象被打趴下了', '大象被打趴下了',now(), 1);
insert into article values (null, 2, 1, '蚂蚁也不好过','蚂蚁也不好过', now(), 0);
insert into article values (null, 2, 1, '瞎说', '瞎说', now(), 1);
insert into article values (null, 4, 1, '没有瞎说', '没有瞎说', now(), 0);
insert into article values (null, 1, 1, '怎么可能', '怎么可能', now(), 1);
insert into article values (null, 6, 1, '怎么没有可能', '怎么没有可能', now(), 0);
insert into article values (null, 6, 1, '可能性是很大的', '可能性是很大的', now(), 0);
insert into article values (null, 2, 1, '大象进医院了', '大象进医院了', now(), 1);
insert into article values (null, 9, 1, '护士是蚂蚁', '护士是蚂蚁', now(), 0);
use bbs;
create table article
(
id int primary key auto_increment,
pid int,
rootid int,
title varchar(255),
cont text,
pdate datetime,
isleaf int #1-not leaf 0-leaf
);
insert into article values (null, 0, 1, '蚂蚁大战大象', '蚂蚁大战大象', now(), 1);
insert into article values (null, 1, 1, '大象被打趴下了', '大象被打趴下了',now(), 1);
insert into article values (null, 2, 1, '蚂蚁也不好过','蚂蚁也不好过', now(), 0);
insert into article values (null, 2, 1, '瞎说', '瞎说', now(), 1);
insert into article values (null, 4, 1, '没有瞎说', '没有瞎说', now(), 0);
insert into article values (null, 1, 1, '怎么可能', '怎么可能', now(), 1);
insert into article values (null, 6, 1, '怎么没有可能', '怎么没有可能', now(), 0);
insert into article values (null, 6, 1, '可能性是很大的', '可能性是很大的', now(), 0);
insert into article values (null, 2, 1, '大象进医院了', '大象进医院了', now(), 1);
insert into article values (null, 9, 1, '护士是蚂蚁', '护士是蚂蚁', now(), 0);
实体类如下:
package database;
import java.util.Date;
/**
* Article entity.
*
* @author smartcat86
*/
public class Article implements java.io.Serializable {
// Fields
private Integer id;
private Integer pid;
private Integer rootid;
private String title;
private String cont;
private Date pdate;
private Integer isleaf;
// Constructors
/** default constructor */
public Article() {
}
/** full constructor */
public Article(Integer pid, Integer rootid, String title, String cont,
Date pdate, Integer isleaf) {
this.pid = pid;
this.rootid = rootid;
this.title = title;
this.cont = cont;
this.pdate = pdate;
this.isleaf = isleaf;
}
// Property accessors
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getPid() {
return this.pid;
}
public void setPid(Integer pid) {
this.pid = pid;
}
public Integer getRootid() {
return this.rootid;
}
public void setRootid(Integer rootid) {
this.rootid = rootid;
}
public String getTitle() {
return this.title;
}
public void setTitle(String title) {
this.title = title;
}
public String getCont() {
return this.cont;
}
public void setCont(String cont) {
this.cont = cont;
}
public Date getPdate() {
return this.pdate;
}
public void setPdate(Date pdate) {
this.pdate = pdate;
}
public Integer getIsleaf() {
return this.isleaf;
}
public void setIsleaf(Integer isleaf) {
this.isleaf = isleaf;
}
}
import java.util.Date;
/**
* Article entity.
*
* @author smartcat86
*/
public class Article implements java.io.Serializable {
// Fields
private Integer id;
private Integer pid;
private Integer rootid;
private String title;
private String cont;
private Date pdate;
private Integer isleaf;
// Constructors
/** default constructor */
public Article() {
}
/** full constructor */
public Article(Integer pid, Integer rootid, String title, String cont,
Date pdate, Integer isleaf) {
this.pid = pid;
this.rootid = rootid;
this.title = title;
this.cont = cont;
this.pdate = pdate;
this.isleaf = isleaf;
}
// Property accessors
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getPid() {
return this.pid;
}
public void setPid(Integer pid) {
this.pid = pid;
}
public Integer getRootid() {
return this.rootid;
}
public void setRootid(Integer rootid) {
this.rootid = rootid;
}
public String getTitle() {
return this.title;
}
public void setTitle(String title) {
this.title = title;
}
public String getCont() {
return this.cont;
}
public void setCont(String cont) {
this.cont = cont;
}
public Date getPdate() {
return this.pdate;
}
public void setPdate(Date pdate) {
this.pdate = pdate;
}
public Integer getIsleaf() {
return this.isleaf;
}
public void setIsleaf(Integer isleaf) {
this.isleaf = isleaf;
}
}
测试类
package database;
public class Test {
public static void main(String[] args){
DBOperate dbo = new DBOperate(HibernateSessionFactory.getSession());//数据库操作类
Article a = new Article();
a.setId(null);//Hibernate中使native使其自动增加
a.setPid(pid);
a.setRootid(rootId);
a.setTitle(title);
a.setCont(cont);
a.setPdate(new Date());
a.setIsleaf(0);
dbo.insert(a);
}
}
public class Test {
public static void main(String[] args){
DBOperate dbo = new DBOperate(HibernateSessionFactory.getSession());//数据库操作类
Article a = new Article();
a.setId(null);//Hibernate中使native使其自动增加
a.setPid(pid);
a.setRootid(rootId);
a.setTitle(title);
a.setCont(cont);
a.setPdate(new Date());
a.setIsleaf(0);
dbo.insert(a);
}
}
后经过测试,插入时间为:2008-01-029 04:04:36.0,和使用 new java.text.SimpleDateFormat("yy-MM-dd HH:mm:ss").format(new java.util.date())得出的结果一样。
当然从数据库读出来了可以使用SimpleDateFormat类来实现。