SoFunction
Updated on 2025-03-08

How to enable declarative transactions in springboot

It is very simple to start a transaction in springboot, you only need annotation @Transactional. Because in springboot, all transactions have been enabled for jpa, jdbc, and mybatis by default, and when they are introduced, things are enabled by default. Of course, if you need to use other orm, such as beatlsql, you need to configure the relevant thing manager yourself.

Preparation phase

The code of the above article is an example, that is, springboot integrates mybatis. The previous article is based on annotations to implement the data access layer of mybatis. This article is based on XML and enables declarative transactions.

Environmental dependency

Introduce mybatis startup dependency in the pom file:

<dependency>
      <groupId></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></groupId>
      <artifactId>druid</artifactId>
      <version>1.0.29</version>
    </dependency>

Initialize the database script

-- 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');

Configure data source

=jdbc:mysql://localhost:3306/test
=root
=123456
-class-name=
-locations=classpath*:mybatis/*
-aliases-package=

By configuring -locations, I specify the location of the mapper's xml file. I placed it under the resources/mybatis file. -aliases-package to indicate the package of the entity mapped to the database.

After the above steps, springboot can access the database through mybatis.

Create entity class

public class Account {
  private int id ;
  private String name ;
  private double money;

  getter..
  setter..

 }

Data access 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 "-////DTD Mapper 3.0//EN"
    "/dtd/">
<mapper namespace=".AccountMapper2">


  <update >
    UPDATE account set money=#{money} WHERE id=#{id}
  </update>
</mapper>

Service layer

@Service
public class AccountService2 {

  @Autowired
  AccountMapper2 accountMapper2;

  @Transactional
  public void transfer() throws RuntimeException{
    (90,1);//User 1 cuts 10 yuan, user 2 cuts 10 yuan, user 2 cuts 10 yuan    int i=1/0;
    (110,2);
  }
}

@Transactional, declare transactions and design a transfer method, 10 yuan is reduced by user 1, and 2 is increased by user 1. After user 1 is reduced by 10, an exception is thrown, that is, user 2 is added with 10 yuan and cannot be executed. After adding @Transactional, neither of them has increased or decreased. When @Transactional is not added, user 1 is reduced by 10, user 2 does not increase, that is, user 2's data is not operated. It can be seen that the @Transactional annotation opens things.

Conclusion

Springboot is very simple to open things, you only need to add a line of annotations, as long as you use jdbctemplate, jpa, mybatis, a common orm.

Source code download:/forezp/SpringBootLearning

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.