Hibernate初学者,不加SessionFactory,自己写

BaseHibernate

 1 package com.proj.dao;
 2 
 3 import java.io.Serializable;
 4 
 5 import org.hibernate.HibernateException;
 6 import org.hibernate.Session;
 7 import org.hibernate.SessionFactory;
 8 import org.hibernate.Transaction;
 9 import org.hibernate.cfg.Configuration;
10 
11 public class BaseHibernateDao {
12 
13     protected SessionFactory sf ;
14     protected Session session;
15     
16     public SessionFactory getSessionFactory(){
17         Configuration cfg = new Configuration().configure();
18         return cfg.buildSessionFactory();
19     }
20     public void getSession(){
21          sf = this.getSessionFactory();
22          session = sf.openSession();
23     }
24     public Serializable add(Object obj){
25         Serializable id = null;
26         getSession();
27         Transaction tx= null;
28         try {
29             tx = session.beginTransaction();
30             id= session.save(obj);
31             tx.commit();
32         } catch (HibernateException e) {
33             if(tx!=null){
34                 tx.rollback();
35             }
36             e.printStackTrace();
37         }finally{
38             
39             session.close();
40         }
41         return id;
42     }
43     public void update(Object obj){
44         getSession();
45         Transaction tx= null;
46         try {
47             tx = session.beginTransaction();
48             session.update(obj);
49             tx.commit();
50         } catch (HibernateException e) {
51             if(tx!=null){
52                 tx.rollback();
53             }
54             e.printStackTrace();
55         }finally{
56             
57             session.close();
58         }
59     }
60     public Object get(Class clazz,Serializable id){
61         Object obj = null;
62         this.getSession();
63         
64         try {
65             obj = session.get(clazz, id);
66         } catch (HibernateException e) {
67             // TODO Auto-generated catch block
68             e.printStackTrace();
69         }finally{
70             session.close();
71         }
72         
73         return obj;
74     }
75     public void delete(Class clazz,Serializable id){
76         Object obj = null;
77         this.getSession();
78         obj = session.get(clazz, id);
79         Transaction tx= null;
80         try {
81             tx = session.beginTransaction();
82             session.delete(obj);
83             tx.commit();
84         } catch (HibernateException e) {
85             if(tx!=null){
86                 tx.rollback();
87             }
88             e.printStackTrace();
89         }finally{
90             session.close();
91         }
92     }
93 }

Dao类

 1 package com.proj.dao;
 2 
 3 import java.util.List;
 4 
 5 import org.hibernate.Query;
 6 import org.hibernate.Session;
 7 import org.hibernate.SessionFactory;
 8 import org.hibernate.Transaction;
 9 import org.hibernate.cfg.Configuration;
10 
11 import com.proj.entity.Stu;
12 
13 public class StuDao extends BaseHibernateDao {
14 
15     public List<Stu> getAll() {
16         Configuration cfg = new Configuration().configure();
17         SessionFactory sf = cfg.buildSessionFactory();
18         Session session = sf.openSession();
19         String hql = "from Stu";
20         Query query = session.createQuery(hql);
21         session.close();
22         return query.list();
23     }
24     public Stu getById(String studentId){
25         return (Stu)super.get(Stu.class, studentId);    
26     }
27     public String addStu(Stu stu){
28         return (String)super.add(stu);
29     }
30     public void updateStu(Stu stu){
31         super.update(stu);
32     }
33     public void deleteStu(String studentId){
34         super.delete(Stu.class, studentId);
35     }
36 }

 

转载于:https://www.cnblogs.com/Mr-Wu/archive/2012/09/10/SessionFactoy.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值