spring事务(一)之编程式事务

时间:2019-02-14
本文章向大家介绍spring事务(一)之编程式事务,主要包括spring事务(一)之编程式事务使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

编程式事务管理: 通过Transaction Template手动管理事务,实际应用中很少使用
项目目录结构(采用springboot+maven构建项目):

为测试方便,省略了service层和dao层接口

  1. 导包
<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
		</dependency>
		<!--mybatis与springboot整合 -->
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>1.3.0</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>5.1.5.RELEASE</version>
		</dependency>
		<!-- 数据库连接池 -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid</artifactId>
			<version>1.1.12</version>
		</dependency>
  1. 创建数据库配置及事务配置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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
    <!-- 配置c3po连接池 -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <!-- 注入属性值 -->
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/springtransaction"></property>
        <property name="username" value="root"></property>
        <property name="password" value="123456"></property>
    </bean>
    <!-- 编程式事务管理 -->
    <!-- 配置事务管理器 -->
    <bean id="dataSourceTransactionManager"
          class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!-- 注入dataSource -->
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!-- 配置事务管理器模板 -->
    <bean id="transactionTemplate"
          class="org.springframework.transaction.support.TransactionTemplate">
        <!-- 注入真正进行事务管理的事务管理器,name必须为 transactionManager否则无法注入 -->
        <property name="transactionManager" ref="dataSourceTransactionManager"></property>
    </bean>

    <!-- JDBC模板对象 -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
</beans>
  1. sql文件
DROP TABLE IF EXISTS 'account';
create table `account` (
  `username` varchar (99),
  `salary` int (11)
);
insert into `account` (`username`, `salary`) values('小王','3000');
insert into `account` (`username`, `salary`) values('小马','3000');

# 解决时区问题
SHOW VARIABLES LIKE '%time_zone%';
SET GLOBAL TIME_ZONE = '+8:00';
  1. dao层
package com.xicheng.springtransaction.mapper;

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

/**
 * @author xichengxml
 * @date 2019/2/14 16:47
 * 编程式事务管理
 */
@Repository
public class TransferDaoImpl {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * 转钱方法
     */
    public void transferMoney() {
        String sql = "update account set salary=salary-? where username=?";
        jdbcTemplate.update(sql, 1000, "Bryan");
    }

    /**
     * 收钱方法
     */
    public void receiveMoney() {
        String sql = "update account set salary=salary+? where username=?";
        jdbcTemplate.update(sql, 1000, "Cathy");
    }
}

  1. service层
package com.xicheng.springtransaction.service;

import com.xicheng.springtransaction.mapper.TransferDaoImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;

/**
 * @author xichengxml
 * @date 2019/2/14 16:58
 */
@Service
public class TransferServiceImpl {

    @Autowired
    private TransferDaoImpl transferDao;

    @Autowired
    private TransactionTemplate transactionTemplate;

    public void transfer() throws Exception {
        transactionTemplate.execute(new TransactionCallback<Object>() {

            @Nullable
            @Override
            public Object doInTransaction(TransactionStatus transactionStatus) {
                Object result = null;
                try {
                    transferDao.transferMoney();
                    // 转账出现异常,回滚事务
                    int i = 10 / 0;
                    transferDao.receiveMoney();
                    result = 1;
                } catch (Exception e) {
                    transactionStatus.setRollbackOnly();
                    System.out.println("Transfer error!");
                    throw e;
                }
                return result;
            }
        });
    }
}
  1. controller层
package com.xicheng.springtransaction.controller;

import com.xicheng.springtransaction.service.TransferServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author xichengxml
 * @date 2019/2/14 17:11
 */
@RestController
public class TransferController {

    @Autowired
    private TransferServiceImpl transferService;

    @RequestMapping("transfer")
    public String transfer() {
        try {
            transferService.transfer();
            return "success";
        } catch (Exception e) {
            return "fail";
        }
    }
}
  1. 启动类(需要单独引入xml文件)
package com.xicheng.springtransaction;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;

@SpringBootApplication
@ImportResource("spring-mybatis.xml")
public class SpringtransactionApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringtransactionApplication.class, args);
	}

}
  1. 启动项目,访问:http://localhost:8080/transfer