一、什么是事务
- 把一组业务当成一个业务来做,要么都成功,要么都失败!
- 事务在项目的开发中,十分重要,涉及到数据的一致性问题。
- 确保完整性和一致性。
事务的ACID原则:
二、Spring中的事务管理
- 声明式事务:AOP
- 编程式事务:需要再代码中,进行事务的管理
约束:
| 12
 3
 4
 
 | <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/cache/spring-tx.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
 
 | 
配置DataSource
| 12
 3
 4
 5
 6
 7
 
 | <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
 <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
 <property name="url" value="jdbc:mysql://localhost:3306/test?useSSL=false&useUnicode=true&characterEncoding=UTF-8"/>
 <property name="username" value="root"/>
 <property name="password" value="nc20011019"/>
 </bean>
 
 | 
通过aop配置事务:
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 
 | <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
 <constructor-arg ref="dataSource" />
 </bean>
 
 
 
 <tx:advice id="txAdvice" transaction-manager="transactionManager">
 
 <tx:attributes>
 <tx:method name="add"/>
 <tx:method name="delete"/>
 <tx:method name="update"/>
 <tx:method name="query"/>
 <tx:method name="*"/>
 </tx:attributes>
 </tx:advice>
 
 <aop:config>
 <aop:pointcut id="pointcut" expression="execution(* com.nichu.mapper.*.*(..))"/>
 <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/>
 </aop:config>
 
 | 
 
    
    
    
    
    
        
        
    
        -------------本文结束感谢您的阅读-------------