SSM——事務配置


1. 在applicationContext.xml中配置事務管理器
<!-- 5. 配置事務管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>

2. 在applicationContext.xml中開啟事務注解
<!-- 5.1 開啟注解事務 -->
<tx:annotation-driven transaction-manager="transactionManager"/>

3. 在applicationContext.xml中掃描所有需配置事務的包

<!-- 5.2 掃描所有的@service組件,多個包之間","分隔-->
<context:component-scan base-package="com.neuedu.service"></context:component-scan>

注意:此處掃描所有的Service,就不用在后面的Spring-mvcConfig.xml中掃描(在SSM整合框架中,Spring配置文件為大容器,Spring-mvc為小容器,大容器中掃描出來的bean可以被小容器訪問,但小容器掃描出來的不能被大容器訪問【類似於繼承關系】)

4. 在需使用事務的接口聲明或方法處,加@Transactional修飾
public interface IEmpService {
//事務管理
@Transactional(rollbackForClassName="java.lang.Exception")
public boolean updateEmps(Integer[] empnos,double comm);

}

5. 測試


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM