This is the fourth day of my participation in the August More text Challenge. For details, see:August is more challenging”
Come to Spring BootWe talked about SpringBoot integration with MyBatis, and today we’re talking about SpringBoot transaction support
💎Spring Boot transaction support
🎀What is a transaction?
When we develop enterprise applications, one of the operations for business people is actually a combination of multiple steps to read and write data. When data operations are performed sequentially, exceptions may occur at any step. If an exception occurs, subsequent operations cannot be completed. In this case, the service logic is not correctly completed and the data of previous successful operations is unreliable.
The function of a transaction is to ensure that every operation of the user is reliable. Every step of the operation in the transaction must be executed successfully. As long as there is an exception, it will be back to the state where no operation was performed at the beginning of the transaction.
Transaction management is one of the most commonly used features of the Spring framework, and will be used in most cases when developing applications using Spring Boot.
Spring Boot uses transactions very simply, and the underlying transaction management provided by Spring itself is still used
➢ use annotations in the entrance class @ EnableTransactionManagement open transaction support
Add the @transactional annotation to the Service method that accesses the database
🎀1. Case thinking
Through SpringBoot +MyBatis to achieve the database student table update operation, built in the method of service layer
If no, check whether the transaction takes effect
Project name: 012-Springboot-web-MyBatis -transacation
This project is to add new methods on the basis of 011, and demonstrate the case in the new methods
🎀2. Implementation steps
Continue with the previous article
(9) Add a method to update students in StudentController
(10) Add the update student method to the StudentService interface
(11) Implement the update student method in the StudentServiceImpl interface implementation class, build an exception and put the @Transactional annotation on the method
(12) on the Application class add @ EnableTransactionManagement open transaction support
@ EnableTransactionManagement optional, but must add @ Transactional affairs on business method to take effect
(13) Start the Application and access it through the browser for testing
The above results indicate that the transaction worked
The @Transactional test database on StudentServiceImpl has been updated