01.配置MyBatis-Plus测试环境

本次实验我准备使用H2数据库,虽然我现在有足够多的脚本快速启动一个MySQL、PG,但是这毕竟会增加我下次启动该项目时的时间成本。H2数据库用于技术验证是非常不错的,可以将代码和数据库集中在一个项目中,很有研究价值,所以我决定使用H2数据库,顺便积累这个数据的使用经验。

准备工作

引入依赖

 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

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>3.4.3.1</version>
    </dependency>
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.20</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>

进行配置

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21

spring:
  application:
    name: mybatis
  datasource:
    schema: classpath:h2/schema.sql
    data: classpath:h2/data.sql
    username: san
    password:
    driver-class-name: org.h2.Driver
    url: jdbc:h2:file:~/test
  h2:
    console:
      enabled: true
      path: /h2-console
      settings:
        web-allow-others: true
        trace: true
mybatis-plus:
  mapper-locations: classpath:mapper/*Mapper.xml

开发Entity和Mapper

User.java

 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

package fun.junjie.mybatis.entity;

import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;


@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@TableName("t_user")
public class User {

    /**
     * 主键
     */
    private String id;

    /**
     * 用户名
     */
    private String name;

}

UserMapper.java

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12

package fun.junjie.mybatis.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import fun.junjie.mybatis.entity.User;
import org.springframework.stereotype.Repository;

@Repository
public interface UserMapper extends BaseMapper<User> {

}

准备schema.sql和data.sql

schema.sql

1
2
3
4
5
6
7
8

DROP TABLE t_user;

CREATE TABLE t_user (
	id varchar(100) PRIMARY KEY NOT NULL,
	name varchar(100) NOT NULL
);

data.sql

1
2
3

INSERT INTO t_user VALUES ('1000', 'zhansan')

测试工作

测试MyBatis-Plus集成是否生效

测试代码如下:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21

package fun.junjie.mybatis.mapper;

import fun.junjie.mybatis.entity.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class UserMapperTest {

    @Autowired
    private UserMapper userMapper;

    @Test
    void selectById() {
        User user = userMapper.selectById("1000");
        System.out.println("");
    }
}

断点发现,user正确的获取到值了,说明h2、MyBatis的集成是有效的。

测试mapper.xml文件能否正常使用

修改UserMapper文件,代码如下:

1
2
3
4
5
6
7
8

@Repository
public interface UserMapper extends BaseMapper<User> {

    User selectByIdCustom(String userId);

}

开发相应的mapper.xml文件,代码如下:

1
2
3
4
5
6
7
8
9

<?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="fun.junjie.mybatis.mapper.UserMapper">
    <select id="selectByIdCustom" resultType="fun.junjie.mybatis.entity.User">
        SELECT * FROM t_user WHERE id = #{userId}
    </select>
</mapper>

开发相应的测试方法:

1
2
3
4
5
6
7

@Test
void selectByIdCustom() {
    User user = userMapper.selectByIdCustom("1000");
    System.out.println("");
}

断点发现,最后获取的数据是我需要的。

截止到目前,我已经完成了基础的实验环境搭建,接下将一一实现我的实验目标。