使用hibernate开发持久层时,我们会发现:虽然entity类的含义和需求不同,其对应的Dao层类对应的方法也是不同的。但是有许多方法操作确实相同的。比如实体的增加,删除,修改更新,以及许多常用的查询方法。这些都是可复用的。因此可以把这些操作写在一个BaseDao中,其他的dao都继承于这个Dao。每个子dao只写与自己的业务相关的方法,这样可以提高代码的复用,增加了开发效率,也方便今后可能的扩展。下面是我在我的项目中使用的BaseDao和BaseDaoImpl的使用方法。仅供参考:
BaseDao:
package com.bupt.auth.dao.base; import java.io.Serializable;
import java.util.List; public interface BaseDao<T> {
Long save(T entity); //保存实体类 void delete(Long id); //删除实体类 void update(T entity); //更新实体 T getById(Long id); //通过id获得实体 List<T> getByIds(Long[] ids);//根据id数组获得对应的实体数组 List<T> findAll();//获得全部的实体 Long totalNum();//实体类的数量 List<T> getPage(int pageNow, int pageSize);//分页查找 List<T> find(String hql , String param);//根据具体的hql语句查找实体类 }
BaseDaoImpl:
package com.bupt.auth.dao.base; import java.lang.reflect.ParameterizedType;
import java.util.Collections;
import java.util.List; import javax.annotation.Resource; import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Transactional; @Transactional
@SuppressWarnings("unchecked")
public abstract class BaseDaoImpl<T> implements BaseDao<T> {
@Resource
private SessionFactory sessionFactory; private Class<T> clazz=null; @SuppressWarnings("unchecked")
public BaseDaoImpl(){
ParameterizedType pt=(ParameterizedType)this.getClass().getGenericSuperclass();
this.clazz = (Class<T>) pt.getActualTypeArguments()[0];
}
public Session getSession() {
return sessionFactory.getCurrentSession();
} public void setSessionFactory(SessionFactory sessionFactory)
{
this.sessionFactory = sessionFactory;
}
public SessionFactory getSessionFactory()
{
return this.sessionFactory;
}
public Long save(T entity) {
return (Long)getSession().save(entity);
} public void delete(Long id) {
// TODO Auto-generated method stub
Object object = getById(id);
if(object != null){
getSession().delete(object);
}
} public void update(T entity) {
// TODO Auto-generated method stub
getSession().update(entity);
} public T getById(Long id) {
// TODO Auto-generated method stub
if(id != null){
return (T)getSession().get(clazz, id);
}else{
return null;
}
} public List<T> getByIds(Long[] ids) {
// TODO Auto-generated method stub\
if(ids == null || ids.length == 0){
return Collections.emptyList();
}else{
return getSession().createQuery("from "+
clazz.getSimpleName() + " where id in(:ids)").setParameterList("ids",ids).list();
}
} public List<T> find(String hql , String param)
{
// 创建查询
Query query = getSession()
.createQuery(hql);
// 为包含占位符的HQL语句设置参数 query.setParameter("1",param); return (List<T>)query.list();
} public List<T> findAll() {
// TODO Auto-generated method stub
return getSession().createQuery("from " + clazz.getSimpleName()).list();
} public Long totalNum() {
// TODO Auto-generated method stub
return (Long)getSession().createQuery("select count(*) from " + clazz.getSimpleName()).uniqueResult();
} public List<T> getPage(int pageNow, int pageSize) {
// TODO Auto-generated method stub
return getSession().createQuery("from " + clazz.getSimpleName()).setFirstResult((pageNow - 1) * pageSize).setMaxResults(pageSize).list();
} /*@SuppressWarnings("unchecked")
public T get(Class<T> entityClazz , String id)
{
return (T)((SessionFactory) getSession()).getCurrentSession()
.get(entityClazz , id);
}*/
}