关于BaseDAO要注意的问题

、引用茶叶写的一个baseDAO

package com.juno.dao.service;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import org.hibernate.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Component;

/**
 * 所有Dao类的父类
 * 
 * @author macrotea 2011-2-31
 * @param <T>
 */

@Component("baseDao")
public class BaseDao<T, ID> extends HibernateDaoSupport {
	private static final Logger logger = LoggerFactory.getLogger(BaseDao.class);

	/**
	 * 设置Dao将操作的实体
	 */
	private Class<T> entityClazz;

	public Class<T> getEntityClazz() {
		return entityClazz;
	}

	public void setEntityClazz(Class<T> entityClazz) {
		this.entityClazz = entityClazz;
	}

	public BaseDao() {
		super();
	}

	public BaseDao(Class<T> entityClazz) {
		this.entityClazz = entityClazz;
	}

	/**
	 * 设置hibernateTemplate所需的SessionFactory
	 */
	@Resource
	public void setLocalSessionFactory(SessionFactory sessionFactory) {
		super.setSessionFactory(sessionFactory);
	}

	/**
	 * 获得所有实体记录
	 */
	@SuppressWarnings("unchecked")
	public List<T> findAll() throws Exception {
		return getHibernateTemplate().find("FROM " + entityClazz.getSimpleName());
	}

	/**
	 * 保存一个实体
	 */
	@SuppressWarnings("unchecked")
	public ID save(T obj) throws Exception {
		return (ID) getHibernateTemplate().save(obj);
	}

	/**
	 * 保存/更新一个实体
	 */
	public void saveOrUpdate(T obj) throws Exception {
		getHibernateTemplate().saveOrUpdate(obj);
	}

	/**
	 * 根据id获得一个实体
	 */
	@SuppressWarnings("unchecked")
	public T findById(int id) throws Exception {
		T retVal = (T) getHibernateTemplate().get(entityClazz, id);
		if (retVal == null) {
			logger.debug("get:该ID:" + id + " 的实体不存在!");
		}
		return retVal;
	}

	/**
	 * 根据id加载一个实体
	 */
	@SuppressWarnings("unchecked")
	public T loadById(int id) throws Exception {
		T retVal = (T) getHibernateTemplate().load(entityClazz, id);
		return retVal;
	}

	/**
	 * 根据id删除一个实体
	 */
	public void deleteById(int id) throws Exception {
		T entity = loadById(id);
		if (entity == null) {
			String msg = "delete:该ID:" + id + " 的实体不存在!";
			throw new Exception(msg);
		}
		getHibernateTemplate().delete(entity);
	}

	/**
	 * 获得所有实体记录总数
	 */
	@SuppressWarnings("unchecked")
	public Long countAll() throws Exception {
		List retVal = getHibernateTemplate().find("SELECT count(*) FROM " + entityClazz.getSimpleName());
		return (Long) retVal.get(0);
	}

	/**
	 * 根据属性和属性值查找
	 */
	@SuppressWarnings("unchecked")
	public List findByProperty(String propertyName, Object value) throws Exception {
		System.out.println("findByProperty value:" + value);
		List retVal = getHibernateTemplate().find("from " + entityClazz.getSimpleName() + " as model where model." + propertyName + "= ?", value);
		return retVal;
	}

	/**
	 * 根据多个id查找
	 */
	public List<T> findByIdArray(Integer[] idArray) throws Exception {
		List<T> retVal = new ArrayList<T>();
		T model = null;
		for (int i = 0; i < idArray.length; i++) {
			model = findById(idArray[i]);
			retVal.add(model);
		}
		return retVal;
	}
}


然后自己写一个dao类来继承basedao
package com.juno.dao.service.impl;

import org.springframework.stereotype.Component;

import com.juno.bean.Hello;
import com.juno.dao.service.BaseDao;
import com.juno.dao.service.HelloDAO;

/**
 * @author Juno
 * 
 */
@Component("helloDao")
public class HelloDAOImpl extends BaseDao<Hello, Integer> implements HelloDAO {
	// property constants
	public static final String USERNAME = "username";
	public static final String AGE = "age";

	/*
	 * 如下方式调用父类方法:super.XXX()
	 */
	public HelloDAOImpl() {
		super(Hello.class);
	}

	@Override
	public void update(Hello Hello) throws Exception {

	}
}
  这时候、一定要记住加上
	public HelloDAOImpl() {
		super(Hello.class);
	}
这个构造方法!要不无效!

猜你喜欢

转载自lyuno.iteye.com/blog/1014528