<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.learn</groupId><artifactId>day04_learn_05tx_xml</artifactId><version>1.0-SNAPSHOT</version><packaging>jar</packaging><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.45</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></project>
package com.learn.dao;import com.learn.domain.Account;/*** 账户的持久层接口*/
public interface IAccountDao {/*** 根据Id查询账户* @param accountId* @return*/Account findAccountById(Integer accountId);/*** 根据名称查询账户* @param accountName* @return*/Account findAccountByName(String accountName);/*** 更新账户* @param account*/void updateAccount(Account account);
}
package com.learn.dao.impl;import com.learn.dao.IAccountDao;
import com.learn.domain.Account;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;import java.util.List;/*** 账户的持久层实现类*/
public class AccountDaoImpl extends JdbcDaoSupport implements IAccountDao {public Account findAccountById(Integer accountId) {List<Account> accounts = super.getJdbcTemplate().query("select * from account where id = ?",new BeanPropertyRowMapper<Account>(Account.class),accountId);return accounts.isEmpty()?null:accounts.get(0);}public Account findAccountByName(String accountName) {List<Account> accounts = super.getJdbcTemplate().query("select * from account where name = ?",new BeanPropertyRowMapper<Account>(Account.class),accountName);if(accounts.isEmpty()){return null;}if(accounts.size()>1){throw new RuntimeException("结果集不唯一");}return accounts.get(0);}public void updateAccount(Account account) {super.getJdbcTemplate().update("update account set name=?,money=? where id=?",account.getName(),account.getMoney(),account.getId());}
}
package com.learn.domain;import java.io.Serializable;/*** 账户的实体类*/
public class Account implements Serializable {private Integer id;private String name;private Float money;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Float getMoney() {return money;}public void setMoney(Float money) {this.money = money;}@Overridepublic String toString() {return "Account{" +"id=" + id +", name='" + name + '\'' +", money=" + money +'}';}
}
package com.learn.service;import com.learn.domain.Account;/*** 账户的业务层接口*/
public interface IAccountService {/*** 根据id查询账户信息* @param accountId* @return*/Account findAccountById(Integer accountId);/*** 转账* @param sourceName    转成账户名称* @param targetName    转入账户名称* @param money         转账金额*/void transfer(String sourceName, String targetName, Float money);
}
package com.learn.service.impl;import com.learn.dao.IAccountDao;
import com.learn.domain.Account;
import com.learn.service.IAccountService;/*** 账户的业务层实现类** 事务控制应该都是在业务层*/
public class AccountServiceImpl implements IAccountService{private IAccountDao accountDao;public void setAccountDao(IAccountDao accountDao) {this.accountDao = accountDao;}public Account findAccountById(Integer accountId) {return accountDao.findAccountById(accountId);}public void transfer(String sourceName, String targetName, Float money) {System.out.println("transfer....");//2.1根据名称查询转出账户Account source = accountDao.findAccountByName(sourceName);//2.2根据名称查询转入账户Account target = accountDao.findAccountByName(targetName);//2.3转出账户减钱source.setMoney(source.getMoney()-money);//2.4转入账户加钱target.setMoney(target.getMoney()+money);//2.5更新转出账户accountDao.updateAccount(source);int i=1/0;//2.6更新转入账户accountDao.updateAccount(target);}
}
<?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"><!-- 配置业务层--><bean id="accountService" class="com.learn.service.impl.AccountServiceImpl"><property name="accountDao" ref="accountDao"></property></bean><!-- 配置账户的持久层--><bean id="accountDao" class="com.learn.dao.impl.AccountDaoImpl"><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://localhost:3306/test"></property><property name="username" value="root"></property><property name="password" value="123456"></property></bean><!-- spring中基于XML的声明式事务控制配置步骤1、配置事务管理器2、配置事务的通知此时我们需要导入事务的约束 tx名称空间和约束,同时也需要aop的使用tx:advice标签配置事务通知属性:id:给事务通知起一个唯一标识transaction-manager:给事务通知提供一个事务管理器引用3、配置AOP中的通用切入点表达式4、建立事务通知和切入点表达式的对应关系5、配置事务的属性是在事务的通知tx:advice标签的内部--><!-- 配置事务管理器 --><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"></property></bean><!-- 配置事务的通知--><tx:advice id="txAdvice" transaction-manager="transactionManager"><!-- 配置事务的属性isolation:用于指定事务的隔离级别。默认值是DEFAULT,表示使用数据库的默认隔离级别。propagation:用于指定事务的传播行为。默认值是REQUIRED,表示一定会有事务,增删改的选择。查询方法可以选择SUPPORTS。read-only:用于指定事务是否只读。只有查询方法才能设置为true。默认值是false,表示读写。timeout:用于指定事务的超时时间,默认值是-1,表示永不超时。如果指定了数值,以秒为单位。rollback-for:用于指定一个异常,当产生该异常时,事务回滚,产生其他异常时,事务不回滚。没有默认值。表示任何异常都回滚。no-rollback-for:用于指定一个异常,当产生该异常时,事务不回滚,产生其他异常时事务回滚。没有默认值。表示任何异常都回滚。--><tx:attributes><tx:method name="*" propagation="REQUIRED" read-only="false"/><tx:method name="find*" propagation="SUPPORTS" read-only="true"></tx:method></tx:attributes></tx:advice><!-- 配置aop--><aop:config><!-- 配置切入点表达式--><aop:pointcut id="pt1" expression="execution(* com.learn.service.impl.*.*(..))"></aop:pointcut><!--建立切入点表达式和事务通知的对应关系 --><aop:advisor advice-ref="txAdvice" pointcut-ref="pt1"></aop:advisor></aop:config>
</beans>
package com.learn.test;import com.learn.service.IAccountService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;/*** 使用Junit单元测试:测试我们的配置*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:bean.xml")
public class AccountServiceTest {@Autowiredprivate  IAccountService as;@Testpublic  void testTransfer(){as.transfer("aaa","bbb",100f);}}

spring基于XML的声明式事务控制-配置步骤相关推荐

  1. 【spring】spring基于xml的声明式事务控制

    结构 domain package com.itheima.domain;import java.io.Serializable;public class Account implements Ser ...

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

    一.引入依赖 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http ...

  3. 【Spring】spring基于注解的声明式事务控制

    结构 domin package com.itheima.domain;import java.io.Serializable;public class Account implements Seri ...

  4. spring基于注解的声明式事务控制

    <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://mave ...

  5. Spring的编程式事务声明式事务 基于注解的声明式事务控制

    文章目录 Spring中编程式事务 基于XML的声明式事务控制 基于注解的声明式事务控制 Spring集成web环境 Spring中编程式事务 Spring的事务控制可以分为编程式事务控制和声明式事务 ...

  6. 【java学习之路】(java框架)010.声明式事务控制

    声明式事务控制 编程式事务控制相关对象 PlatformTransactionManager* PlatformTransactionManager 接口是 spring 的事务管理器,它里面提供了我 ...

  7. JAVA日记之SpringJdbcTemplate/声明式事务控制 ----喝最烈的酒.

    JdbcTemplate基本使用 01-JdbcTemplate基本使用-概述 JdbcTemplate是spring框架中提供的一个对象,是对原始繁琐的Jdbc API对象的简单封装.spring框 ...

  8. [JAVAEE]实验06:基于XML和基于注解的声明式事务管理方式模拟银行转账程序

    一.实验目的: 熟练掌握声明式事务管理. 二.实验内容: 编写一个模拟银行转账的程序,要求在转账时通过Spring对事务进行控制. 三.实验要求: 分别使用基于XML和基于注解的声明式事务管理方式来实 ...

  9. 注解配置声明式事务控制解析

    ①使用 @Transactional 在需要进行事务控制的类或是方法上修饰,注解可用的属性同 xml 配置方式,例如隔离级别.传播行为等. ②注解使用在类上,那么该类下的所有方法都使用同一套注解参数配 ...

最新文章

  1. python3x 中如何使用tkMessageBox
  2. 颜色空间转换 cvtColor()[OpenCV 笔记13]
  3. 背包问题概述(Lintcode- 562.Backpack IV问题解决)
  4. 十一、explain属性介绍
  5. Oracle分析函数FIRST_VALUE、LAST_VALUE
  6. jsp页面中使用超链接标签a中的属性href和onclick同时触发怎么执行
  7. oracle数据库迁移 增大空间,Oracle数据库迁移、创建表空间、创建数据表实例讲解-Oracle...
  8. android 使用adb远程调试
  9. docker部署ubuntu并连接ssh远程调试代码
  10. Reverse Pairs
  11. 金三银四Java高级工程师面试题整理,2年以上经验必看
  12. jquery动画效果总结
  13. ubuntu20 系统 Apache+花生壳(内网穿透)服务项目上线
  14. 什么是数据挖掘技术,基本概念是什么?
  15. Responses 部分 | Http Header
  16. Opengl三视图的坐标变换
  17. 调研 FlinkSql功能测试及实战演练
  18. React Native - 使用图片选择器react-native-image-picker拍照、选照片
  19. Python PySpark 大数据时代
  20. 第一章 Redis基础

热门文章

  1. BZOJ 3997 [TJOI2015]组合数学(单调DP)
  2. Ch2 空间配置器(allocator) ---笔记
  3. C++文件流操作备忘录
  4. 应该在什么时候使用Hadoop
  5. 【Hibernate】Hibernate的jar包的用途
  6. 【防衰老教程】记录一次IDEA,开发JavaWeb项目时JS中文乱码排错
  7. js中this关键字的使用
  8. 50行代码搞定无限滑动幻灯片
  9. android自动化测试工具之monkey
  10. Linux常用命令--文件(夹)查找之find命令