spring Affairs (c) a statement of the transaction @Transactional

Copyright: https://blog.csdn.net/xichengqc/article/details/87347037

Based on the full annotation of @Transactional way: the declarative transaction management is simplified to the extreme. Developers only need to add one line after processing Bean enable the relevant configuration in the configuration file, and then you can implement transaction management methods need to implement transaction management classes or use @Transactional specify business rules, but do not function in other ways inferior.
Project directory structure:
Here Insert Picture Description

  1. sql file
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. Guide package
<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!-- 配置之后可以直接使用事务注解,不需要配置xml文件 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-jdbc</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>

		<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>
		<dependency>
			<groupId>com.google.code.gson</groupId>
			<artifactId>gson</artifactId>
			<version>2.8.5</version>
		</dependency>
		<dependency>
			<groupId>org.aspectj</groupId >
			<artifactId>aspectjweaver</artifactId >
			<version> 1.8.7</version >
		</dependency>
	</dependencies>
  1. Profiles
<?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">
    <!-- 配置连接池 -->
    <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="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
</beans>
  1. dao layer, service layer, Controller layer, class start (note add Scan Profile)
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");
    }
}
package com.xicheng.springtransaction.mapper;

import org.apache.ibatis.annotations.Mapper;

/**
 * @author xichengxml
 * @date 2019/2/15 10:29
 * 用于消除springboot中“No MyBatis mapper was found in '[com.xicheng.springtransaction]' package. Please check your configuration.”的警告
 */
@Mapper
public interface EliminateWarnMapper {
}
package com.xicheng.springtransaction.service;

/**
 * @author xichengxml
 * @date 2019/2/14 20:21
 */
public interface TransferService {

    void transfer() throws Exception;
}
package com.xicheng.springtransaction.service;

import com.xicheng.springtransaction.mapper.TransferDaoImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author xichengxml
 * @date 2019/2/14 16:58
 */

@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, rollbackFor = Exception.class)
@Service
public class TransferServiceImpl implements TransferService {

    @Autowired
    private TransferDaoImpl transferDao;

    @Override
    public void transfer() throws Exception {
        transferDao.transferMoney();
        // 转账出现异常,回滚事务
        int i = 10 / 0;
        transferDao.receiveMoney();
    }

}
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";
        }
    }
}
package com.xicheng.springtransaction;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
import org.springframework.transaction.annotation.EnableTransactionManagement;

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

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

}
  1. Start the project, visit: HTTP: // localhost: 8080 / Transfer

Guess you like

Origin blog.csdn.net/xichengqc/article/details/87347037