使用AOP实现类型安全的泛型DAO

由于要求在项目中使用泛型的DAO,所以上网Google了一下,找到了IBM的一篇文章。文章讲得不错,但是有些地方不清楚,如果完全按照那篇文章可能还会遇到一些困难。所以写了这篇文章,解释如何在项目中加入泛型的DAO实现。

首先是总的类关系的UML图:

然后是在配置文件中的关系图:  

其中,IStaffDao是我们自己定义的接口,这个接口类似:

public   interface  IStaffDAO  extends  GenericDao < Staff, Integer >

public  List listAll(); 

public  Staff getByLogonAndId(String logon, Integer id); 

// more  

}
 

 

GenericDao<T , PK extends Serilizable> 是泛型的 Dao 接口:

/**
 * 2006-11-22
 * 范型DAO接口
 * 
@author  Zou Ang
 * Contact <a href ="mailto:[email protected]">Zou Ang</a>
 
*/

public   interface  GenericDao < T, PK  extends  Serializable >   {

    
/**
     * 保存一个对象到数据库
     * 
@param  newInstance 需要保存的对象
     * 
@return
     
*/

    PK create(T newInstance);
    
/**
     * 从数据库读取一个对象
     * 
@param  id 主键
     * 
@return
     
*/

    T read(PK id);
    
    
/**
     * 更新一个对象
     * 
@param  transientObject 被更新的对象
     
*/

    
void  update(T transientObject);
    
    
/**
     * 删除一个对象
     * 
@param  transientObject 被删除的对象
     
*/

    
void  delete(T transientObject);
}

 

GenericDaoHibernateImpl GenericDao 接口的泛型实现 :

 


/**
 * 2006-11-22
 * 范型DAO实现
 * 
@author  Zou Ang
 * Contact <a href ="mailto:[email protected]">Zou Ang</a>
 
*/

public   class  GenericDaoHibernateImpl < T,PK  extends  Serializable >  
    
extends  HibernateDaoSupport 
        
implements  GenericDao < T, PK >  ,FinderExecutor {
    
    
private  Class < T >  type;
    
private  FinderNamingStrategy namingStrategy  =   new  SimpleFinderNamingStrategy();  //  Default. Can override in config
     private  FinderArgumentTypeFactory argumentTypeFactory  =   new  SimpleFinderArgumentTypeFactory();  //  Default. Can override in config
    
    
public  GenericDaoHibernateImpl(Class < T >  type) {
        
this .type  =  type;
    }


    
/*  (non-Javadoc)
     * @see com.gdnfha.atcs.common.service.dao.GenericDao#create(java.lang.Object)
     
*/

    
public  PK create(T newInstance) 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值