Spring事务实例:

    entity实体类:

public class Accounts {
private int accountid;
private String accountname;
private double balance; public int getAccountid() {
return accountid;
} public void setAccountid(int accountid) {
this.accountid = accountid;
} public String getAccountname() {
return accountname;
} public void setAccountname(String accountname) {
this.accountname = accountname;
} public double getBalance() {
return balance;
} public void setBalance(double balance) {
this.balance = balance;
}
}

    dao层接口:

    //加钱
void addMoney(double money); //减钱
void subMoney(double money);

    daoimpl实现类接口:

    @Override
@Transactional(propagation = Propagation.REQUIRED)
public void addMoney(double money) {
String sql="UPDATE accounts SET balance=balance+? WHERE accountname='张三'";
this.getJdbcTemplate().update(sql,money);
} @Override
@Transactional(propagation = Propagation.REQUIRED)
public void subMoney(double money) {
String sql="UPDATE accounts SET balance=balance-? WHERE accountname='小红'";
this.getJdbcTemplate().update(sql,money);
}

    service业务层:

    //转账
void transferMoney();

    service业务实现层:

    //转账的方法
/*@Transactional(isolation = Isolation.READ_COMMITTED,propagation = Propagation.REQUIRED)*/
@Transactional
@Override
public void transferMoney() {
accountDao.subMoney(); //价钱
int num=/; //模拟一个异常 使用事务解决
accountDao.addMoney();
}

    applicationContext.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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.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"> <!--添加jdbc-->
<bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties"></property>
</bean> <!--配置数据源-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="url" value="${url}"></property>
<property name="driverClassName" value="${driver}"></property>
<property name="username" value="${user}"></property>
<property name="password" value="${password}"></property>
</bean> <!--配置jdbcTemplate核心对象-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean> <!--Dao接口的实现类对象-->
<bean id="accountDao" class="com.te.daoimpl.AccountDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean> <!--service接口的实现类对象-->
<bean id="accountService" class="com.te.serviceImpl.AccountServiceImpl">
<property name="accountDao" ref="accountDao"></property>
</bean> <!--配置事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean> <!--配置事务-->
   <!--方式一:工厂配置-->
<!--配置Spring事务增强的代理工厂Bean-->
<bean id="transactionProxyFactoryBean" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<--事务管理器-->
<property name="transactionManager" ref="transactionManager"></property>
<--加载主业务对象-->
<property name="target" ref="accountService"></property>
<--设置事务的隔离级别和传播行为-->
<property name="transactionAttributes">
<props>
<--键值key为具体的方法名value,可以为传播行为或隔离级别-->
<prop key="transferMoney">ISOLATION_READ_UNCOMMITTED,PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
  <!--方式二:AOP配置-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="transferMoney" propagation="REQUIRED" isolation="READ_COMMITTED" />
</tx:attributes>
</tx:advice>
<!--定义切面-->
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.te.service.*.*(..))"></aop:advisor>
</aop:config>   <!--方式三:注解式--> 
  <tx:annotation-driven/>
  <context:component-scan base-package="com.te"></context:component-scan>
</beans>

    测试:

    @Test
public void test03(){
ApplicationContext ctx=new ClassPathXmlApplicationContext("application-del.xml");
AccountService accountService =(AccountService)ctx.getBean("accountService");
//转账
accountService.transferMoney();
}

最新文章

  1. iOS系列教程 目录 (持续更新...)
  2. ACM/ICPC 之 数论-素数筛选法 与 &quot;打表&quot;思路(POJ 1595)
  3. [Linux] yum和apt-get用法及区别
  4. springmvc学习笔记--REST API的异常处理
  5. 商品标签例子——CSS3 transform 属性
  6. Largest product in a series
  7. BLOG PLUGINS
  8. 跨服务器查询sql (摘要)
  9. chrome开发工具指南(七)
  10. 02_HTML5+CSS详解第三天
  11. SLAM+语音机器人DIY系列:(二)ROS入门——7.理解tf的原理
  12. [转]C#通过委托更新UI(异步加载)
  13. 20175126《Java程序设计》第七周学习总结
  14. Linux 进程后台运行的几种方式 screen
  15. C Looooops(poj2115+扩展欧几里德)
  16. C#-MVC开发微信应用(3)--文本消息和图文消息的应答
  17. android-------Android Studio使用MAT分析工具遇到的错误
  18. Linux C语言连接 sqlserver数据库
  19. Python20 - Day09
  20. CodeForces 558C Amr and Chemistry (位运算,数论,规律,枚举)

热门文章

  1. Scrapy进阶知识点总结(二)——选择器Selectors
  2. 本周授课内容:http,https,Tomcat,servlet
  3. Java自学基础、进阶、项目实战网站推荐
  4. T-SQL Part IX, PIVOT and UNPIVOT
  5. suseoj 1206 众数问题 (相邻数比较)
  6. nyoj 96-n-1位数 (strlen, atoi, ceil)
  7. nyoj 169-素数 (打表)
  8. 剑指Offer-20.包含min函数的栈(C++/Java)
  9. vuejs 入门
  10. python3 之 面向对象(类)、继承、派生和多态