Posted on 2009-02-06 23:41
橡皮人 閱讀(1946)
評論(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>