spring boot 整合mybatis及使用Junit進行測試


一. spring boot 整合mybatis

1.整合思路:

  1.1 添加依賴 mybatis

  1.2 在配置文件中配置數據源信息

  1.3 編寫pojo mapper接口 mapeer映射文件

  1.4手動配置mybatis的包掃描,在主啟動類添加@MapperScan

    1.5 啟動springboot服務器

2.開始工程部署:

  2.1:添加依賴 mybatis

    

復制代碼
<!--整合springboot與mybatis的整合--> <dependencies> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.1.1</version> </dependency> </dependencies> <!--將mapper下的mapper接口與mapper映射文件放在一個mapper包下所需要的依賴--> <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:在配置文件中配置數據源信息   application.yml

復制代碼
#DB Configation  JPA Configation
spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/test
//注意如果出現了無法連接數據庫問題,在tx后面添加 ?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone = GMT
     username: root
  password: root 
jpa: database: MySQL
generate-ddl: true
show-sql: true
復制代碼

2.3書寫pojo實體類和對應的mapper接口及映射文件

 

 如果將接口和映射文件放在一個包下,那么會遇到:

錯誤:org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.offcn.mapper.UserMapper.getUserList

解決方案:

1:在resources目錄下建立一個和mapper接口相同的目錄結構,把mapper映射文件放進去

2:如果想把mapper接口和mapper映射文件放在一起

那么在pom.xml中添加如下配置

復制代碼
  <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> </build>
復制代碼

 

pojo實體類

package com.wf.pojo; public class Muser { private Integer id; private String username; private String password; private String name;   //此處添加set,get,構造方法以及重寫toString
}

 

 

mapper接口:

復制代碼
package com.wf.mapper; import com.wf.pojo.Muser; import java.util.List; public interface MuserMapper { List<Muser> getUserList(); }
復制代碼

mapeer映射文件:

復制代碼
<?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>
復制代碼

對應的controller中書寫接口方法:

復制代碼
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(){ return muserMapper.getUserList(); } }
復制代碼

2.4手動配置mybatis的包掃描,在主啟動類添加@MapperScan

復制代碼
package com.wf; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; //掃描mapper包下的所有mapper接口和映射文件 @MapperScan(basePackages = "com.xhn.mapper") //添加啟動類 @SpringBootApplication public class StartApplication { public static void main(String[] args) { SpringApplication.run(StartApplication.class,args); } }
復制代碼

 2.5 啟動springboot服務器

 二:使用Junit進行測試

使用用法:

1 添加依賴

        <!--測試junit--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>

 

2 創建測試類

 

 

3 在測試類上添加注解,並注入測試對象

復制代碼
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 AppTest { //依賴注入  @Autowired private MuserMapper mapper; @Test public void test01(){ //先獲取所有數據 List<Muser> userList = mapper.getUserList(); for (Muser muser : userList) { System.out.println(muser); } } }
復制代碼

 


 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM