springboot开启事务很简单,只须要一个注解@Transactional 就能够了。由于在springboot中已经默认对jpa、jdbc、mybatis开启了事事务,引入它们依赖的时候,事物就默认开启。固然,若是你须要用其余的orm,好比beatlsql,就须要本身配置相关的事物管理器。java
以上一篇文章的代码为例子,即springboot整合mybatis,上一篇文章是基于注解来实现mybatis的数据访问层,这篇文章基于xml的来实现,并开启声明式事务。mysql
在pom文件中引入mybatis启动依赖:git
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.0</version> </dependency>
引入mysql 依赖:github
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.29</version> </dependency>
-- create table `account` # DROP TABLE `account` IF EXISTS CREATE TABLE `account` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(20) NOT NULL, `money` double DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8; INSERT INTO `account` VALUES ('1', 'aaa', '1000'); INSERT INTO `account` VALUES ('2', 'bbb', '1000'); INSERT INTO `account` VALUES ('3', 'ccc', '1000');
spring.datasource.url=jdbc:mysql://localhost:3306/test spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.jdbc.Driver mybatis.mapper-locations=classpath*:mybatis/*Mapper.xml mybatis.type-aliases-package=com.forezp.entity
经过配置mybatis.mapper-locations来指明mapper的xml文件存放位置,我是放在resources/mybatis文件下的。mybatis.type-aliases-package来指明和数据库映射的实体的所在包。spring
通过以上步骤,springboot就能够经过mybatis访问数据库来。sql
public class Account { private int id ; private String name ; private double money; getter.. setter.. }
接口:数据库
public interface AccountMapper2 { int update( @Param("money") double money, @Param("id") int id); }
mapper:springboot
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.forezp.dao.AccountMapper2"> <update id="update"> UPDATE account set money=#{money} WHERE id=#{id} </update> </mapper>
@Service public class AccountService2 { @Autowired AccountMapper2 accountMapper2; @Transactional public void transfer() throws RuntimeException{ accountMapper2.update(90,1);//用户1减10块 用户2加10块 int i=1/0; accountMapper2.update(110,2); } }
@Transactional,声明事务,并设计一个转帐方法,用户1减10块,用户2加10块。在用户1减10 ,以后,抛出异常,即用户2加10块钱不能执行,当加注解@Transactional以后,两我的的钱都没有增减。当不加@Transactional,用户1减了10,用户2没有增长,即没有操做用户2 的数据。可见@Transactional注解开启了事物。
结语mybatis
springboot 开启事物很简单,只须要加一行注解就能够了,前提你用的是jdbctemplate, jpa, mybatis,这种常见的orm。app
源码下载:https://github.com/forezp/Spr...