Springboot open transaction is simple, only need one annotation @transactional on it. Things are turned on by default because they have been defaulted on in Springboot for JPA, JDBC, MyBatis, and when they are introduced to dependencies. Of course, if you need to use other ORM, such as beatlsql, you need to configure the relevant things manager yourself.
Preparation phase
The code for the above article is an example, Springboot integration MyBatis, the previous article is based on annotations to implement the MyBatis data access layer, this article is based on XML to implement, and open declarative transactions.
Environmental dependency
Introduce the MyBatis boot dependency in the Pom file:
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId> Mybatis-spring-boot-starter</artifactid> <version>1.3.0</version></dependency>
Introducing MySQL Dependencies
<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>
Initializing Database Scripts
--Create TABLE ' account ' # DROP table ' account ' IF existscreate table ' account ' ( ' id ' int (one) ' Not NULL auto_incremen T, ' name ' varchar (NOT null), ' money ' double DEFAULT NULL, PRIMARY KEY (' id ')) engine=innodb auto_ increment=4 DEFAULT Charset=utf8;insert into ' account ' values (' 1 ', ' aaa ', ' n '); INSERT into ' account ' values (' 2 ', ' BBB ', ' + '); INSERT into ' account ' VALUES (' 3 ', ' CCC ', ' 1000 ');
Configure the data source
Spring.datasource.url=jdbc:mysql://localhost:3306/testspring.datasource.username= Rootspring.datasource.password=123456spring.datasource.driver-class-name= Com.mysql.jdbc.drivermybatis.mapper-locations=classpath*:mybatis/*mapper.xmlmybatis.type-aliases-package= Com.forezp.entity
By configuring Mybatis.mapper-locations to indicate the location of the mapper XML file, I placed it under the Resources/mybatis file. Mybatis.type-aliases-package to indicate the package that contains the entity to which the database is mapped.
After the above steps, Springboot can access the database through MyBatis.
Create an entity class
public class Account { private int id; private String name; private double money; Getter: Setter: }
Data Access DAO Layer
Interface:
Public interface AccountMapper2 { int update (@Param (' money ') double money, @Param ("id") int ID);}
Mapper
<?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 Layer
@Servicepublic class AccountService2 { @Autowired AccountMapper2 accountMapper2; @Transactional public void Transfer () throws runtimeexception{ accountmapper2.update (90,1);//user 1 minus 10 blocks User 2 plus 10 block int i=1/0; Accountmapper2.update (110,2); }}
@Transactional, declare the transaction, and design a transfer method, the user 1 minus 10 blocks, the user 2 plus 10 blocks. After the user 1 minus 10, after the exception, that is, the user 2 plus 10 dollars can not be executed, when added annotated @transactional, two people's money has not increased or decreased. When @transactional is not added, user 1 is reduced by 10, user 2 does not increase, that is, no user 2 data is manipulated. Visible @transactional Annotations Open things up.
Conclusion
Springboot open things very simple, only need to add a line of annotations on it, if you are using JdbcTemplate, JPA, MyBatis, this common ORM.
SOURCE Download: https://github.com/forezp/SpringBootLearning
Resources
managing-transactions/
Excellent article recommendation:
- More Springboot Tutorials: Springboot Unofficial Tutorials | Article Summary
- More Springcoud Tutorials: The simplest springcloud tutorials in history | Article Summary
Transferred from: http://blog.csdn.net/forezp/article/details/70833629
(GO) springboot Unofficial Tutorial | Seventh article: Springboot Open declarative Transactions