Preface
Now the industry's more popular data operation Layer Framework MyBatis, the following explains how Springboot integration MyBatis, here is the annotation configuration sql.
The consolidation MyBatis in spring is not much to say, the recent heavy use of spring boot, so tidy up the steps of consolidating mybatis in spring boot. Search the Spring boot Integration MyBatis article, the method is relatively old, more cumbersome. After checking the documentation, it has actually supported simpler integration and use. Here's a detailed description of how to consolidate mybatis in spring boot and implement the mapping through annotations.
Integrated MyBatis
New Spring Boot Project
Pom.xml In the introduction of dependencies here Use the Spring-boot-starter base and spring-boot-starter-test to do unit tests validate data access The introduction of the need to connect MySQL to rely on the Mysql-connector-java to introduce integration MyBatis core dependencies Mybatis-spring-boot-starter here does not introduce SPRING-BOOT-STARTER-JDBC dependencies, is because this dependency is already included in the Mybatis-spring-boot-starter
<?xml version= "1.0" encoding= "UTF-8"?> <project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "http ://www.w3.org/2001/XMLSchema-instance "xsi:schemalocation=" http://maven.apache.org/POM/4.0.0 http:// Maven.apache.org/xsd/maven-4.0.0.xsd "> <modelVersion>4.0.0</modelVersion> <groupId> Com.xiaojingg</groupid> <artifactId>springbootstudy-demo9-mybatis</artifactId> <version> 0.0.1-snapshot</version> <packaging>jar</packaging> <name>springbootstudy-demo9-mybatis& Lt;/name> <description>demo Project for Spring boot</description> <parent> <GROUPID&G T;org.springframework.boot</groupid> <artifactId>spring-boot-starter-parent</artifactId> ;version>1.5.9.release</version> <relativePath/> <!--lookup parent from Repository--> < /parent> <properties> <project.build.sourceencoDing>utf-8</project.build.sourceencoding> <project.reporting.outputencoding>utf-8</ project.reporting.outputencoding> <java.version>1.8</java.version> </properties> <DEP endencies> <dependency> <groupId>org.springframework.boot</groupId> <arti factid>spring-boot-starter</artifactid> </dependency> <dependency> <groupid&
Gt;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> </dependenci es> <build> <plugins> <plugin> <groupid>org.springframework.boot
</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
Configure MySQL's connection configuration in Application.properties as described previously with JDBC and spring-data connection databases
Spring.datasource.url=jdbc:mysql://localhost:3306/test
Spring.datasource.username=root
Spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
As with other spring boot projects, simple and concise completion of the basic configuration, below see how to easily use MyBatis access to the database.
use MyBatis to create the user table in MySQL containing the ID (BIGINT), name (INT), Age (VARCHAR) fields. Also, create the mapping object user
Package com.xiaojingg.domain;
public class User {
private Long ID;
private String name;
Private Integer age;
Public Long GetId () {return
ID;
}
public void SetId (Long id) {
this.id = ID;
}
Public String GetName () {return
name;
}
public void SetName (String name) {
this.name = name;
}
Public Integer Getage () {return age
;
}
public void Setage (Integer age) {
this.age = age;
}
}
Create the action Usermapper for the user map to implement the insert and query operations for subsequent unit test validation
Package com.xiaojingg.domain;
Import org.apache.ibatis.annotations.*; @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 ') in
Teger age);
The main classes are as follows: Package com.xiaojingg;
Import org.springframework.boot.SpringApplication;
Import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication public class Springbootstudydemo9mybatisapplication {public static void main (string[] args) {
Springapplication.run (Springbootstudydemo9mybatisapplication.class, args);
Create a unit test test logic: Insert a name=aaa,age=20 record, and then according to NAME=AAA query, print data test end roll back data to ensure that the test unit every time the data Environment independent, here can be shielded here annotation!
Package Com.xiaojingg;
Import Com.xiaojingg.domain.User;
Import Com.xiaojingg.domain.UserMapper;
Import Org.junit.Assert;
Import Org.junit.Test;
Import Org.junit.runner.RunWith; Import org.springframework.beans.factory.annotation.autowired;
Import Org.springframework.boot.test.context.SpringBootTest;
Import Org.springframework.test.annotation.Rollback;
Import Org.springframework.test.context.junit4.SpringRunner;
Import org.springframework.transaction.annotation.Transactional; @RunWith (Springrunner.class) @SpringBootTest//@Transactional public class
springbootstudydemo9mybatisapplicationtests {@Autowired private usermapper usermapper;
@Test @Rollback public void Findbyname () throws Exception {Usermapper.insert ("AAA", 20);
User u = usermapper.findbyname ("AAA");
System.out.print ("=========================" +u.getage (). Intvalue ()); }
}
Run the screenshot below: