import java.util.List;
import org.hibernate.Query;
import
org.springframework.orm.hibernate3.support.HibernateDaoSupport;
/*
* 最基本的DAO操作,已供應(yīng)其他類來調(diào)用
*/
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;
}
}
這個(gè)類是最原始的去DAO,封裝了一些數(shù)據(jù)的增刪查改,然后我編寫了數(shù)據(jù)訪問接口,數(shù)據(jù)訪問的實(shí)現(xiàn)類繼承自這個(gè)類再實(shí)現(xiàn)接口。代碼如下:
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);
}
}
當(dāng)我AOP的切入點(diǎn)配置到BaseDAO是事務(wù)是不會(huì)提交的,這個(gè)事務(wù)的切入點(diǎn)必須配置到直接訪問數(shù)據(jù)庫(kù)類的上一層。配置文件如下:
<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>