hibernate基于泛型基础增删改查方法封装

IBaseDao.java

package com.winssage.fengshu.basedao;

public interface IBaseDao<T> {

	public void save(T entity);

	public void delete(String[] ids);

	public void update(T entity);

	public T find(Long id);

	public void saveOrUpdate(T entity);
}



BaseDaoImpl.java

(这里对HibarnateDaoSupport.java做了进一步封装,自行更改为extends HibarnateDaoSupport即可

package com.winssage.fengshu.basedao;

import java.lang.reflect.ParameterizedType;

import org.apache.log4j.Logger;
import org.hibernate.HibernateException;

@SuppressWarnings("unchecked")
public class BaseDaoImpl<T> extends BaseHibernateSupport implements IBaseDao<T> {

	protected Logger log = Logger.getLogger(this.getClass());

	protected Class<T> clazz;

	/**
	 * 通过反射泛型获取Class类型,getGenericSuperclass()方法获取对象的泛型的父类类型信息,
	 * getActualTypeArguments()[0]方法得到T的真实类型
	 * 
	 */
	public BaseDaoImpl() {
		ParameterizedType pt = (ParameterizedType) this.getClass()
				.getGenericSuperclass();
		this.clazz = (Class<T>) pt.getActualTypeArguments()[0];
	}

	/**
	 * 公共的增,删,改,查方法
	 */

	public void save(T entity) {
		try {
			this.getHibernateTemplate().save(entity);
		} catch (HibernateException sqle) {
			log.error(sqle.toString());
		}

	}

	public void update(T entity) {
		try {
			this.getHibernateTemplate().update(entity);
		} catch (HibernateException sqle) {
			log.error(sqle.toString());
		}

	}

	public void delete(String[] ids) {
		T entity = null;
		try {
			for (int i = 0; i < ids.length; i++) {
				entity = this.find(Long.parseLong(ids[i]));
				this.getHibernateTemplate().delete(entity);
			}
		} catch (HibernateException sqle) {
			log.error(sqle.toString());
		} finally {
			entity = null;
		}

	}

	public T find(Long id) {
		T entity = null;
		try {
			entity = (T) this.getHibernateTemplate().get(clazz, id);
		} catch (HibernateException sqle) {
			entity = null;
			log.error(sqle.toString());
		}
		return entity;
	}

	public void saveOrUpdate(T enity) {

		try {
			this.getHibernateTemplate().saveOrUpdate(enity);

		} catch (RuntimeException re) {

			throw re;
		}
	}

}





用法:

应用层dao继承basedao

public interface IProjectDao  extends IBaseDao<ProjectBase>{

}



应用层impl继承basedaoimpl
public class ProjectBaseDaoImpl extends BaseDaoImpl<ProjectBase> implements
		IProjectDao {

}


重写hibernateSupport

package com.shl.common;

import javax.annotation.Resource;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.dao.support.DaoSupport;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.SessionFactoryUtils;

/**
 * 主要目的是为了实现注解
 * 
 * @author lizhifeng limanman fengshuzi
 *
 */
public class BaseHibernateSupport extends DaoSupport {
	private SessionFactory sessionFactory;
	private HibernateTemplate hibernateTemplate;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	@Resource(name = "sessionFactory")
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
		this.hibernateTemplate = createHibernateTemplate(sessionFactory);
	}

	public Session getSession() {
		if (this.sessionFactory == null) {
			throw new HibernateException("Session Create Fail,SessionFactory is null!");
		}
		return this.sessionFactory.getCurrentSession();
	}

	protected HibernateTemplate createHibernateTemplate(SessionFactory sessionFactory) {
		return new HibernateTemplate(sessionFactory);
	}

	@Override
	protected void checkDaoConfig() throws IllegalArgumentException {
		if (this.hibernateTemplate == null) {
			throw new IllegalArgumentException("'sessionFactory' or 'hibernateTemplate' is required");
		}
	}

	protected final Session getSession(boolean allowCreate) throws DataAccessResourceFailureException, IllegalStateException {
		return (!allowCreate ? SessionFactoryUtils.getSession(getSessionFactory(), false) : SessionFactoryUtils.getSession(getSessionFactory(),

		this.hibernateTemplate.getEntityInterceptor(), this.hibernateTemplate.getJdbcExceptionTranslator()));
	}

	protected final DataAccessException convertHibernateAccessException(HibernateException ex) {
		return this.hibernateTemplate.convertHibernateAccessException(ex);
	}

	protected final void releaseSession(Session session) {
		SessionFactoryUtils.releaseSession(session, getSessionFactory());
		if (null != session)
			session = null;
	}

	public final void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}

	public final HibernateTemplate getHibernateTemplate() {
		return this.hibernateTemplate;
	}
}



后记-查询所有的方法的实现

	@Override
	public List<T> findAll() {
		List<T> list = (List<T>) this.getHibernateTemplate().find(
				"from " + clazz.getName());
		return list;
	}





你可能感兴趣的:(java,增删改查封装,基础crud)