spring-tx-anno,spring事务中xml与注解相结合的方式之转账示例

本文介绍了Spring事务管理中的四大特性:原子性、一致性、持久性和隔离性,并详细讲解了四种不同的隔离级别。通过一个具体的转账示例,展示了如何在项目中结合XML配置和注解来实现事务管理。示例包括了项目配置、Bean定义、DAO层、Service层以及测试类的相关代码。
摘要由CSDN通过智能技术生成

事物的447:
1.4大特性:
原子性,一致性,持久性,隔离性
2.事务的隔离级别:
ISOLATION_DEFAULT
ISOLATION_READ_UNCOMMITTED
ISOLATION_READ_COMMITTED
ISOLATION_REPEATABLE_READ
ISOLATION_SERIALIZABLE
下面是一个示例:
基于maven项目的pom.xml配置
pom.xml

<dependencies>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>5.2.1.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>5.2.1.RELEASE</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<version>5.2.1.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-tx</artifactId>
			<version>5.2.1.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>5.2.1.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.45</version>
		</dependency>
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid</artifactId>
			<version>1.1.21</version>
		</dependency>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjweaver</artifactId>
			<version>1.9.4</version>
		</dependency>
	</dependencies>

bean
Account.java

package com.bean;

import java.io.Serializable;

public class Account implements Serializable {
	private static final long serialVersionUID = 1L;
	private Integer id;
	private int balance;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public int getBalance() {
		return balance;
	}

	public void setBalance(int balance) {
		this.balance = balance;
	}

	@Override
	public String toString() {
		return "Account [id=" + id + ", balance=" + balance + "]";
	}
}

dao
AccountDao.java

package com.dao;

public interface AccountDao {
	/**
	 * 转出
	 * @param outId 转出账号
	 * @param money 转出金额
	 */
	public void transOut(Integer outId, int money);
	
	/**
	 * 转入
	 * @param inId 转入账号
	 * @param money 转入金额
	 */
	public void transIn(Integer inId, int money);
}

AccountDaoImpl.java

package com.dao.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.dao.AccountDao;

@Repository
public class AccountDaoImpl implements AccountDao {
	@Autowired
	private JdbcTemplate template;

	@Override
	public void transOut(Integer outId, int money) {
		template.update("UPDATE tb_account SET balance=balance-? WHERE id=?", money, outId);
	}

	@Override
	public void transIn(Integer inId, int money) {
		template.update("UPDATE tb_account SET balance=balance+? WHERE id=?", money, inId);
	}
}

AccountService.java

package com.service;

public interface AccountService {
	/**
	 * 转账
	 * @param outId 转出账号
	 * @param inId  转入账号
	 * @param money 金额
	 */
	public void trans(Integer outId, Integer inId, int money);
}

AccountServiceImpl.java

package com.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dao.AccountDao;
import com.service.AccountService;

/**
 * @Transactional是事务注解,它可以标注在类上,也可标注在方法上。
 * 如果是标注在类上表示该类中所有方法都需要事务,
 * 如果是标注在方法上,则表示该方法可能需要事务。
 */

@Service
@Transactional
public class AccountServiceImpl implements AccountService {
	@Autowired
	private AccountDao accountDao;

	/**
	 * 转账
	 * @param outId 转出账号
	 * @param inId  转入账号
	 * @param money 金额
	 */
	public void trans(Integer outId, Integer inId, int money) {
		accountDao.transOut(outId, money);
		//int a = 1/0;
		//System.out.println(a);
		accountDao.transIn(inId, money);
	}
/*	
	@Transactional(propagation=Propagation.SUPPORTS, readOnly=true)//表示不用事务管理器
	public Object get() {
		
	}
*/
}

beans.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:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.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">
	
	<!-- 配置连接属性 -->
	<context:property-placeholder location="classpath:db.properties"/>

	<!-- 配置IoC解析器 -->
	<context:component-scan base-package="com" />
	<!-- DI解析器 -->
	<context:annotation-config />
	
	<!-- 配置DataSource -->
	<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
			init-method="init" destroy-method="close">
		<property name="driverClassName" value="${jdbc.driverClassName}"/>
		<property name="url" value="${jdbc.url}"/>
		<property name="username" value="${jdbc.username}"/>
		<property name="password" value="${jdbc.password}"/>
	</bean>

	<!-- JdbcTemplate -->
	<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" />
	</bean>
	
	<!-- 配置事务解析器 -->
	<tx:annotation-driven transaction-manager="transactionManager"/>
	
</beans>

db.properties

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.username=root
jdbc.password=root

AccountServiceTest.java

package com.test;

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.SpringRunner;

import com.service.AccountService;

@RunWith(SpringRunner.class)
@ContextConfiguration("classpath:beans.xml")
public class AccountServiceTest {
	@Autowired
	private AccountService service;
	
	@Test
	public void testTrans() {
		service.trans(1000, 1001, 1000);
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值