SpringBoot (3) 集成Mybatis实现简单的增删改查

       1,新建SpringBoot项目

  

  

 

 

      2,导入所需的依赖

 1          //web支持      
 2          <dependency>
 3             <groupId>org.springframework.boot</groupId>
 4             <artifactId>spring-boot-starter-web</artifactId>
 5         </dependency>
 6         //mybatis 模块
 7         <dependency>
 8             <groupId>org.mybatis.spring.boot</groupId>
 9             <artifactId>mybatis-spring-boot-starter</artifactId>
10             <version>2.1.1</version>
11         </dependency>
12       //mysq连接驱动
13         <dependency>
14             <groupId>mysql</groupId>
15             <artifactId>mysql-connector-java</artifactId>
16         </dependency>
17         //测试模块
18         <dependency>
19             <groupId>org.springframework.boot</groupId>
20             <artifactId>spring-boot-starter-test</artifactId>
21             <scope>test</scope>
22         </dependency>
23         // junit测试包
24         <dependency>
25             <groupId>junit</groupId>
26             <artifactId>junit</artifactId>
27         </dependency>    

  

  3,mybatis连接配置

 

   4,编写启动类

 1 package indi.lwc.springboot2;
 2 
 3 import org.mybatis.spring.annotation.MapperScan;
 4 import org.springframework.boot.SpringApplication;
 5 import org.springframework.boot.autoconfigure.SpringBootApplication;
 6 
 7 @SpringBootApplication
 8 //@MapperScan 可以指定要扫描的mapper类路径
 9 @MapperScan("indi.lwc.springboot2.dao") //扫描该包下的接口
10 public class Springboot2Application {
11 
12     public static void main(String[] args) {
13         SpringApplication.run(Springboot2Application.class, args);
14     }
15 
16 }

  

  5,编写业务相关的代码(dao,entity,service 这些根据自己的需求编写)

     我这里直贴  mapper 映射文件。

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE mapper
 3         PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 4         "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 5 <mapper namespace="indi.lwc.springboot2.dao.IEmpDao">
 6 
 7     <select id="queryEmpList" resultType="indi.lwc.springboot2.entity.Emp" parameterType="indi.lwc.springboot2.entity.Emp">
 8         SELECT
 9         *
10         FROM emp e
11         <where>
12             <if test="emp!=null and emp.ename!=null and emp.ename!=''">
13                 AND e.ename like '%${emp.ename}%'
14             </if>
15             <if test="emp!=null and emp.esex!=null and emp.esex!=''">
16                 AND  e.esex=#{emp.esex}
17             </if>
18             <if test="emp!=null and emp.dno!=null and emp.dno!=null">
19                 AND e.dno=#{emp.dno}
20             </if>
21         </where>
22         LIMIT #{rowIndex},#{pageSize}
23     </select>
24     <select id="queryEmpCount" resultType="int" parameterType="indi.lwc.springboot2.entity.Emp">
25         SELECT count(1) FROM emp e
26         <where>
27             <if test="emp!=null and emp.ename!=null and emp.ename!=''">
28                 AND e.ename like '%${emp.ename}%'
29             </if>
30             <if test="emp!=null and emp.esex!=null and emp.esex!=''">
31                 AND  e.esex=#{emp.esex}
32             </if>
33             <if test="emp!=null and emp.dno!=null and emp.dno!=0">
34                 AND e.dno=#{emp.dno}
35             </if>
36         </where>
37     </select>
38 </mapper>

  

  6,最后用junit简单的测试一下咯

 1 package indi.lwc.springboot2;
 2 
 3 import indi.lwc.springboot2.dao.IEmpDao;
 4 import indi.lwc.springboot2.entity.Emp;
 5 import org.junit.Test;
 6 import org.junit.runner.RunWith;
 7 import org.springframework.boot.test.context.SpringBootTest;
 8 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 9 import org.springframework.test.context.junit4.SpringRunner;
10 import org.springframework.test.context.web.WebAppConfiguration;
11 
12 import javax.annotation.Resource;
13 import java.util.List;
14 
15 //junit4运行环境
16 @RunWith(SpringRunner.class)
17 //单元测试时需要执行的SpringBoot启动类(根据需要引入),它就会扫描该类的后生包。
18 @SpringBootTest(classes= Springboot2Application.class)
19 public  class Springboot2ApplicationTests {
20 
21     @Resource
22     private IEmpDao empDao;
23 
24     @Test
25    public void testQueryEmps() {
26         //做个简单的查询
27         List<Emp> empList =  empDao.queryEmpList(new Emp(),1,10);
28         for (Emp e:empList
29              ) {
30             System.out.println(e.toString());
31         }
32     }
33 
34 
35 
36 } 

  

需要源码的留言即可,乐意分享!

posted @ 2020-03-10 15:58  听~风在北边  阅读(316)  评论(0编辑  收藏  举报