Enterprise Distribution Micro Service Cloud Springcloud springboot MyBatis (13) Spring Boot integration MyBatis

Source: Internet
Author: User

Spring integration MyBatis Not much to say, the recent use of spring boot, so to organize the spring boot in the integration of MyBatis steps. Search the spring boot integration MyBatis articles, methods are relatively old, more cumbersome. Check the documentation, the actual support for a simpler integration and use. Here's a detailed description of how to integrate MyBatis in spring boot and implement the mapping through annotations.

Integrated MyBatis
  • Create a new spring boot project, or operate on a Chapter1 basis

  • pom.xmlIn the introduction of dependency

  • The Spring-boot-starter base and spring-boot-starter-test are used for unit testing to verify data access.

  • Introducing the necessary dependency Mysql-connector-java to connect to MySQL

    <parent><groupId>org.springframework.boot</groupId><artifactId> Spring-boot-starter-parent</artifactid><version>1.3.2.release</version><relativepath/> <!--lookup parent from repository--></parent><dependencies><dependency><groupid> org.springframework.boot</groupid><artifactid>spring-boot-starter</artifactid></ Dependency><dependency><groupid>org.springframework.boot</groupid><artifactid> Spring-boot-starter-test</artifactid><scope>test</scope></dependency><dependency ><groupid>org.mybatis.spring.boot</groupid><artifactid>mybatis-spring-boot-starter</ Artifactid><version>1.1.1</version></dependency><dependency><groupid>mysql </groupId><artifactId>mysql-connector-java</artifactId><version>5.1.21</version> </dependency></dependencies>

    As previously described using the JDBC and Spring-data connection database, configure the application.properties MySQL connection configuration in

    Spring.datasource.url=jdbc:mysql://localhost:3306/testspring.datasource.username= Rootspring.datasource.password=123456spring.datasource.driver-class-name=com.mysql.jdbc.driver

    As with other spring boot projects, the basic configuration is simple and concise, and the following is a quick and easy way to access the database using MyBatis.

Using MyBatis

Create a user table in MySQL that contains the ID (BIGINT), name (INT), and Age (VARCHAR) fields. Also, create a mapping object user

public class User {    private Long ID;    private String name;    Private Integer age;    Omit getter and Setter}

Create user mapping operation Usermapper, for subsequent unit test validation, implement insert and query operations

@Mapperpublic interface Usermapper {    @Select ("select * from USER WHERE NAME = #{name}")    USER findbyname (@Param ("N Ame ") String name);    @Insert ("INSERT into USER (NAME, age) VALUES (#{name}, #{age})")    int Insert (@Param (' name ') String NAME, @Param ("age") Integer age);}

To create a spring boot main class

@SpringBootApplicationpublic class Application {public static void main (string[] args) {Springapplication.run ( Application.class, args);}}

  

    • Creating unit Tests

    • Test logic: Insert a NAME=AAA,AGE=20 record, then query according to NAME=AAA and determine if age is 20
    • Test end rollback data to ensure independent data environment for each run of the test unit
      @RunWith (Springjunit4classrunner.class) @SpringApplicationConfiguration (classes = application.class) public class applicationtests {@Autowiredprivate usermapper usermapper; @Test @rollbackpublic void Findbyname () throws Exception { Usermapper.insert ("AAA", 20); User u = usermapper.findbyname ("AAA"); Assert.assertequals (U.getage (). Intvalue ());}}

        Source Source

Enterprise Distribution Micro Service Cloud Springcloud springboot MyBatis (13) Spring Boot integration MyBatis

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.