Spring Cloud Spring Boot mybatis Distributed Microservice Cloud Architecture (25) Integration of MyBatis

Keywords: Spring Mybatis MySQL JDBC

Integrating MyBatis in Spring is not much to say. Spring Boot has been used extensively recently, so tidy up the steps of integrating MyBatis in Spring Boot. I searched Spring Boot's articles on integrating MyBatis, and the methods are old and cumbersome. After reviewing the documentation, we have actually supported simpler integration and use. Here's how to integrate MyBatis in Spring Boot and implement mapping through annotations.

Integrating MyBatis

  • New Spring Book project, or Chapter1 Operate on the basis of

  • Introducing dependencies into pom.xml

    • The spring-boot-starter foundation and spring-boot-starter-test are used here for unit test validation data access.
    • Necessary dependencies on mysql-connector-java for introducing connection to MySQL
    • Introducing MyBatis Core Dependence on mybatis-spring-boot-starter
    • The spring-boot-starter-jdbc dependency is not introduced here because it is already included in mybatis-spring-boot-starter.
      <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>

       

  • Configure mysql's connection configuration in application.properties, as described earlier, using jdbc and spring-data to connect databases
    spring.datasource.url=jdbc:mysql://localhost:3306/test
    spring.datasource.username=root
    spring.datasource.password=123456
    spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    

    Like other Spring Boot projects, the basic configuration is simple and concise. Here's how to use MyBatis to access the database easily and conveniently.

    Use MyBatis

  • Create a User table in Mysql that contains id(BIGINT), name(INT), age(VARCHAR) fields. At the same time, create the mapping object User
    public class User {
    
        private Long id;
        private String name;
        private Integer age;
    
        // Omit getter and setter
    
    }

     

  • User Mapper, the operation of creating User mapping, implements insert and query operations for subsequent unit test validation
    
    @Mapper
    public interface UserMapper {
    
        @Select("SELECT * FROM USER WHERE NAME = #{name}")
        User findByName(@Param("name") String name);
    
        @Insert("INSERT INTO USER(NAME, AGE) VALUES(#{name}, #{age})")
        int insert(@Param("name") String name, @Param("age") Integer age);
    
    }

     

  • Create Spring Boot main class
    @SpringBootApplication
    public class Application {
    
    	public static void main(String[] args) {
    		SpringApplication.run(Application.class, args);
    	}
    
    }

    Create unit tests

  • Test logic: Insert a record with name=AAA, age=20, then query according to name=AAA, and determine whether age is 20
  • Roll back the data at the end of the test to ensure the independence of the data environment for each run of the test unit
    
    @RunWith(SpringJUnit4ClassRunner.class)
    @SpringApplicationConfiguration(classes = Application.class)
    public class ApplicationTests {
    
    	@Autowired
    	private UserMapper userMapper;
    
    	@Test
    	@Rollback
    	public void findByName() throws Exception {
    		userMapper.insert("AAA", 20);
    		User u = userMapper.findByName("AAA");
    		Assert.assertEquals(20, u.getAge().intValue());
    	}
    
    }

    Source of source code

Posted by Frozenlight777 on Fri, 14 Dec 2018 06:21:03 -0800