构建hibernate

 

package hanqi.dao;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

import hanqi.entity.Tbankcard;

public class TbankcardDAO {
    
    Configuration cfg = null;
    ServiceRegistry sr = null;
     
    SessionFactory sf = null;
    Session se = null;
    Transaction tr= null;
    
    
    public TbankcardDAO()
    {
        //加载配置文件
        cfg  =  new Configuration().configure();
        
        //注册服务
        sr = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).build();
    }
    
    
    private void init()
    {
        //
        sf = cfg.buildSessionFactory(sr);
        se = sf.openSession();
        tr = se.beginTransaction();
    }
    
    
    private void destroy()
    {
        tr.commit();
        se.close();
        sf.close();
    }
    
    
    //单条查询
    public Tbankcard getTBbankcard(String cardid)
    {
        Tbankcard rtn = null;
        
        init();
        
        rtn = (Tbankcard)se.get(Tbankcard.class, cardid);
        
        
        destroy();
        
        
        return rtn;
        
    }

}

 

posted @ 2016-11-28 10:28  张好好  阅读(135)  评论(0编辑  收藏  举报