通用DAO代码

import java.util.List; 

public interface BaseDao<M extends java.io.Serializable, PK extends java.io.Serializable> {

	/** 
	 * 按主键取记录
	 * @param id 主键值  
	 * @return 记录实体对象,如果没有符合主键条件的记录,则返回null 
	 */  
	public M findById(PK id); 
	
	/**
	 * 
	 * @param hql hibernate hql查询语句
	 * @param params 查询参数数组
	 * @return 返回一个list集合数据 
	 */
	public List<M> findByHQL(String hql, Object... params);

	/** 
	 * 修改一个实体对象(UPDATE一条记录)
	 * @param entity 实体对象 
	 */  
	public void update(M entity);  
	
	/**
	 * 插入一个实体(在数据库INSERT一条记录)
	 * @param entity 实体对象    
	 */  
	public PK save(M entity);  

	/** 
	 * 增加或更新实体 
	 * @param entity 实体对象 
	 */  
	public void saveOrUpdate(M entity);  
	
	/** 
	 * 删除指定的实体 
	 * @param entity 实体对象 
	 */  
	public void delete(M entity);
	
	/** 
	 * 删除指定id的实体 
	 * @param id pk 
	 */
	public void deleteById(PK id);
}

import java.util.List;
import java.lang.reflect.ParameterizedType; 
import javax.annotation.Resource;  
import org.hibernate.Query;  
import org.hibernate.Session;  
import org.hibernate.SessionFactory;

public abstract class BaseDaoImpl<M extends java.io.Serializable, PK extends java.io.Serializable> implements BaseDao<M, PK> {

	
	protected Class<M> entityClass;

	/**
	 * 向DAO层注入SessionFactory 
	 */
	@Resource
	protected SessionFactory sessionFactory;
	
	/**
	 * 构造方法,根据实例类自动获取实体类类型 
	 */
	@SuppressWarnings("unchecked")
	public BaseDaoImpl() {  
		
		ParameterizedType type = (ParameterizedType) this.getClass().getGenericSuperclass();  
		this.entityClass = (Class<M>) type.getActualTypeArguments()[0];
	}  

	/**
	 * 继承类取得session
	 * @return
	 */
	protected Session currentSession() {  
		//事务必须是开启的(Required),否则获取不到
		return this.sessionFactory.getCurrentSession();  
	}  

	@SuppressWarnings("unchecked")
	@Override
	public M findById(PK id) {
		
		return (M) this.currentSession().get(entityClass, id);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<M> findByHQL(String hql, Object... params) {
		
		Query query = this.currentSession().createQuery(hql);  
		for (int i = 0; params != null && i < params.length; i++) { 
			
			query.setParameter(i, params[i]);  
		}

		return query.list();  
	}

	@Override
	public void update(M entity) {
		
		this.currentSession().update(entity);
	}

	@SuppressWarnings("unchecked")
	@Override
	public PK save(M entity) {
		
		PK id = (PK) this.currentSession().save(entity);
		return id;
	}

	@Override
	public void saveOrUpdate(M entity) {
		
		this.currentSession().saveOrUpdate(entity);
	}

	@Override
	public void delete(M entity) {
		
		this.currentSession().delete(entity);
	}
	
	@Override
	public void deleteById(PK id) {
		
		this.currentSession().delete(this.findById(id));
	}

}

////////////////////////////////////////////////////////
public interface StudentDao extends BaseDao<Student, String> {

	/** 
	 * 添加IBaseDAO 没有定义的方法
	 */
	public void studentMethod();
}

import org.springframework.stereotype.Repository;

@Repository(value = "studentDao")
public class StudentDaoImpl extends BaseDaoImpl<Student, String> implements StudentDao {

	@Override
	public void studentMethod() {
		
		System.out.println("studentMethod execute");
	}
}

猜你喜欢

转载自jaesonchen.iteye.com/blog/2287141