打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
HibernateSessionFactoryUtil类和封装了(增删改查)方法的HibernateTest类
HibernateSessionFactoryUtil类提供了一个getSessionFactory()静态方法。
通过调用此方法可以返回一个SessionFactory对象。
在其他地方创建Session对象的时候:
只需要这样一句代码:
Session session = HibernateSessionFactoryUtil.getSessionFactory().getCurrentSession();
这样既可获得Session对象。
HibernateTest类封装了对数据操作更删改查的基本方法。
根据不同的业务逻辑环境代码作出相应的变更即可使用。
HibernateSessionFactoryUtil.java
[java] view plain copy
package com.xiami.util;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateSessionFactoryUtil {
private static final SessionFactory sessionFactory;
static {
try {
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
throw new ExceptionInInitializerError(ex);
}
}
private HibernateSessionFactoryUtil() {
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
HibernateTest.java
[java] view plain copy
package com.xiami.examples;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.xiami.util.HibernateSessionFactoryUtil;
public class HibernateTest {
public static void main(String args[]){
HibernateTest test = new HibernateTest();
//增加一条记录]
//      Guestbook gb = new Guestbook();
//      gb.setContent("我是内容");
//      gb.setCreatedTime("2012-03-17");
//      gb.setEmail("kalision@foxmail.com");
//      gb.setName("mr.zhou");
//      gb.setPhone("12345678912");
//      gb.setTitle("我的信息");
//      test.addGuestbook(gb);
//删除一条记录
//      test.deleteGuestbook(7);
//修改更新一条记录
//      Guestbook gb = test.getGuestbook(1);
//      gb.setName("admin");
//      test.updateGuest(gb);
//得到一条记录
Guestbook gb = test.getGuestbook(1);
System.out.println(gb.getName());
}
/*
* 增加一条记录的方法
*/
public void addGuestbook(Guestbook gb){
Session session = HibernateSessionFactoryUtil.getSessionFactory().getCurrentSession();
Transaction tx = session.beginTransaction();
session.save(gb);
tx.commit();
}
/*
* 删除一条记录的方法
*/
public void deleteGuestbook(Integer id){
Session session = HibernateSessionFactoryUtil.getSessionFactory().getCurrentSession();
Transaction tx = session.beginTransaction();
//第一种方法.先得到对应id的记录的对象,然后在删除此对象对应的记录。
//      Guestbook gb = (Guestbook) session.get(Guestbook.class, new Integer(id));
//      tx.commit();
//      session = HibernateSessionFactoryUtil.getSessionFactory().getCurrentSession();
//      tx = session.beginTransaction();
//      session.delete(gb);
//      tx.commit();
//第二种方法。直接调用本类中的getGuestbook()方法来得到要对应id的对象。直接就删除了。
Guestbook gb = getGuestbook(id);
session.delete(gb);
tx.commit();
}
/*
* 修改一条记录的方法(更新)
*/
public void updateGuest(Guestbook gb){
Session session = HibernateSessionFactoryUtil.getSessionFactory().getCurrentSession();
Transaction tx = session.beginTransaction();
session.update(gb);
tx.commit();
}
/*
* 查询一条记录的方法
*/
public Guestbook getGuestbook(Integer id){
Session session = HibernateSessionFactoryUtil.getSessionFactory().getCurrentSession();
Transaction tx = session.beginTransaction();
Guestbook gb = new Guestbook();
gb = (Guestbook) session.get(Guestbook.class, new Integer(id));
return gb;
}
}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Hibernate映射---多对一关联关系
Hibernate HQL查询
浅谈OSIV与泛型DAO模式 - java专题论坛-java.csecs.com
关于Hibernate Could not obtain transaction
getCurrentSession()和openSession()的区别
第一章:struts2框架与hibernate框架
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服