spring mybatis-plus

pom.xml

1
2
3
4
5
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.1</version>
</dependency>
  • properties
    1
    2
    3
    4
    5
    6
    # DataSource Config
    MYSQL_HOST=192.168.50.28
    spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:13306/summer?allowPublicKeyRetrieval=true&useSSL=false
    spring.datasource.username=root
    spring.datasource.password=summer
    spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
  • Application add @MapperScan({"com.example.summermetrics.mapper"})
  • User.java
    1
    2
    3
    4
    5
    6
    7
    8
    @Data
    public class User {
    private Long id;
    private String name;
    private Integer age;
    private String email;
    }

  • UerMapper.java
    1
    2
    3
    4
    5
    6
    7
    8
    @Data
    public class User {
    private Long id;
    private String name;
    private Integer age;
    private String email;
    }

unit test

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter-test</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
  • 目录结构

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    .
    ├── java
    │   └── com
    │   └── example
    │   └── summermetrics
    │   ├── SummerMetricsApplicationTests.java
    │   └── mapper
    │   └── MybatisPlusSampleTest.java
    └── resources
    ├── application-test.properties
    ├── db
    │   ├── data-h2.sql
    │   └── schema-h2.sql
    └── log4j2-test.xml

  • application-test.properties

    1
    2
    3
    4
    5
    6
    7
    8
    9
    logging.config=classpath:log4j2-test.xml

    # DataSource Config
    spring.datasource.url=jdbc:h2:mem:test
    spring.datasource.username=root
    spring.datasource.password=test
    spring.sql.init.schema-locations=classpath:db/schema-h2.sql
    spring.sql.init.data-locations=classpath:db/data-h2.sql
    spring.datasource.driver-class-name=org.h2.Driver
  • MybatisPlusSampleTest

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    package com.example.summermetrics.mapper;

    import java.util.List;

    import org.junit.FixMethodOrder;
    import org.junit.internal.MethodSorter;
    import org.junit.jupiter.api.MethodOrderer;
    import org.junit.jupiter.api.Order;
    import org.junit.jupiter.api.Test;
    import org.junit.jupiter.api.TestMethodOrder;
    import org.junit.runner.OrderWith;
    import org.junit.runner.RunWith;
    import org.junit.runners.MethodSorters;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
    import org.springframework.test.annotation.Rollback;
    import org.springframework.test.context.junit4.SpringRunner;

    import com.baomidou.mybatisplus.test.autoconfigure.MybatisPlusTest;
    import com.example.summermetrics.domain.User;

    @RunWith(SpringRunner.class)
    @MybatisPlusTest
    @AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
    @TestMethodOrder(MethodOrderer.OrderAnnotation.class)
    class MybatisPlusSampleTest {

    @Autowired
    private UserMapper userMapper;

    @Test
    @Rollback
    @Order(0)
    void testInsert() {
    User user = User.builder().id(1111L).name("test").age(3).email("email").build();
    userMapper.insert(user);
    User user1 = userMapper.selectById(1111L);
    assert user1.getName().equals("test");
    }

    @Test
    @Order(1)
    void testGetUser() {

    List<User> users = userMapper.selectList(null);
    assert users.size() == 5;
    }
    }

    参考文章

  • 官方文档

  • Spring boot Mybatis-Plus数据库单测实战(三种方式)

  • my demo

评论