All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.phoenix.basic.impl.HibernateDaoImpl Maven / Gradle / Ivy

Go to download

对hibernate4的封装。封装了Druid,通过Druid可以轻量级的对其他数据库进行操作

There is a newer version: 1.5.1
Show newest version
package org.phoenix.basic.impl;

import java.lang.reflect.ParameterizedType;
import java.util.LinkedList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.ScrollableResults;
import org.hibernate.Session;
import org.phoenix.basic.dao.IBaseDao;
import org.phoenix.basic.utils.HibernateUtil;

/**
 * 通用的Dao方法
 * @author mengfeiyang
 *
 */
@SuppressWarnings("unchecked")
public class HibernateDaoImpl implements IBaseDao{
	private Class clz;
	public Class getClz() {
		if(clz==null) {
			//获取泛型的Class对象
			clz = ((Class)
					(((ParameterizedType)(this.getClass().getGenericSuperclass())).getActualTypeArguments()[0]));
		}
		return clz;
	}
	@Override
	public T add(T t) {
		
		Session session = HibernateUtil.openSession();
		session.beginTransaction();
		session.save(t);
		session.getTransaction().commit();	
		session.close();
		return t;
	}

	@Override
	public void update(T t) {
		Session session = HibernateUtil.openSession();
		session.beginTransaction();
		session.update(t);
		session.getTransaction().commit();		
		session.close();
	}

	@Override
	public void delete(int id) {
		Session session = HibernateUtil.openSession();
		session.beginTransaction();
		T t = load(id);
		session.delete(t);
		session.getTransaction().commit();	
		session.close();
	}

	//使用load会启用延迟加载,get不会
	@Override
	public T load(int id) {
		Session session = null;
		try{
			session = HibernateUtil.openSession();
			T t = (T) session.get(getClz(), id);
			return t;
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(session!=null)session.close();
		}
			return null;
	}
	
	public LinkedList loadRowDatas(String sql){
		LinkedList lists = new LinkedList();
		Session session = HibernateUtil.openSession();
		Query query = session.createSQLQuery(sql);
		ScrollableResults sr = query.scroll();
		while(sr.next()){
			lists.add(sr.get());
		}
		return lists;
	}
	
	public T load(String hql){
			Session session = HibernateUtil.openSession();
			Query query = session.createQuery(hql);
			T t = (T) query.uniqueResult();
			session.close();
			return t;
	}
	
	@Override
	public List loadAll(String hql) {
			Session session = HibernateUtil.openSession();
			Query query = session.createQuery(hql);
			List t = (List) query.list();
			session.close();
			return t;
	}
	@Override
	public List loadAll() {
		Session session = HibernateUtil.openSession();
		Query query = session.createQuery("from "+getClz());
		List t = (List) query.list();
		session.close();
		return t;
	}
	@Override
	public void addBatchData(List t) {
		Session session = HibernateUtil.openSession();
		session.beginTransaction();
		for(T it : t){
			session.save(it);
		}
		session.getTransaction().commit();	
		session.close();
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy