A. Spring boot integrate mybatis
1. Integration ideas:
1.1 Add rely mybatis
1.2 the configuration data in the configuration file source information
1.3 Interface mapeer write pojo mapper mapping file
1.4
1.5 server start springboot
2. Start the deployment project:
2.1: Adding rely mybatis
<! - Integration Integration of springboot and mybatis ->
< Dependencies > < dependency > < the groupId > org.mybatis.spring.boot </ the groupId > < the artifactId > mybatis-springboot-Starter </ the artifactId > < Version > 1.1.1 </ Version > </ dependency > </ Dependencies > <-! the mapper interface mapper mapper mapping file in dependence on the next packet mapper required -> < Build > < Resources ><resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.*</include> </includes> <filtering>false</filtering> </resource> </resources> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
2.2: configuration data source information in the configuration file application.yml
#DB Configation JPA Configation
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/test
username: root
password: root
jpa:
database: MySQL
generate-ddl: true
show-sql: true
If you do not want to interfaces and mapping files in a package under, you will encounter:
错误:org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.offcn.mapper.UserMapper.getUserList
Solution:
A mapping file into exactly the same directory structure directory resources
2. According to the above configuration dependent plug behind to
mapper interfaces:
package com.wf.mapper;
import com.wf.pojo.Muser;
import java.util.List; public interface MuserMapper { List<Muser> getUserList(); }
mapeer mapping file:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.xhn.mapper.MuserMapper"> <select id="getUserList" resultType="com.xhn.pojo.Muser"> select * from user </select> </mapper>
Corresponding to the writing controller interface methods:
package com.wf.controller;
import com.wf.mapper.MuserMapper;
import com.wf.pojo.Muser; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController @RequestMapping("/user") public class UserController { @Autowired private MuserMapper muserMapper; //使用mybatis查询出所有数据 @RequestMapping("/list1") public List<Muser> getUserList1(){ returnmuserMapper.getUserList (); }}
2.4
Package com.wf; Import org.mybatis.spring.annotation.MapperScan; Import org.springframework.boot.SpringApplication; Import org.springframework.boot.autoconfigure.SpringBootApplication; // all interfaces mapper and mapper mapping file in the package scanning @ MapperScan (basePackages = "com.xhn.mapper" ) // add startup class @SpringBootApplication public class the StartApplication { public static void main (String [] args) {SpringApplication.run (the StartApplication. class , args);}}
2.5 server start springboot
Two: Use Junit test
Use usage:
Add 1 dependent
<!--测试junit-->
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
2 Create a test class
package com.wf;
import com.wf.mapper.MuserMapper;
import com.wf.pojo.Muser; 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.context.junit4.SpringRunner; import java.util.List; @RunWith(SpringRunner.class) //加载主启动类 @SpringBootTest(classes = StartApplication.class) public class{The AppTest // DI @Autowired Private MuserMapper Mapper; @Test public void Test01 () { // first acquires all the data List <Muser> userList = mapper.getUserList (); for (MUSER Muser: userList) {the System.out. println (muser);}}}