Sixth Chapter Springboot + Business

Source: Internet
Author: User

In the actual development, actually seldom uses the transaction, generally the transaction uses more is in the money computation aspect.

When MyBatis integrates with spring, what does the transaction do? In fact, it is very simple to add @transactional annotations to the corresponding method (usually the service layer) on the basis of the previous section of the code.

1, Com.xxx.firstboot.exception.UserException

 Packagecom.xxx.firstboot.exception;Importorg.springframework.dao.DataAccessException;/*** Custom Exceptions for testing transactions*/ Public classUserexceptionextendsdataaccessexception{Private Static Final LongSerialversionuid = 8901479830692029025L;  Publicuserexception (String msg) {Super(msg); }}
View Code

Description: This is a custom annotation that inherits the DataAccessException class.

2, Com.xxx.firstboot.dao.UserDao

 Public int Insertuser (string Username, string password) {        return  usermapper.insertuser (username, password);    }      Public void testtransactional (String username) {        thrownew userexception ("Test Transaction");    
View Code

Description: The test for a transaction in this class only uses two methods, and the second method testtransactional throws a custom exception.

3, Com.xxx.firstboot.service.UserService

@Transactional2 public     void  testtransaction (string Username, string password) { 3         System.out.println (userdao.insertuser (username, password)); 4         userdao.testtransactional (username); 5     }
View Code

Description: The two methods of the above Userdao are called in this method.

The first method inserts a piece of data into the database, the second method throws our custom exception, and if the transaction is configured successfully, the first method inserts the database back, otherwise the data is inserted successfully.

4, Com.xxx.firstboot.controller.UserController

@ApiOperation ("Test Transaction") @ApiImplicitParams ({@ApiImplicitParam (Paramtype= "Query", name= "username", datatype= "String", required=true, value= "User's name", defaultvalue= "Zhaojigang"), @ApiImplicitParam (Paramtype= "Query", name= "password", datatype= "String", required=true, value= "User's password", defaultvalue= "Wangna")}) @ApiResponses ({@ApiResponse (code=400,message= "Request parameter not filled out."), @ApiResponse (code=404,message= "Request path not or page jump path is not correct")}) @RequestMapping (value= "/testtransaction", method=requestmethod.get) Public voidTesttransaction (@RequestParam ("username") String username, @RequestParam ("Password"String password) {userservice.testtransaction (username, password); }
View Code

Test:

Start the service with the MAVEN command-->swagger run url--> see if the database is plugged in successfully

Question: Check a lot of information, MyBatis and Springboot integration (data source adopted druid), in order to add transactions, many people will be in the mybatisconfig of the previous section of this class to do two things please

    • Add the @enabletransactionmanagement annotation on the Mybatisconfig class, which enables the annotation-type transaction management <tx:annotation-driven/> This @transactional annotation on the method works, but the actual test does not add this sentence, @Transactional annotations are still useful
    • Added a method to get the transaction manager in the Mybatisconfig class
/** 2      *      Configure transaction manager 3*/4     @Bean5     @Primary6      Public throws exception{7         return New Datasourcetransactionmanager (Getdatasource ()); 8     }
View Code

Add this sentence: Use the transaction manager in the method to manage transactions in places where @transactional annotations are used.

Sixth Chapter Springboot + Business

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.