构建hibernate

时间:2023-03-09 08:42:58
构建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; } }