1、首先定义泛型DAO的接口。
2、给出基于HibernateDaoSupport类的具体实现。
3、在web.xml里注册Spring的OpenSessionInViewFilter。
4、继承基础DAO类即可重用DAO类方法。
具体的调用方式如下:
提供出DeptDao的Spring配置文件片段:
package com.ys.common.dao;
import java.io.Serializable;
import java.util.List;
public interface IBaseDao<T, ID extends Serializable> {
/**
* 保存实体
*
* @param entity
* 实体类
*/
public void save(T entity);
/**
* 删除实体
*
* @param entity
* 实体类
*/
public void delete(T entity);
/**
* 根据实体id 删除实体
*
* @param entityClass
* 实体类
* @param id
* 实体id
*/
public void deleteById(Class<T> entityClass, ID id);
/**
* 更新实体
*
* @param entity
* 实体类
*/
public void update(T entity);
/**
* 根据实体id 查询单个实体
*
* @param entityClass
* 实体类
* @param id
* 实体id
* @return
*/
public T findById(Class<T> entityClass, ID id);
/**
* 列出所有实体集合
*
* @param entityClass
* 实体类
* @return 实体类List
*/
public List<T> findAll(Class<T> entityClass);
/**
* 根据实体参数,查询符合条件的实体类集合
*
* @param hql
* @param values
* 参数
* @return
*/
public List<Object> find(String hql, Object... values);
}
2、给出基于HibernateDaoSupport类的具体实现。
package com.ys.common.dao;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public class BaseHibernateDao<T, ID extends Serializable> extends
HibernateDaoSupport implements IBaseDao<T, ID> {
public void delete(T entity) {
this.getHibernateTemplate().delete(entity);
}
public void deleteById(Class<T> entityClass, ID id) {
delete(this.findById(entityClass, id));
}
@SuppressWarnings("unchecked")
public T findById(Class<T> entityClass, ID id) {
return (T) this.getHibernateTemplate().get(entityClass, id);
}
@SuppressWarnings("unchecked")
public List<T> findAll(Class<T> entityClass) {
String name = entityClass.getName();
return this.getHibernateTemplate().find("from " + name);
}
public void save(T entity) {
this.getHibernateTemplate().save(entity);
}
public void update(T entity) {
this.getHibernateTemplate().update(entity);
}
@SuppressWarnings("unchecked")
public List<Object> find(String hql, Object... values) {
return this.getHibernateTemplate().find(hql, values);
}
}
3、在web.xml里注册Spring的OpenSessionInViewFilter。
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
4、继承基础DAO类即可重用DAO类方法。
package com.ys.system.dept.dao;
import com.ys.common.dao.BaseHibernateDao;
import com.ys.system.dept.bean.SysDept;
public class DeptDao extends BaseHibernateDao<SysDept, Integer> {
}
具体的调用方式如下:
package com.ys.system.dept.service;
import java.util.List;
import com.ys.system.dept.bean.SysDept;
import com.ys.system.dept.dao.DeptDao;
public class DeptService implements IDeptService {
//Spring容器依赖注入
private DeptDao deptDao;
public DeptDao getDeptDao() {
return deptDao;
}
public void setDeptDao(DeptDao deptDao) {
this.deptDao = deptDao;
}
public void delete(SysDept dept) {
deptDao.delete(dept);
}
public SysDept findById(int deptId) {
return deptDao.findById(SysDept.class, deptId);
}
public void save(SysDept dept) {
deptDao.save(dept);
}
public void update(SysDept dept) {
deptDao.update(dept);
}
}
提供出DeptDao的Spring配置文件片段:
<bean id="deptDao" class="com.ys.system.dept.dao.DeptDao" scope="singleton">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="deptService" class="com.ys.system.dept.service.DeptService">
<property name="deptDao" ref="deptDao"></property>
</bean>