spring 中事务管理

事务中三个核心接口:

平台事务管理器,用于管理事务

提供了三个操作方法:

他的几个常用实现类:

             

一、基于XML方式的声明式

1.导入Jar包,在基础jar包的基础之上导入aop包,和jdbc事务包,还有数据库驱动包

                                         

2.配置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:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
         http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
        
        <!-- 开启注解扫描 -->
        <context:component-scan base-package="com.wx"/>
        
        <!-- org\springframework\jdbc\datasource\DriverManagerDataSource.class -->
        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        	<!-- oracle配置 -->
        	<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
        	<property name="url" value="jdbc:oracle:thin:@localhost:1521:idts"/>
        	<!-- mysql配置 -->
        	<!-- <property name="driverClassName" value="jdbc:mysql://127.0.0.1/student" />
        	<property name="url" value="com.mysql.jdbc.Driver"></property> -->
        	<property name="username" value="spring"/>
        	<property name="password" value="spring"/>
        </bean>
        <!-- org\springframework\jdbc\core\JdbcTemplate.class -->
        <bean id= "jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        	<property name="dataSource" ref="dataSource"/>
        </bean>
        <!-- 配置事务管理器 -->
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        	<property name="dataSource" ref="dataSource"></property>
        </bean>
        <!-- 编写通知,指定事务管理器 -->
        <tx:advice id="txAdvice" transaction-manager="transactionManager">
        	<tx:attributes>
        		<tx:method name="*" propagation="REQUIRED" isolation="DEFAULT" read-only="false"/>
        	</tx:attributes>
        </tx:advice>
        <!-- 配置切面 -->
        <aop:config>
        	<aop:pointcut expression="execution(* com.wx.dao.*.*(..))" id="myPointCut"/>
        	<aop:advisor advice-ref="txAdvice" pointcut-ref="myPointCut"/>
        </aop:config>
        
</beans>

3.书写代码

package com.wx.domain;

public class Account {
	private String id;
	private String username;
	private Double balance;
	
	
	public Account(){}
	public Account(String username,Double balance){
	
		this.username= username;
		this.balance = balance;
	}
	
	
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public Double getBalance() {
		return balance;
	}
	public void setBalance(Double balance) {
		this.balance = balance;
	}
	@Override
	public String toString() {
		return "Account [id=" + id + ", username=" + username + ", balance="
				+ balance + "]";
	}
	
}
package com.wx.dao;

import java.util.List;
import java.util.Map;

import com.wx.domain.Account;

public interface AccountDao {
	//转账操作
	public void transfer(String outUser,String inUser,Double money);
}
package com.wx.dao.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.wx.dao.AccountDao;
import com.wx.domain.Account;

@Repository("accountDao")
public class AccountDaoImpl implements AccountDao {

	@Resource(name="jdbcTemplate")
	private JdbcTemplate jdbcTemplate;
	
	

	@Override
	public void transfer(String outUser, String inUser, Double money) {
		// TODO Auto-generated method stub
		//从outUser中减少钱
		String outSql = "update account set balance = balance - ? where username = ?";
		this.jdbcTemplate.update(outSql, new Object[]{money,outUser});
		
		int a = 1/0;
		
		//从inUser中增加钱
		String inSql = "update account set balance = balance + ? where username = ?";
		this.jdbcTemplate.update(inSql, new Object[]{money,inUser});
	}

}
package com.wx.service;

import java.util.List;
import java.util.Map;

import com.wx.domain.Account;

public interface AccountService {
	//转账操作
	public void transfer(String outUser,String inUser,Double money);
}
package com.wx.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.wx.dao.AccountDao;
import com.wx.domain.Account;
import com.wx.service.AccountService;


@Service("accountService")
public class AccountServiceImpl implements AccountService {

	@Resource(name = "accountDao")
	private AccountDao accountDao;
	
	
	@Override
	public void transfer(String outUser, String inUser, Double money) {
		// TODO Auto-generated method stub
		this.accountDao.transfer(outUser, inUser, money);
	}

}

 

4.测试

package com.wx.test;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;

import com.wx.domain.Account;
import com.wx.service.AccountService;



public class MyTest {
	public static void main(String[] args){
		ApplicationContext application = new ClassPathXmlApplicationContext("applicationContext.xml");
		AccountService accountService = (AccountService)application.getBean("accountService");
		
		String outUser = "wx1";
		String inUser = "wx";
		Double money = 1000.0;
		accountService.transfer(outUser, inUser, money);
		System.out.println("success");
		
	}
}

5.结果:报错后,数据库中的数据没有改变。

                          

二、基于Annotation方式的声明式事务

使用注解配置,只需要在配置文件中加入:配置事务管理器和开启事务注解即可,然后再指定类中加入@Transactional(isolation=Isolation.DEFAULT,propagation=Propagation.REQUIRED),属性根据需要添加

1.需要导入的jar包如上

2.配置文件

<?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:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
         http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
        
        <!-- 开启注解扫描 -->
        <context:component-scan base-package="com.wx"/>
        
        <!-- org\springframework\jdbc\datasource\DriverManagerDataSource.class -->
        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        	<!-- oracle配置 -->
        	<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
        	<property name="url" value="jdbc:oracle:thin:@localhost:1521:idts"/>
        	<!-- mysql配置 -->
        	<!-- <property name="driverClassName" value="jdbc:mysql://127.0.0.1/student" />
        	<property name="url" value="com.mysql.jdbc.Driver"></property> -->
        	<property name="username" value="spring"/>
        	<property name="password" value="spring"/>
        </bean>
        <!-- org\springframework\jdbc\core\JdbcTemplate.class -->
        <bean id= "jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        	<property name="dataSource" ref="dataSource"/>
        </bean>
        <!-- 配置事务管理器 -->
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        	<property name="dataSource" ref="dataSource"></property>
        </bean>
        
        <tx:annotation-driven transaction-manager="transactionManager"/>
        
</beans>

3.书写代码

package com.wx.domain;

public class Account {
	private String id;
	private String username;
	private Double balance;
	
	
	public Account(){}
	public Account(String username,Double balance){
	
		this.username= username;
		this.balance = balance;
	}
	
	
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public Double getBalance() {
		return balance;
	}
	public void setBalance(Double balance) {
		this.balance = balance;
	}
	@Override
	public String toString() {
		return "Account [id=" + id + ", username=" + username + ", balance="
				+ balance + "]";
	}
	
}
package com.wx.dao;

import java.util.List;
import java.util.Map;

import com.wx.domain.Account;

public interface AccountDao {
	//转账操作
	public void transfer(String outUser,String inUser,Double money);
}

 

package com.wx.dao.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.wx.dao.AccountDao;
import com.wx.domain.Account;

@Repository("accountDao")
@Transactional(isolation=Isolation.DEFAULT,propagation=Propagation.REQUIRED)
public class AccountDaoImpl implements AccountDao {

	@Resource(name="jdbcTemplate")
	private JdbcTemplate jdbcTemplate;
	
	
	@Override
	public void transfer(String outUser, String inUser, Double money) {
		// TODO Auto-generated method stub
		//从outUser中减少钱
		String outSql = "update account set balance = balance - ? where username = ?";
		this.jdbcTemplate.update(outSql, new Object[]{money,outUser});
		
		int a = 1/0;
		
		//从inUser中增加钱
		String inSql = "update account set balance = balance + ? where username = ?";
		this.jdbcTemplate.update(inSql, new Object[]{money,inUser});
	}

}
package com.wx.service;

import java.util.List;
import java.util.Map;

import com.wx.domain.Account;

public interface AccountService {
	//转账操作
	public void transfer(String outUser,String inUser,Double money);
}
package com.wx.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.wx.dao.AccountDao;
import com.wx.domain.Account;
import com.wx.service.AccountService;


@Service("accountService")
public class AccountServiceImpl implements AccountService {

	@Resource(name = "accountDao")
	private AccountDao accountDao;
	
	
	@Override
	public void transfer(String outUser, String inUser, Double money) {
		// TODO Auto-generated method stub
		this.accountDao.transfer(outUser, inUser, money);
	}

}

4.测试:

package com.wx.test;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;

import com.wx.domain.Account;
import com.wx.service.AccountService;



public class MyTest {
	public static void main(String[] args){
		ApplicationContext application = new ClassPathXmlApplicationContext("applicationContext.xml");
		AccountService accountService = (AccountService)application.getBean("accountService");
		
		String outUser = "wx1";
		String inUser = "wx";
		Double money = 1000.0;
		accountService.transfer(outUser, inUser, money);
		System.out.println("success");
		
	}
}

5.结果:

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值