当前位置: 首页 > news >正文

Spring_day4

Spring 中的事务控制

基于 XML 的声明式事务控制(配置方式)

  1. 第一步:拷贝必要的 jar 包到工程的 lib 目录
 <dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.0.2.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>5.0.2.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-tx</artifactId><version>5.0.2.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>5.0.2.RELEASE</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.6</version></dependency><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.8.7</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version></dependency></dependencies>
  1. 第二步:创建 spring 的配置文件并导入约束
<?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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd">
</beans>
  1. 第三步:在配置文件中配置业务层和持久层对
<!-- 配置 service -->
<bean id="accountService" class="cn.myp666.service.impl.AccountServiceImpl"><property name="accountDao" ref="accountDao"></property>
</bean>
<!-- 配置 dao -->
<bean id="accountDao" class="cn.myp666.dao.impl.AccountDaoImpl">
<!-- 注入 dataSource --><property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置数据源 -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="driverClassName" value="com.mysql.jdbc.Driver"></property><property name="url" value="jdbc:mysql:///db1"></property><property name="username" value="root"></property><property name="password" value="1419"></property>
</bean>
事务配置步骤
  1. 第一步:配置事务管理器
<!-- 配置一个事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 注入 DataSource --><property name="dataSource" ref="dataSource"></property>
</bean>
  1. 第二步:配置事务的通知引用事务管理器
<!-- 事务的配置 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
</tx:advice>
  1. 第三步:配置事务的属性
<!--在 tx:advice 标签内部 配置事务的属性 -->
<tx:attributes>
<!-- 指定方法名称:是业务核心方法read-only:是否是只读事务。默认 false,不只读。isolation:指定事务的隔离级别。默认值是使用数据库的默认隔离级别。propagation:指定事务的传播行为。timeout:指定超时时间。默认值为:-1。永不超时。rollback-for:用于指定一个异常,当执行产生该异常时,事务回滚。产生其他异常,事务不回滚。
没有默认值,任何异常都回滚。no-rollback-for:用于指定一个异常,当产生该异常时,事务不回滚,产生其他异常时,事务回
滚。没有默认值,任何异常都回滚。
-->
<tx:method name="*" read-only="false" propagation="REQUIRED"/>
<tx:method name="find*" read-only="true" propagation="SUPPORTS"/>
</tx:attributes>
  1. 第四步:配置 AOP 切入点表达式
<!-- 配置 aop -->
<aop:config><!-- 配置切入点表达式 --><aop:pointcut expression="execution(* cn.myp666.service.impl.*.*(..))"
id="pt1"/>
</aop:config>
  1. 第五步:配置切入点表达式和事务通知的对应关系
<!-- 在 aop:config 标签内部:建立事务的通知和切入点表达式的关系 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="pt1"/>



基于注解的配置方式

  1. 第一步:拷贝必备的 jar 包到工程的 lib 目录
  2. 第二步:创建 spring 的配置文件导入约束并配置扫描的包
<?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"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><!-- 配置 spring 创建容器时要扫描的包 --><context:component-scan base-package="cn.myp666"></context:component-scan><!-- 配置 JdbcTemplate--><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource" ref="dataSource"></property></bean><!-- 配置 spring 提供的内置数据源 --><bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="driverClassName"
value="com.mysql.jdbc.Driver"></property><property name="url"
value="jdbc:mysql://localhost:3306/db1"></property><property name="username" value="root"></property><property name="password" value="1419"></property></bean>
</beans>
注解配置步骤
  1. 第一步:配置事务管理器并注入数据源
<!-- 配置事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"></property>
</bean>
  1. 第二步:在业务层使用@Transactional 注解
@Service("accountService")
@Transactional(readOnly=true,propagation=Propagation.SUPPORTS)
public class AccountServiceImpl implements IAccountService {@Autowiredprivate IAccountDao accountDao;@Overridepublic Account findAccountById(Integer id) {return accountDao.findAccountById(id);}@Override@Transactional(readOnly=false,propagation=Propagation.REQUIRED)public void transfer(String sourceName, String targeName, Float money) {//1.根据名称查询两个账户Account source = accountDao.findAccountByName(sourceName);Account target = accountDao.findAccountByName(targeName);//2.修改两个账户的金额source.setMoney(source.getMoney()-money);//转出账户减钱target.setMoney(target.getMoney()+money);//转入账户加钱//3.更新两个账户accountDao.updateAccount(source);//int i=1/0;accountDao.updateAccount(target);}
}
  • 该注解的属性和 xml 中的属性含义一致。该注解可以出现在接口上,类上和方法上。
    出现接口上,表示该接口的所有实现类都有事务支持。
    出现在类上,表示类中所有方法有事务支持
    出现在方法上,表示方法有事务支持。
    以上三个位置的优先级:方法>类>接口

  1. 第三步:在配置文件中开启 spring 对注解事务的支持
<!-- 开启 spring 对注解事务的支持 -->
<tx:annotation-driven transaction-manager="transactionManager"/>

http://www.taodudu.cc/news/show-1476400.html

相关文章:

  • 深度优先搜索汇总
  • 练习题(2024/5/15)
  • 【Python系列】字节串与字典字节串
  • 如何修改antd的pagination翻页组件为中文?
  • Signal 即将成为JavaScript的一部分
  • treejs 3D+echart实现三维旋转炫酷导航网页
  • SpringMVC_day1
  • SpringMVC_day1_常用注解
  • SpringMVC_02
  • Error creating bean with name 'dataSource' defined in class path resource [spring/spring-dao.xml]:
  • SpringSecurity入门
  • 服务注册不进eureka
  • Spring Cloud总结
  • 在此之前的博客地址
  • golang利用反射写入excel的简单工具类
  • 实习工作难点记录
  • c 结构体之位域(位段)
  • 辗转相除求最大公约数,最大公倍数
  • Ubuntu“无法解析或打开软件包的列表或是状态文件”的解决办法。
  • 错误:cc1: error: unrecognized command line option “-m32”
  • 在编写mini2440 helloworld驱动遇到的问题
  • [leetcode] Median of Two Sorted Arrays 寻找两个有序数组的中位数
  • [leetcode] Reverse Integer 反转一个整数
  • [leetcode] Palindrome Number 回文数判断
  • [leetcode] Longest Common Prefix 字符窜最长公共前缀判断
  • [leetcode] Single Number 查找数组中的单数
  • [leetcode] Power of Two 判断一个数是否是2的平方
  • [leetcode] Max Points on a Line 判断最多有多少个点在同一条直线上
  • 使用selenium webdriver进行元素定位
  • 一个动态增长的栈实现
  • sublime cscope使用方法
  • [leetcode] 24. Swap Nodes in Pairs
  • sublime text常用快捷键整理
  • kmp算法字符串匹配C语言实现
  • VLC设置串流的TTL值
  • [leetcode]328. Odd Even Linked List