Spring Boot implemented and integrated paging unit testing mybatis

Spring Boot Mybatis achieve integration is still quite simple, here I use the database is MySQL, so first introduced MySQL connection tool

 <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.48</version>
        </dependency>
        <!-- alibaba的druid数据库连接池 -->
        <!-- https://mvnrepository.com/artifact/com.alibaba/druid-spring-boot-starter -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.21</version>
        </dependency>

Introducing mybatis Integration Toolkit

<!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.0.0</version>
        </dependency>

Note here that the problem version, the situation sometimes some conflicts can be adjusted under the relevant version, and in particular 1.3.X version 2.X gap is still there.

We need to use the paging function, so the introduction of paging Kit

<!-- mybatis分页插件 -->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.12</version>
        </dependency>

Use paging code is as follows:

        Integer page = params.getPage();
        Integer pageSize = params.getPageSize();
        //分页处理
        PageHelper.startPage(page, pageSize, true);
        List<User> classTimeList = userMapper.userList(params);
        PageInfo<User> pageInfo = new PageInfo<>(classTimeList);

 After basically this configuration is completed, the completion of the relevant yml file configuration, integration is complete. So we generally completes integration, we all want to unit test test mybatis whether a configuration problem. Spring Boot separate unit tested configuration, refer to:

Spring Boot unit testing

So there is a little different place for mybatis unit test, first introduced POM file:

<dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter-test</artifactId>
            <version>2.0.0</version>
            <scope>test</scope>
        </dependency>

Then create application.yml file resources test in

spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:8306/wecode_saas_qc?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8
    username: write_user
    password: write@codeus
#mybatis的配置
mybatis:
  type-handlers-package: com.clark.type.hanlder
  configuration:
      log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
      map-underscore-to-camel-case: true

New Spring Boot start classes

@SpringBootApplication
public class Run {
    public static void main(String[] args) {
        SpringApplication.run(Run.class, args);
    }
}

New Test class

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Run.class)
@EnableAutoConfiguration
public class RunTests {

    @Resource
    private UserMapper userMapper;


    @Test
    public void test() throws Exception {

        User user = null;

        // 新增测试
        System.out.println("------------ 新增测试 ------------");
        user = new User();
        user.setNameBig("conanli");
        user.setTelephone(String.valueOf(Math.random()));
        System.out.println("insert: " + userMapper.insert(user));

        // 更新测试
        System.out.println("------------ 更新测试 ------------");
        user = new User();
        user.setId(1L);
        user.setTelephone("111111");
        System.out.println("update: " + userMapper.updateById(user));

        // 获取测试
        System.out.println("------------ 获取测试 ------------");
        System.out.println("user: " + userMapper.getById(1L));

        // 删除测试
        System.out.println("------------ 删除测试 ------------");
        System.out.println("delete: " + userMapper.deleteById(1L));

        // 存在测试
        System.out.println("------------ 存在测试 ------------");
        System.out.println("exist: " + userMapper.existById(1L));

        System.out.println("all"+userMapper.getAll());

    }
}

New Test Mapper class, BaseMapper here refer to Mybatis modification

@Mapper
public interface UserMapper extends BaseMapper<User,Long> {


    @Select("select * from user ")
    List<User> getAll();
}

OK, configured and ready to run the test class.

He published 183 original articles · won praise 37 · views 160 000 +

Guess you like

Origin blog.csdn.net/zhuwei_clark/article/details/104942582