Spring框架 -- 7 Spring中的事务管理

    技术2026-01-26  12

    Spring的事务管理

    Spring中事务相关的概念Spring中的事务管理事务管理的API接口 Spring对事务的管理基于注解形式的事务管理器

    Spring中事务相关的概念

    什么是事务: 事务的特征:ACID -》原子性、一致性、隔离性、持久性 隔离不当引起的脏数据问题:脏读、不可重复读、幻读 隔离级别不同:

    Spring中的事务管理

    事务管理的API接口

    PlatformTransactionManager 事务管理器 TransactionDefinition:事务定义信息(隔离,传播、超时、只读) TransactionStatus:事务具体运行状态 参考资料: https://baijiahao.baidu.com/s?id=1624322378090716314&wfr=spider&for=pc

    Spring对事务的管理

    两种形式:

    编程式的事务管理(很少使用) 使用声明式的事务管理 -基于xml配置形式实现 -基于注解实现 7.3.1 基于xml形式的事务管理 配置文件的思想:AOP的思想实现

    <?xml version="1.0" encoding="UTF-8" ?> <!--根标签--> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="dataSource" class="com.mchange.v2.c3p0.DriverManagerDataSource"> <!--配置连接数据库的核心配置4个参数--> <property name="driverClass" value="com.mysql.jdbc.Driver"/> <property name="user" value="root"/> <property name="password" value="123456"/> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/mybatis"/> </bean> <!--配置SQLSessionFactory--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!--配置数据源--> <property name="dataSource" ref="dataSource"/> <!--加载mybatis的配置--> <property name="configLocation" value="mybatis/mybatis.xml"/> <!--配置xml文件的映射,在mybatis当中,在<mapper>标签下添加的--> <property name="mapperLocations"> <list> <value>mapper/User2Mapper.xml</value> </list> </property> </bean> <!-- 通过代理对象进行mapper的映射 class即mybatis-spring包提供的MapperFactoryBean --> <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> <!--mapperInterface指定mapper接口--> <property name="mapperInterface" value="com.tulun.dao.User2Mapper"/> <!--指定SQLSessionFactory--> <property name="sqlSessionFactory" ref="sqlSessionFactory"/> </bean> <!--第一步:配置事务管理器--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!--注入DataSource--> <property name="dataSource" ref="dataSource"/> </bean> <!--第二步:配置事务增强--> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <!--做事务处理--> <tx:attributes> <!-- 设定进行事务操作方法的匹配工作 add*指定增强的方法名 表示add开头的都可以 propagation:事务传播行为 isolation:指定隔离级别 --> <tx:method name="add*" propagation="REQUIRED" isolation="DEFAULT"/> </tx:attributes> </tx:advice> <!--第三步:配置切面--> <aop:config> <!--切入点--> <aop:pointcut id="pointcut1" expression="execution(* com.tulun.dao.UserDao.*(..))"/> <!--切面--> <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut1"/> </aop:config> </beans>

    基于注解形式的事务管理器

    配置信息

    <!--第一步:配置事务管理器--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!--注入DataSource--> <property name="dataSource" ref="dataSource"/> </bean> <!--第二步:配置事务注解:开启事务注解--> <tx:annotation-driven transaction-manager="transactionManager"/>

    注解:@Transactional

    @Transactional public class UserDao { private JdbcTemplate jdbcTemplate; public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } @Transactional public User2 getUserById(Long id){ //查询SQL String sql = "select * from user where id = ?"; User2 user2 = jdbcTemplate.queryForObject(sql,new Object[]{id},new UserMapper()); //使用jdbcTemplate模板获取数据库数据 return user2; } }

    @Transactional表示是事务,该注解可以添加在类上或者是方法上,添加在类上即该类的所有方法都使用事务,添加在方法上即当前的方法是支持事务的。

    Processed: 0.024, SQL: 9