使用Hibernate轻松解决java中date与datetime类型不兼容的问题

     今天在没事,跟着尚学堂写一个论坛的程序。在写入时间时,发现了问题。在建立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 );

 

实体类如下:

 

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;
    }

}

 

测试类

 

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);
    }
    
}

 

后经过测试,插入时间为:2008-01-029 04:04:36.0,和使用 new java.text.SimpleDateFormat("yy-MM-dd HH:mm:ss").format(new java.util.date())得出的结果一样。

当然从数据库读出来了可以使用SimpleDateFormat类来实现。

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值