你的位置:首页 > 软件开发 > Java > Myeclipse下使用Hibernate的小程序

Myeclipse下使用Hibernate的小程序

发布时间:2016-05-29 00:00:07
首先参考 http://jingyan.baidu.com/article/456c463b99f4370a583144a8.html创建一个项目,照着上面教程到最后一步的时候,系统自动生成entity类,dao类,sessionfactory类和对应的配置文件 在 ...

首先参考 http://jingyan.baidu.com/article/456c463b99f4370a583144a8.html

创建一个项目,照着上面教程到最后一步的时候,系统自动生成entity类,dao类,sessionfactory类和对应的配置文件

 

Myeclipse下使用Hibernate的小程序

 

在下面新建一个Test1.java

需要注意的是,当我只调用dao类save的方法时,数据并没有插入成功,再加入事务的语句后,数据才成功的保存到了数据库中。

package com.pyf.test2;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;import com.pyf.test.*;public class Test1 {  /**   * @param args   */  public static void main(String[] args) {    // TODO Auto-generated method stub    // 必须加入事务    User user = new User(5,"aa",5);        UserDAO userdao = new UserDAO();    /*    Session session = HibernateSessionFactory.getSession();    session.beginTransaction();    session.save(user);    session.getTransaction().commit();    session.close();    */    //必须加入事务    Transaction ts = userdao.getSession().beginTransaction();    userdao.save(user);    ts.commit();    userdao.getSession().close();      }}

原标题:Myeclipse下使用Hibernate的小程序

关键词:eclipse

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

可能感兴趣文章

我的浏览记录