HibernateUtil 大全 待修复

package com.chnu.util;

//import org.hibernate.Session;
import java.util.List;

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

//import com.yao.domain.Student;

final public class HibernateUtil {
	
	private static SessionFactory sessionFactory = null;
	private static ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
	
	public HibernateUtil(){
		
	}
	static{
		sessionFactory =  new Configuration().configure().buildSessionFactory();
	}
	//获取全新的session
	public static Session openSession(){
		return sessionFactory.openSession();
	}
	
	//获取和线程关联的session
	public static Session getCurrentSession(){
		
		Session session = threadLocal.get();
		//判断是否得到
		//add by yyy   || !session.isOpen()
		if(session == null  || !session.isOpen()){
			session = sessionFactory.openSession();
			//session对象设置到threadLocal -- 相当于session和线程绑定
			threadLocal.set(session);
		}
		return session;
	}
	//提供一个通过Id号返回对象的方法
	public static  Object queryById(Class clazz, java.io.Serializable id){
		Session session = null;
		Transaction tx = null;
		Object obj = null;
		try {
			session = getCurrentSession();
			tx = session.beginTransaction();
			
			obj = session.load(clazz, id);
			
			tx.commit();
			
			System.out.println("完成");
		} catch (Exception e) {

			if(tx != null){
				tx.rollback();
			}
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}finally{
			if(session !=null && session.isOpen()){
				session.close();
			}
		}
		return obj;
	}
	
	//统一的查询语句
	public static List executeQuery(String hql, String[] params){
		Session session = null;
//		Transaction tx = null;
		Query query = null;
		List list = null;
		session = openSession();
		try {
//			tx = session.beginTransaction();
			query = session.createQuery(hql);
			if(params!=null && params.length > 0){
				for(int i=0; i < params.length; i++){
					query.setString(i, params[i]);
				}
			}
			list = query.list();
//			tx.commit();
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
			// TODO: handle exception
		}finally{
			if(session != null && session.isOpen()){
				session.close();
			}
		}
		return list;
	}
	
	//统一的分页查询--只得到一个页的结果
	public static List queryPage(String hql, String[] params, int pageSize, int currentPage){
		Session session = null;
		Transaction tx = null;
		List list = null;
		Query query = null;
		
		session = getCurrentSession();
		tx = session.beginTransaction();
		query = session.createQuery(hql);
		if(params!=null && params.length > 0){
			for(int i=0; i < params.length; i++){
				query.setString(i, params[i]);
			}
		}
		//增加分页时的设置信息
		query.setFirstResult((currentPage-1)*pageSize).setMaxResults(pageSize);
		list = query.list();
		try {
			tx.commit();
		} catch (Exception e) {
			if(tx != null){
				tx.rollback();
			}
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
			// TODO: handle exception
		}finally{
			if(session != null && session.isOpen()){
				session.close();
			}
		}
		return list;
		
	}
	
	/*统一的添加方法*/
	public static void save(Object obj){
		Session session = null;
		Transaction tx = null;
		session = getCurrentSession();
		tx = session.beginTransaction();
		
		session.save(obj);
		
		try {
			tx.commit();
		} catch (Exception e) {
			if(tx != null){
				tx.rollback();
			}
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}finally{
			if(session != null && session.isOpen()){
				session.close();
			}
		}
	}
	
	/*统一的更新删除*/
	public static void updateAndDelete(String hql, String[] params){
		
		Session session = null;
		Transaction tx = null;
		try {
			session = getCurrentSession();
			tx = session.beginTransaction();
			Query query = session.createQuery(hql);
			if(params!=null && params.length > 0){
				for(int i=0; i<params.length; i++){
					query.setString(i, params[i]);
				}
			}
			query.executeUpdate();
			tx.commit();
			for(String s : params){
				System.out.println(s);
			}
//			System.out.println(params.toString());
			System.out.println("完成");
		} catch (Exception e) {

			if(tx != null){
				tx.rollback();
			}
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}finally{
			if(session !=null && session.isOpen()){
				session.close();
			}
		}
	}
	/*
	 * 获取一个对象的方法
	 */
	public static Object getObjectOnlyOne(String hql, String[] params){
		Session session = null;
		Transaction tx = null;
		Object obj = null;
		Query query = null;
		try {
			session = getCurrentSession();
			tx = session.beginTransaction();
			query = session.createQuery(hql);
			if(params.length != 0){
				for(int i=0; i < params.length; i ++){
					query.setString(i, params[i]);
				}
			}
			obj = query.uniqueResult();
			tx.commit();
			
		} catch (Exception e) {
			if(tx != null){
				tx.rollback();
			}
			throw new RuntimeException("查询一个对象方法 "+e.getMessage());
			
		}finally{
			if(session != null && session.isOpen()){
				session.close();
			}
		}
		return obj;
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值