Spring Boot integrated mybatis+ transaction control

Source: Internet
Author: User

The Code for demo demo, using annotations to complete the integration of spring boot and mybatis, and to control things

Configuration of the data source:

1 spring.datasource.url=jdbc:mysql://localhost:3306/book2 Spring.datasource.username=Root3 spring.datasource.password=4 spring.datasource.driver-class-name=com.mysql.jdbc.driver

Dependent Packages:

1 <dependency>2     <groupId>mysql</groupId>3     <artifactid >mysql-connector-java</artifactId>4 </dependency>5 <dependency>6     <groupId>org.mybatis.spring.boot</groupId>7     <artifactId> Mybatis-spring-boot-starter</artifactid>8     <version>1.0.0</version>9 </dependency>

Mapper:

@Configuration  Public Interface Bookmapper {        @Update ("Update books set Publicsher =#{publicsher} where BookID = #{id}")      publicint update (@Param ("Publicsher") String Publicsher, @Param ("id")int  ID);}

Service:

@Service  Public class Implements bookservice {        @Autowired    private  bookmapper bookmapper;        @Transactional    publicvoidint  bookId) {        bookmapper.update ( Publicsher,bookid);}    }

Controller:

@RestController  Public class Bookcontroller {        @Autowired    private  bookservice bookservice;        @RequestMapping ("/updatabook")    public string Updatabook (string name,int  ID) {        bookservice.update (name,id);         return "Updata Success";    }    }

APP:

@ComponentScan (basepackages = {"Com.szzt.controller", "Com.szzt.service", "Com.szzt.exceptionHandler"}) @ Mapperscan (basepackages= "Com.szzt.mapper") @EnableAutoConfigurationpublicclass App {        publicstaticvoid  main (string[] args) {        Springapplication.run (App.class, args);    }    }

Spring Boot integrated mybatis+ transaction control

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.