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

1  Packagecom.xxx.firstboot.exception;2 3 Importorg.springframework.dao.DataAccessException;4 5 /**6 * Custom Exceptions for testing transactions7  */8  Public classUserexceptionextendsdataaccessexception{9 Ten     Private Static Final LongSerialversionuid = 8901479830692029025L; One  A      Publicuserexception (String msg) { -         Super(msg); -     } the  -}
View Code

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

2, Com.xxx.firstboot.dao.UserDao

 1  public  int   Insertuser (string username, string password) { 2  return   Usermapper.insertuser ( Username, password);  3   4  5  public  void   Testtransactional (String username) { 6   New  userexception ("Test Transaction"  7 } 
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

1     @Transactional 2      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

1@ApiOperation ("Test Transaction")2 @ApiImplicitParams ({3@ApiImplicitParam (paramtype= "Query", name= "username", datatype= "String", required=true, value= "User's name", defaultvalue= "Zhaojigang"),4@ApiImplicitParam (paramtype= "Query", name= "password", datatype= "String", required=true, value= "User's password", defaultvalue= "Wangna")5     })6 @ApiResponses ({7@ApiResponse (code=400,message= "request parameter not completed"),8@ApiResponse (code=404,message= "Request path not or page jump path is not correct")9     })Ten@RequestMapping (value= "/testtransaction", method=requestmethod.get) One      Public voidTesttransaction (@RequestParam ("username") String username, A@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
    1     /** 2      * Configuration transaction manager 3      */ 4     @Bean5    @Primary6public     throws  exception{7         returnnew  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.