Mybatic and spring-bound transaction management

Source: Internet
Author: User

Original address: http://czj4451.iteye.com/blog/2037759

MyBatis and spring, transaction management more convenient, here is the way to use transactionnal, the wrong place, I hope you point out.

1. After integration with spring, use Spring's transaction management:

A. @Transactional method:

Create the Beans-da-tx.xml file under the Classpath and join the transaction configuration on the basis of Beans-da.xml (series five):

XML code
    1. <!--Configure the transaction manager, note that the DataSource and Sqlsessionfactorybean datasource here are consistent, otherwise the transaction will not function--
      <bean id= "TransactionManager"
      class= "Org.springframework.jdbc.datasource.DataSourceTransactionManager" >
      <property name= "DataSource" ref= "DataSource"/>
      </bean>

    2. <tx:annotation-driven transaction-manager= "TransactionManager"/><!--transaction annotations driven, classes and methods labeled @transactional will have transactional -



Service class:

Java code
    1. @Service ("UserService")
    2. Public class UserService {
    3. @Autowired
    4. Iusermapper mapper;
    5. public int batchupdateuserswhenexception () { //non-transactional
    6. User user = new User (9, "before exception");
    7. int affectedcount = mapper.updateuser (user); //Successful execution
    8. User User2 = new User ("after exception");
    9. int i = 1/ 0; //Throw run-time exceptions
    10. int affectedCount2 = Mapper.updateuser (user2); //Not implemented
    11. if (Affectedcount = = 1 && affectedCount2 = = 1) {
    12. return 1;
    13. }
    14. return 0;
    15. }
    16. @Transactional
    17. public int txupdateuserswhenexception () { //transactional
    18. User user = new User (9, "before exception");
    19. int affectedcount = mapper.updateuser (user); //Rollback due to subsequent exception
    20. User User2 = new User ("after exception");
    21. int i = 1/ 0; //Throw run-time exception, transaction rollback
    22. int affectedCount2 = Mapper.updateuser (user2); //Not implemented
    23. if (Affectedcount = = 1 && affectedCount2 = = 1) {
    24. return 1;
    25. }
    26. return 0;
    27. }
    28. }

Mybatic transaction Management in conjunction with spring

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.