spring基于注解的声明式事务控制配置

Posted lijiahaoAA

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring基于注解的声明式事务控制配置相关的知识,希望对你有一定的参考价值。

配置文件:

<?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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
https://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">

<!--配置spring创建容器时需要扫描的包-->
<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.cj.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/eesy?serverTimezone=Asia/Shanghai&amp;characterEncoding=utf8&amp;useSSL=false"></property>
<property name="username" value="root"></property>
<property name="password" value="123456"></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>

service实现类:
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;

/**
* @Author: lijiahao
* @Description:
* @Data: Create in 0:16 2020/2/6
* @Modified By:
*/
@Service("accountService")
@Transactional(propagation = Propagation.SUPPORTS,readOnly = true)//只读型事务的配置
public class AccountServiceImpl implements IAccountService {

@Autowired
private IAccountDao accountDao;

public Account findAccountById(Integer accountid) {
return accountDao.findAccountById(accountid);
}

//需要的是读写型事务配置
@Transactional(propagation = Propagation.REQUIRED,readOnly = false)
public void transfer(String sourceName, String targetName, Float money) {
System.out.println("trans......");
//2.1.根据名称查询转出帐户
Account source = accountDao.findAccountByName(sourceName);
//2.2.根据名称查询转入帐户
Account target = accountDao.findAccountByName(targetName);
//2.3.转出账户减钱
source.setMoney(source.getMoney()-money);
//2.4.转入帐户加钱
target.setMoney(target.getMoney()+money);
//2.5.更新转出账户
accountDao.updateAccount(source);

int i = 1/0;

//2.6.更新转入账户
accountDao.updateAccount(target);

}
}

dao层实现类:
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;

/**
* @Author: lijiahao
* @Description:
* @Data: Create in 2:32 2020/2/9
* @Modified By:
*/
@Repository("accountDao")
public class AccountDaoImpl implements IAccountDao {

@Autowired
private JdbcTemplate jdbcTemplate;

public Account findAccountById(Integer id) {
List<Account> accountList = jdbcTemplate.query("select * from account where id = ?", new BeanPropertyRowMapper<Account>(Account.class),id);
return accountList.isEmpty()?null:accountList.get(0);
}

public Account findAccountByName(String name) {
List<Account> accountList = jdbcTemplate.query("select * from account where name = ?", new BeanPropertyRowMapper<Account>(Account.class),name);
if(accountList.isEmpty()){
return null;
}
if(accountList.size()>1){
throw new RuntimeException("结果集不唯一");
}
return accountList.get(0);
}

public void updateAccount(Account account) {
jdbcTemplate.update("update account set name=? ,money=? where id = ?", account.getName(),account.getMoney(),account.getId());
}
}

实体类:
package com.itheima.domain;

import java.io.Serializable;

/**
* @Author: lijiahao
* @Description: 账户的实体类
* @Data: Create in 1:32 2020/2/9
* @Modified By:
*/
public class Account implements Serializable {

private Integer id;
private String name;
private Float money;

@Override
public String toString() {
return "account{" +
"id=" + id +
", name=‘" + name + ‘‘‘ +
", money=" + 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;
}
}

测试:
package com.itheima.test;

import com.itheima.domain.Account;
import com.itheima.service.IAccountService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.List;

/**
* @Author: lijiahao
* @Description: 使用junit测试配置
* @Data: Create in 0:59 2020/2/6
* @Modified By:
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:bean.xml")
public class AccountServiceTest {
@Autowired
private IAccountService as;

@Test
public void testTransfer(){
as.transfer("aaa", "bbb", 100f);
}

}

以上是关于spring基于注解的声明式事务控制配置的主要内容,如果未能解决你的问题,请参考以下文章

阶段3 2.Spring_10.Spring中事务控制_8 spring基于纯注解的声明式事务控制

Spring 事务控制 -- 基于XML的声明式事务控制:详细配置

Spring基于纯注解的声明式事务控制

阶段3 2.Spring_10.Spring中事务控制_7 spring基于注解的声明式事务控制

Spring 事务控制 -- 基于XML的声明式事务控制:环境搭建

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