Hibernate开发中的BaseDao

整理了一下Hibernate开发中的BaseDao

package com.qfedu.common.dao.impl;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.query.Query;

import com.qfedu.common.dao.IBaseDao;
import com.qfedu.common.entity.WoPage;
import com.qfedu.common.util.WoUtil;

// @Repository
@SuppressWarnings(value = { "unchecked" })
public class BaseDao implements IBaseDao {
	private final static Logger LOG = LogManager.getLogger(BaseDao.class);

	private Class entityClass;
	private String entityName;

	public BaseDao() {
		// 获取当前父类的泛型
		ParameterizedType pt = (ParameterizedType) this.getClass().getGenericSuperclass();
		// 获取E的实际Class
		entityClass = (Class) pt.getActualTypeArguments()[0];
		entityName = entityClass.getSimpleName();
	}

	/**
	 * 被子类继承,从而注入hibernate的SessionFactory
	 */
	@Resource
	protected SessionFactory sessionFactory;

	/**
	 * 
	 * @return
	 */
	protected Session getSession() {
		return sessionFactory.getCurrentSession();
	}

	@Override
	public void create(E entity) {
		getSession().persist(entity);
	}

	@Override
	public void delete(E entity) {
		getSession().delete(entity);
	}

	@Override
	public boolean deleteById(Serializable id) {
		E entity = findById(id);
		if (entity != null) {
			delete(entity);
			return true;
		}
		return false;
	}

	@Override
	public void update(E entity) {
		getSession().merge(entity);
	}

	@Override
	public E findById(Serializable id) {
		return (E) getSession().get(entityClass, id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.qf.wobb.dao.IBaseDao#findBy(java.util.Map)
	 */
	public E findBy(Map props) {
		List list = findAllBy(props);
		return list.size() == 1 ? list.get(0) : null;
	}

	public List findAll() {
		return findAllBy(null);
	}

	/**
	 * 根据props中的key作为查询的属性名,value作为属性值,来查询对象列表。
	 * Map props = new HashMap();
	 * props.put ("loginName", "admin");
	 * props.put ("password", "123");
	 * @see com.qfedu.common.dao.IBaseDao#findAllBy(java.util.Map)
	 */
	public List findAllBy(Map props) {
		String hql = "";
		if (props != null && props.size() > 0) {
			for (String key : props.keySet()) {
				if (!hql.equals("")) {
					hql += " and ";
				}
				hql += " u." + key + " = :" + key.replace('.', '_');
			}
			hql = "from " + entityName + " u where " + hql;
		} else {
			hql = "from " + entityName + " u";
		}
		LOG.info(hql);
		Query query = getSession().createQuery(hql, entityClass);
		if (props != null && props.size() > 0) {
			for (String key : props.keySet()) {
				query.setParameter(key.replace('.', '_'), props.get(key));
			}
		}
		return (List) query.getResultList();
	}

	@Override
	public WoPage findAllBy(String whereOrOrderBy, Long start, Long limit, Map props) {
		// 获取分页数据
		String hql = "from " + entityName + " e ";
		if (!WoUtil.isEmpty(whereOrOrderBy)) {
			String upper = whereOrOrderBy.toUpperCase().trim();
			if (upper.startsWith("ORDER ") || upper.startsWith("WHERE ")) {
				hql += whereOrOrderBy;
			} else {
				hql += " where " + whereOrOrderBy;
			}
		}
		Query query = getSession().createQuery(hql, entityClass);
		for (String key : props.keySet()) {
			query.setParameter(key, props.get(key));
		}
		List list = (List) query.setMaxResults(limit.intValue()).setFirstResult(start.intValue()).list();
		// 获取总数量
		String countHql = "select count(*) " + hql;
		query = getSession().createQuery(countHql);
		for (String key : props.keySet()) {
			query.setParameter(key, props.get(key));
		}
		Long total = (Long) query.uniqueResult();
		return new WoPage(list, total);
	}

	@Override
	public List findAllBy(String whereOrOrderBy, Map props) {
		// 获取数据列表
		String hql = "from " + entityName + " e ";
		if (!WoUtil.isEmpty(whereOrOrderBy)) {
			String upper = whereOrOrderBy.toUpperCase().trim();
			if (upper.startsWith("ORDER ") || upper.startsWith("WHERE ")) {
				hql += whereOrOrderBy;
			} else {
				hql += " where " + whereOrOrderBy;
			}
		}
		Query query = getSession().createQuery(hql, entityClass);
		for (String key : props.keySet()) {
			query.setParameter(key, props.get(key));
		}
		List list = (List) query.list();
		return list;
	}
}

 

你可能感兴趣的:(Hibernate开发中的BaseDao)