多数据源下内存数据库的使用
一、h2数据库
1. 引入依赖
<!--多数据源-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>3.5.2</version>
</dependency>
<!--h2 内存数据库-->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>2.1.214</version>
<scope>test</scope>
</dependency>
2. application.yml配置
由于是测试使用的,因此该文件特指test/resource目录下的application.yml
spring:
profiles:
active: test
datasource:
driver-class-name: org.h2.Driver
# 纯粹的多数据库
dynamic:
primary: h2-first #设置默认的数据源或者数据源组
strict: false #严格匹配数据源,默认false. true未匹配到指定数据源时抛异常,false使用默认数据源
datasource:
h2-first:
url: jdbc:h2:mem:test_db1
username: 'root1'
password: '123456'
init:
schema: classpath:db1-sql/schema.sql
data: classpath:db1-sql/data.sql
h2-second:
url: jdbc:h2:mem:test_db2
username: 'root2'
password: '123456'
init:
schema: classpath:db2-sql/schema.sql
data: classpath:db2-sql/data.sql
自己在test/resource目录下创建数据库初始化脚本即可,如配置中的db1-sql/schema.sql
和db1-sql/data.sql
需要注意的是,h2的建表语句和mysql有所不同,h2建表语句似乎是不可以添加字符长度的。
当然,这可能是我所使用的h2版本问题。
3. mapper层控制数据源
如果使用默认数据源,那么mapper文件不需要做任何操作
如果想要指定该mapper使用的是其他数据源,那么需要添加@DS
注解
如:
@DS("h2-second")
public interface UserMapper extends BaseMapper<User> {}
二、内嵌mysql
1. 引入依赖
多数据源依赖同上,不再列出。这里只列出内嵌mysql的依赖
<dependency>
<groupId>com.wix</groupId>
<artifactId>wix-embedded-mysql</artifactId>
<version>4.6.1</version>
<scope>test</scope>
</dependency>
2. application.yml
为了减小篇幅,这里就列出一个数据源了,更多数据源的配置就无脑参考第一个,往下添加即可。
spring:
datasource:
dynamic:
primary: mysql-db1
strict: false
datasource:
mysql-db1:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/test1?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
username: 'test'
password: '123456'
lazy: true
3. 测试类编写
基本测试类
import com.newtank.scorpio.paas.infrastructure.TestStarter;
import com.wix.mysql.EmbeddedMysql;
import com.wix.mysql.ScriptResolver;
import com.wix.mysql.config.MysqldConfig;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;
import static com.wix.mysql.EmbeddedMysql.anEmbeddedMysql;
import static com.wix.mysql.config.Charset.UTF8;
import static com.wix.mysql.config.MysqldConfig.aMysqldConfig;
import static com.wix.mysql.distribution.Version.v5_6_21;
// todo 自己写一个TestStarter类(启动类)
@SpringBootTest(classes = TestStarter.class)
@RunWith(SpringRunner.class)
public class BaseDBTest {
private EmbeddedMysql embeddedMysql;
public void initEmbeddedDatabase(String initFilePath, String schemaName) {
if (embeddedMysql != null) {
System.out.println("==================embedded mysql exist==================");
return;
}
System.out.println("==================init embedded mysql==================");
// windows环境下: 5.7.x及以上版本需要 MSVCR120.dll 这个文件, 如果没有这个文件的话,就用5.6.x版本
MysqldConfig config = aMysqldConfig(v5_6_21)
// 基础设置
.withCharset(UTF8)
.withPort(3306)
.withUser("test", "123456")
.withTimeZone(TimeZone.getTimeZone("GMT+:08:00").getID())
.withTimeout(2, TimeUnit.MINUTES)
.withServerVariable("max_connect_errors", 666)
.build();
embeddedMysql = anEmbeddedMysql(config)
.addSchema(schemaName, ScriptResolver.classPathScript(initFilePath))
.start();
}
}
正式测试类
import org.junit.Before;
import org.junit.Test;
import javax.annotation.Resource;
import java.util.List;
import static org.junit.Assert.assertEquals;
// fixme 这些实体和service只是作为例子
public class AgentTest extends BaseDBTest {
@Resource
private IAgentServiceD agentServiceD;
@Before
public void setUp() {
// todo 需要自己准备init.sql
initEmbeddedDatabase("db1/init.sql", "test1");
}
@Test
public void testSelect() {
List<Agent> list = agentServiceD.list();
assertEquals(3, list.size());
}
}