SSH2的泛型DAO

1、首先定义泛型DAO的接口。 
Java代码   收藏代码
  1. package com.ys.common.dao;  
  2.   
  3. import java.io.Serializable;  
  4. import java.util.List;  
  5.   
  6. public interface IBaseDao<T, ID extends Serializable> {  
  7.       
  8.     /** 
  9.      * 保存实体 
  10.      *  
  11.      * @param entity 
  12.      *            实体类 
  13.      */  
  14.     public void save(T entity);  
  15.   
  16.     /** 
  17.      * 删除实体 
  18.      *  
  19.      * @param entity 
  20.      *            实体类 
  21.      */  
  22.     public void delete(T entity);  
  23.   
  24.     /** 
  25.      * 根据实体id 删除实体 
  26.      *  
  27.      * @param entityClass 
  28.      *            实体类 
  29.      * @param id 
  30.      *            实体id 
  31.      */  
  32.     public void deleteById(Class<T> entityClass, ID id);  
  33.   
  34.     /** 
  35.      * 更新实体 
  36.      *  
  37.      * @param entity 
  38.      *            实体类 
  39.      */  
  40.     public void update(T entity);  
  41.   
  42.     /** 
  43.      * 根据实体id 查询单个实体 
  44.      *  
  45.      * @param entityClass 
  46.      *            实体类 
  47.      * @param id 
  48.      *            实体id 
  49.      * @return 
  50.      */  
  51.     public T findById(Class<T> entityClass, ID id);  
  52.   
  53.     /** 
  54.      * 列出所有实体集合 
  55.      *  
  56.      * @param entityClass 
  57.      *            实体类 
  58.      * @return 实体类List 
  59.      */  
  60.     public List<T> findAll(Class<T> entityClass);  
  61.   
  62.     /** 
  63.      * 根据实体参数,查询符合条件的实体类集合 
  64.      *  
  65.      * @param hql 
  66.      * @param values 
  67.      *            参数 
  68.      * @return 
  69.      */  
  70.     public List<Object> find(String hql, Object... values);  
  71.   
  72. }  


2、给出基于HibernateDaoSupport类的具体实现。 
Java代码   收藏代码
  1. package com.ys.common.dao;  
  2.   
  3. import java.io.Serializable;  
  4. import java.sql.SQLException;  
  5. import java.util.List;  
  6.   
  7. import org.hibernate.HibernateException;  
  8. import org.hibernate.Query;  
  9. import org.hibernate.Session;  
  10. import org.springframework.orm.hibernate3.HibernateCallback;  
  11. import org.springframework.orm.hibernate3.support.HibernateDaoSupport;  
  12.   
  13. public class BaseHibernateDao<T, ID extends Serializable> extends  
  14.         HibernateDaoSupport implements IBaseDao<T, ID> {  
  15.   
  16.     public void delete(T entity) {  
  17.         this.getHibernateTemplate().delete(entity);  
  18.     }  
  19.   
  20.     public void deleteById(Class<T> entityClass, ID id) {  
  21.         delete(this.findById(entityClass, id));  
  22.     }  
  23.   
  24.     @SuppressWarnings("unchecked")  
  25.     public T findById(Class<T> entityClass, ID id) {  
  26.         return (T) this.getHibernateTemplate().get(entityClass, id);  
  27.     }  
  28.   
  29.     @SuppressWarnings("unchecked")  
  30.     public List<T> findAll(Class<T> entityClass) {  
  31.         String name = entityClass.getName();  
  32.         return this.getHibernateTemplate().find("from " + name);  
  33.     }  
  34.   
  35.     public void save(T entity) {  
  36.         this.getHibernateTemplate().save(entity);  
  37.     }  
  38.   
  39.     public void update(T entity) {  
  40.         this.getHibernateTemplate().update(entity);  
  41.     }  
  42.   
  43.     @SuppressWarnings("unchecked")  
  44.     public List<Object> find(String hql, Object... values) {  
  45.         return this.getHibernateTemplate().find(hql, values);  
  46.     }  
  47.   
  48. }  


3、在web.xml里注册Spring的OpenSessionInViewFilter。 
Xml代码   收藏代码
  1. <filter>  
  2.     <filter-name>hibernateFilter</filter-name>  
  3.     <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>  
  4. </filter>  
  5. <filter-mapping>  
  6.     <filter-name>hibernateFilter</filter-name>  
  7.     <url-pattern>*.action</url-pattern>  
  8. </filter-mapping>  


4、继承基础DAO类即可重用DAO类方法。 
Java代码   收藏代码
  1. package com.ys.system.dept.dao;  
  2.   
  3. import com.ys.common.dao.BaseHibernateDao;  
  4. import com.ys.system.dept.bean.SysDept;  
  5.   
  6. public class DeptDao extends BaseHibernateDao<SysDept, Integer> {  
  7.       
  8. }  


具体的调用方式如下: 
Java代码   收藏代码
  1. package com.ys.system.dept.service;  
  2.   
  3. import java.util.List;  
  4.   
  5. import com.ys.system.dept.bean.SysDept;  
  6. import com.ys.system.dept.dao.DeptDao;  
  7.   
  8. public class DeptService implements IDeptService {  
  9.   
  10.          //Spring容器依赖注入  
  11.     private DeptDao deptDao;  
  12.   
  13.     public DeptDao getDeptDao() {  
  14.         return deptDao;  
  15.     }  
  16.   
  17.     public void setDeptDao(DeptDao deptDao) {  
  18.         this.deptDao = deptDao;  
  19.     }  
  20.   
  21.     public void delete(SysDept dept) {  
  22.         deptDao.delete(dept);  
  23.     }  
  24.   
  25.     public SysDept findById(int deptId) {  
  26.         return deptDao.findById(SysDept.class, deptId);  
  27.     }  
  28.   
  29.     public void save(SysDept dept) {  
  30.         deptDao.save(dept);  
  31.     }  
  32.   
  33.     public void update(SysDept dept) {  
  34.         deptDao.update(dept);  
  35.     }  
  36.   
  37. }  


提供出DeptDao的Spring配置文件片段: 
        
Xml代码   收藏代码
  1. <bean id="deptDao" class="com.ys.system.dept.dao.DeptDao" scope="singleton">  
  2.     <property name="sessionFactory">  
  3.         <ref bean="sessionFactory" />  
  4.     </property>  
  5. </bean>  
  6.   
  7. <bean id="deptService" class="com.ys.system.dept.service.DeptService">  
  8.     <property name="deptDao" ref="deptDao"></property>  
  9. </bean>  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值