首页 > 代码库 > 封装HibernateUtil(静态单例模式)和增删改查方法

封装HibernateUtil(静态单例模式)和增删改查方法

封装HibernateUtil

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {
    //定义常量是为了实现单例,不让随便new
    //ThreadLocal<Session>不是集合,是线程锁,为了单线程安全
    private static final ThreadLocal<Session> lock = new ThreadLocal<Session>();
    
    private static final SessionFactory factory = buildFactory();
    private static SessionFactory buildFactory() {
        Configuration cfg = new Configuration().configure();//读取 hibernate.cfg.xml 的配置信息
        return cfg.buildSessionFactory();
    }
    
    public static Session getSession() {
        //先获取线程锁中的session
        Session session = lock.get();
        
        if (session == null) {//如果没有session就新建一个session赋值给lock
            session = factory.openSession();
            lock.set(session);
        }
        
        return session;
    }
    public static void closeSession() {
        
        Session session = lock.get();//先获取线程锁中的session
        
        if (session != null) {//如果session有值,那么关闭session
            session.close();
            lock.set(null);    //将session关闭之后再给lock赋值null,方便其他线程使用    
        }
    }
    
}

 

增删改查方法封装,使用时是调取该类中的具体方法即可

import java.util.List;
import org.hibernate.Session;
import org.hibernate.query.Query;
import maya.model.Sto;
import maya.util.SearchItem;

public class StoDAO {
    private Session session = null;
    public StoDAO() {//构造函数,引用HibernateUtil类的getSession方法
        session = HibernateUtil.getSession();
    }

//添加。要用 事务来控制,另外,增、改也需要
    public void insert(Sto data) {
        data = null;
        try {
            session.beginTransaction();//开始事务
            data = http://www.mamicode.com/new Sto();
            data.setStorno(data.getStorno());
            data.setStorname(data.getStorname());
            data.setStoset(data.getStoset());
            data.setStoaddr(data.getStoaddr());
            data.setWindow(data.getWindow());
            data.setTelno1(data.getTelno1());
            
            session.save(data);//添加到session中
            session.getTransaction().commit();//提交事务
        } catch (Exception e) {
            session.getTransaction().rollback();//抛出异常,事务回滚至开始
            e.printStackTrace();
        } finally {
            HibernateUtil.closeSession();//关闭事务
        }  
    }

//删除
    public void delete(String storno) {
        try {            
            Sto data = session.load(Sto.class, storno);//先查出要删除的对象            
            if (data != null) {
                session.beginTransaction();
                session.delete(data);//将删除信息添加至session                    
                session.getTransaction().commit();//提交事务
            }
        } catch (Exception e) {
            e.printStackTrace();
            session.getTransaction().rollback();
        } finally {
            HibernateUtil.closeSession();//关闭
        }
    }

//修改
    public void update(Sto data) {
        
        try {            
            Sto sto = session.get(Sto.class, data.getStorno());//先查出要修改的对象
            if (data != null) {
                session.beginTransaction();//开始事务
                sto.setStorname(data.getStorname());
                sto.setStoset(data.getStoset());
                sto.setStoaddr(data.getStoaddr());
                sto.setWindow(data.getWindow());
                sto.setTelno1(data.getTelno1());
                
                session.update(sto);//将修改信息添加至session
                session.getTransaction().commit();//提交事务
            }
        } catch (Exception e) {
            e.printStackTrace();
            session.getTransaction().rollback();
            
        } finally {
            session.close();//关闭
        }
    }

//查单个,不用依靠事务控制
        public Sto select2(String storno) {
            Sto sto = null;
            try {
                sto = (Sto)session.createQuery("from Sto where storno=?")
                        .setParameter(0, storno)//第一个问号用0来表示,第二个问号用1,以此类推
                        .getSingleResult();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                HibernateUtil.closeSession();//调用HibernateUtil类的close方法来关闭
            }
            return sto;
        }
//查集合
    public List<Sto> select() {
        List<Sto> list = null;
        try {
            list = session.createQuery("from Sto").getResultList();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            HibernateUtil.closeSession();
        }
        return list;
    }

}

 

封装HibernateUtil(静态单例模式)和增删改查方法