结构

domin

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 org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;import java.util.List;/*** 账户的业务成层实现类* 事务控制应该都是在业务层*/
@Service("accountService")
@Transactional
//@Transactional(propagation = Propagation.SUPPORTS,readOnly = true)  //只读型事务控制
public class AccountServiceImpl implements IAccountService {@Autowiredprivate IAccountDao accountDao;@Overridepublic Account findAccountById(Integer accountId) {return accountDao.findAccountById(accountId);}//@Transactional(propagation = Propagation.REQUIRED,readOnly = false)  //此处需要读写型事务控制@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.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.stereotype.Repository;import java.util.List;/*** 账户的持久层实现类*/
@Repository("accountDao")
public class AccountDaoImpl  implements IAccountDao {@Autowiredprivate JdbcTemplate jdbcTemplate;@Overridepublic Account findAccountById(Integer accountId) {List<Account> accountList= jdbcTemplate.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= jdbcTemplate.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) {jdbcTemplate.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"xmlns:context="http://www.springframework.org/schema/context"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.xsdhttp://www.springframework.org/schema/contexthttps://www.springframework.org/schema/context/spring-context.xsd"><!--创建容器时要扫描的包--><context:component-scan base-package="com.itheima"></context:component-scan><!--配置JdbcTemplate--><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><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中基于注解的声明式事务控制配置步骤1.配置事务管理器2.开启spring对注解事务的支持3.在需要事务支持的地方使用@Transactional注解--><!--配置事务管理器--><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"></property></bean><!--开始spring对注解事务的支持--><tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
</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基于注解的声明式事务控制相关推荐

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

最新文章

  1. Android记事本开发02
  2. php实现按时间排序_按时间排序的问题?
  3. 兼容Tomcat和Weblogic的Spring 数据源JNDI配置
  4. 洛谷 - P1217 - 回文质数 - 枚举
  5. 14、java中的集合(1)
  6. GroupID和ArtifactID
  7. docker mysql总是退出_Docker提升测试效率之路
  8. Objective-C 2.0 with Cocoa Foundation--- 8,类方法以及私有方法
  9. mysql 存储汉字_MySQL存储汉字
  10. Centos 5.2安装Cacti并集成Nagios安装文档
  11. centos 添加快捷
  12. 计蒜客 2016计蒜之道比赛 初赛第四场 记录
  13. dvwa最详细安装过程
  14. word一打字就有下划线_word打字自带下划线 为什么WORD打字时总带有下划线,如何解决?...
  15. android+cast+sdk,如何使用Android发现Chromecast设备?
  16. ios中获得UUID的方法,ios怎么获得uuid
  17. 拔丝芋头的Java学习日记--Day5
  18. sin40度不用计算机怎么求,sin40度怎么算 sin40度如何算
  19. Flink Window Function
  20. 新房装修|选空调挂机还是中央空调?

热门文章

  1. python 位运算与等号_Python 运算符
  2. linux 下根据cpp文件快速书写头文件
  3. leetcode 567. Permutation in String 字符串的排列 滑动窗口法
  4. 【Sql Server】DateBase-SQL调整
  5. linux配置java环境变量(详细)
  6. 云端一体全栈解决方案
  7. MindSpore网络模型类
  8. 视觉SLAM技术应用
  9. PHP算法题:如何实现冒泡排序
  10. [JS][C++]两题斐波那契数列:上台阶、triangle