结构

domain

package com.itheima.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 +'}';}
}

service

接口层

package com.itheima.service;import com.itheima.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.itheima.service.impl;import com.itheima.dao.IAccountDao;
import com.itheima.domain.Account;
import com.itheima.service.IAccountService;import java.util.List;/*** 账户的业务成层实现类* 事务控制应该都是在业务层*/
public class AccountServiceImpl implements IAccountService {private IAccountDao accountDao;public void setAccountDao(IAccountDao accountDao) {this.accountDao = accountDao;}@Overridepublic Account findAccountById(Integer accountId) {return accountDao.findAccountById(accountId);}@Overridepublic void transfer(String sourceName, String targetName, Float money){//1.根据名称查询转出账户Account source=accountDao.findAccountByName(sourceName); //获取一个数据库连接//2.根据名称查询转入账户Account target=accountDao.findAccountByName(targetName); //获取一个数据库连接//3.转出账户减钱source.setMoney(source.getMoney()-money);//4.转入账户加钱target.setMoney(target.getMoney()+money);//5.更新转出账户accountDao.updateAccount(source); //获取一个数据库连接int i=1/0;//6.更新转入账户accountDao.updateAccount(target); //获取一个数据库连接}}

dao

接口层

package com.itheima.dao;import com.itheima.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.itheima.dao.impl;import com.itheima.dao.IAccountDao;
import com.itheima.domain.Account;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.support.JdbcDaoSupport;import java.util.List;/*** 账户的持久层实现类*/
public class AccountDaoImpl extends JdbcDaoSupport implements IAccountDao {@Overridepublic Account findAccountById(Integer accountId) {List<Account> accountList= super.getJdbcTemplate().query("select * from account where id=?",new BeanPropertyRowMapper<Account>(Account.class),accountId);return accountList.isEmpty()?null:accountList.get(0);}@Overridepublic Account findAccountByName(String accountName) {List<Account> accountList= super.getJdbcTemplate().query("select * from account where name=?",new BeanPropertyRowMapper<Account>(Account.class),accountName);if(accountList.isEmpty()){return null;}if(accountList.size()>1){throw new RuntimeException("结果集不唯一");}return accountList.get(0);}@Overridepublic void updateAccount(Account account) {super.getJdbcTemplate().update("update account set name=?,money=? where id=?",account.getName(),account.getMoney(),account.getId());}
}

bean.xml

<?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/beanshttps://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/txhttps://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/aophttps://www.springframework.org/schema/aop/spring-aop.xsd"><!--配置账户的业务层--><bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"><property name="accountDao" ref="accountDao"></property></bean><!--配置账户的持久层--><bean id="accountDao" class="com.itheima.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/eesy"></property><property name="username" value="root"></property><property name="password" value="root"></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:用于指定事务的传播行为。默认值是"MANDATORY",表示一定会有事务,增删改的选择。查询方法可以选择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.itheima.service.impl.*.*(..))"/><!--建立切入点表达式和事务通知的对应关系--><aop:advisor advice-ref="txAdvice" pointcut-ref="pt1"></aop:advisor></aop:config>
</beans>

测试类

package com.itheima.test;import com.itheima.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;/*** 使用Junited单元测试,测试我们的配置*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:bean.xml")
public class AccountServiceTest {@Autowiredprivate IAccountService as;@Testpublic void testTransfer(){as.transfer("aaa","bbb",100f);}
}

【spring】spring基于xml的声明式事务控制相关推荐

  1. spring基于XML的声明式事务控制-配置步骤

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

  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. Spring JDBC-使用XML配置声明式事务

    系列 概述 基于aop/tx命名空间的配置 示例 tx:method元素属性 系列 Spring对事务管理的支持概述以及 编程式的事务管理 Spring JDBC-使用XML配置声明式事务 Sprin ...

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

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

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

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

  9. Spring JDBC-使用注解配置声明式事务

    系列 概述 使用Transactional注解 txannotation-driven其他属性 关于Transaction的属性 在何处标注Transactional注解 在方法处使用注解 使用不同的 ...

最新文章

  1. 算法的时间与空间复杂度详解
  2. [C#] 等待启动的进程执行完毕
  3. 【MFC】显示系统时间的状态栏
  4. java js webservice_java Web技术探路:js Ajax调用WebService
  5. STL12-queue容器
  6. web开发模式+三层架构与MVC
  7. linux中nginx的nginx.config文件的配置和启动(包括重启)
  8. 查看单元测试用例覆盖率新姿势:IDEA 集成 JaCoCo
  9. 愚人节将至,怎么恶搞最过瘾
  10. 简单解析一下,实施MES管理系统后有哪些效益
  11. Convex Optimization 读书笔记 (2)
  12. 数据库系统概述之断言
  13. c语言1到20联程,闫超
  14. 深入 javascript 之 原型和原型链!!!
  15. 对于刚入行的Android程序员来说,找Android培训机构应该注意哪些方面?
  16. 计算机网络与英语教学,计算机网络技术在大学英语教学的运用
  17. 0基础快速入门Python,小白必看
  18. 飞奔的TCL,崛起的中国科技巨头
  19. 胡适说:他拍的是真正如画的北京
  20. 使用过滤器做一个过滤敏感词实例

热门文章

  1. 2022-2028年中国煤制天然气市场投资分析及前景预测报告
  2. Python+OpenCV 图像处理系列(8)—— Numpy 对象及图像对象创建与赋值
  3. 前端Vue学习之路(一)-初识Vue
  4. 【SpringMVC】基本概念
  5. Pytorch Bi-LSTM + CRF 代码详解
  6. tf.contrib.layers.xavier_initializer
  7. HiCar人-车-家全场景智慧互联
  8. MindInsight张量可视设计介绍
  9. nvGRAPH API参考分析(二)
  10. 编译器设计-解析类型