spring boot + mybatis + h2 配置 + 源码

开发需要写单测,遇到持久化测试的场景。如果用 MySQL 或者其他本地数据库,等项目迁移后需要重新配置数据库,很麻烦。更难以接受的是 单测时 MySQL 不会回滚数据,这导致单测跑完,数据库里存了一堆数据。手动不清空可能会影响到下次测试,更有甚者是在一次测试中,前后两个单测相互影响,跑挂了测试。

如果采用内存数据库,就可以避免上述的问题。之前配置过,结果忘得差不多了。这次因为其他需求,要在搞一遍,顺带这记录下配置细节。

1、源码

demo

2、 添加依赖

compile group: 'com.h2database', name: 'h2', version: '1.4.200'

3、配置 H2

# H2
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console
# Datasource
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect

4、创建表并插入几条记录

在 Spring Boot 项目的 resource 路径下,创建两个文件: data.sqlschema.sql。项目启动时,会自动加载这两张表里的数据。

-- schema.sql

create table users
(
   name varchar(255) not null,
   password varchar(255) not null,
   PRIMARY KEY(name)
);
-- data.sql

insert into users
values('Aseem', '1234');

insert into users
values('Firdous', '5678');

insert into users
values('diego', '123');

5、测试 H2 配置效果

H2 的配置工作到这里就做完了。项目启动后,h2自动创建 users 表和插入三条记录。

测试下:

在浏览器中输入:http://localhost:8080/h2-console

在这里插入图片描述

点击连接

在这里插入图片描述

6、mybatis 的配置

顺便也记录下吧。

6.1 依赖

这里要加两个,一个用在测试中,另一个用在生产环境中。

implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.1.0'
    testCompile group: 'org.mybatis.spring.boot', name: 'mybatis-spring-boot-starter-test', version: '2.1.3'

6.2 配置

application.properties

# 声明 mybatis 的配置文件位置。
mybatis.config-location=classpath:mybatis-config.xml
# 声明 mybatis 所有的 XXXMapper.xml 所在位置。
mybatis.mapper-locations=classpath:mapper/*.xml

mybatis-config.xml。如果没有定义 handler,下面的内容可以直接复制。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
  <settings>
    <!-- Globally enables or disables any caches configured in any mapper under this configuration -->
    <setting name="cacheEnabled" value="true"/>
    <!-- Sets the numfindByNamer of seconds the driver will wait for a response from the database -->
    <setting name="defaultStatementTimeout" value="3000"/>
    <!-- Enables automatic mapping from classic database column names A_COLUMN to camel case classic Java property names aColumn -->
    <setting name="mapUnderscoreToCamelCase" value="true"/>
    <!--<setting name="logImpl" value="STDOUT_LOGGING"/>-->
    <!-- Allows JDBC support for generated keys. A compatible driver is required.
    This setting forces generated keys to be used if set to true,
     as some drivers deny compatibility but still work -->
    <setting name="useGeneratedKeys" value="true"/>
  </settings>

  <!-- Continue going here -->

</configuration>

6.3 单测

import com.example.h2.domain.User;
import com.example.h2.domain.UserRepository;
import java.util.Optional;
import org.junit.jupiter.api.Test;
import org.mybatis.spring.boot.test.autoconfigure.MybatisTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Import;

@MybatisTest
@Import(MybatisUserRepository.class)
class MybatisUserRepositoryTest {

  @Autowired
  private UserRepository userRepository;

  @Test
  public void should_get_user_succeed(){
    Optional<User> optional = userRepository.ofId("diego");
    assertThat(optional.isPresent(),is(true));

    User fetched = optional.get();
    assertThat(fetched.getName(),is("diego"));
    assertThat(fetched.getPassword(),is("123"));
  }

}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值