Posted on 2009-02-06 23:41 
橡皮人 阅读(1961) 
评论(0)  编辑  收藏  
			 
			
		 
		
import java.util.List;
 
import org.hibernate.Query;
import
org.springframework.orm.hibernate3.support.HibernateDaoSupport;
 
/*
 * 最基本的DAO操作,已供应其他类来调用
 */
public class BaseDAO extends HibernateDaoSupport
{
    /*
     * 添加
     */
    public boolean addObject(Object obj) {
       boolean state = true;
       try {
           this.getHibernateTemplate().save(obj);
 
       } catch (Exception e) {
           e.printStackTrace();
           state = false;
       }
       return state;
    }
 
    /*
     * 删除
     */
    public boolean delObject(Object obj) {
       boolean state = true;
       try {
           this.getHibernateTemplate().delete(obj);
       } catch (Exception e) {
 
           state = false;
       }
       return state;
 
    }
}
 
这个类是最原始的去DAO,封装了一些数据的增删查改,然后我编写了数据访问接口,数据访问的实现类继承自这个类再实现接口。代码如下:
 
import com.custservice.base.BasdBase;
import com.custservice.basicdao.BaseDAO;
 
public class BasdService extends BaseDAO implements BasdBase {
 
    public boolean delete(Object obj) {
       return super.delObject(obj);
    }
    public boolean saveObj(Object obj) {
 
       return super.addObject(obj);
    }
}
当我AOP的切入点配置到BaseDAO是事务是不会提交的,这个事务的切入点必须配置到直接访问数据库类的上一层。配置文件如下:
<tx:advice id="txAdvice" transaction-manager="transactionManager">
       <tx:attributes>
           <tx:method name="save*"
propagation="REQUIRED" />
           <tx:method name="update*"
propagation="REQUIRED" />
           <tx:method name="del*" propagation="REQUIRED" />
           <tx:method name="*" read-only="true" />
       </tx:attributes>
    </tx:advice>
 
    <aop:config>
       <aop:pointcut id="allMethod"
           expression="execution(*
com.custservice.service.*.*(..))" />
       <aop:advisor pointcut-ref="allMethod"
advice-ref="txAdvice" />
    </aop:config>