H2 不香么? 为什么使用MariaDB4J ?
谈到在测试中使用内存数据库,估计首先想到的就是H2了。使用时也非常方便,只要在下述SpringBoot的配置文件里修改一下datasource即可。application.propertiesspring.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
然后一个名字为testdb的数据库就就绪了。如果要导入数据库表和基础数据的话,也只要在src/test/resouces下面提供如下两个文件
schema.sql – 创建schema和表
data.sql – 导入基础数据
看上去是不是很方便呢?
不过随着在项目中使用的深入,就发现了一些问题语法上兼容性(如注释)以及产品功能上的问题(如多层临时表),具体可以关注笔者之前整理的一个list。如果选择继续使用H2,就需要团队为H2去维护另外一套H2专用的schema。因此,使用MySQL的团队需要额外寻找一个H2的替代品,一个更兼容MySQL的内存数据库。
经过一番搜索,MariaDB4J 终于走进了团队的视野。https://github.com/vorburger/MariaDB4j这个开源项目的目标就是让用户use MariaDB (MySQL(R)) from Java without ANY installation / external dependencies
在SpringBoot项目中使用
来看看如何使用它。以下是一个测试专用的App基类package com.testlink4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Configuration;
import ch.vorburger.mariadb4j.springframework.MariaDB4jSpringService;
@Configuration
@SpringBootApplication
// mapper 接口类扫描包配置
@MapperScan("com.testlink4j.dao")
public class TestApplication{//extends Application {
public static void main(String[] args){
SpringApplication.run(TestApplication.class,args);
}
private final MariaDB4jSpringService mariaDB4JSpringService;
@Autowired
public TestApplication (MariaDB4jSpringService mariaDB4JSpringService){
this.mariaDB4JSpringService= mariaDB4JSpringService;
}
}
通过给Spring应用提供一个测试用的TestApplication,并提供一个带有MariaDB4jSpringService 作为入参,就可以实现数据库的启动了。. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.2.5.RELEASE)
INFO 7264 --- [ main] c.t.s.impl.TestProjectServiceImplTest : No active profile set, falling back to default profiles: default
.s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 35ms. Found 0 JPA repository interfaces.
INFO 7264 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 0 (http)
INFO 7264 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
INFO 7264 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.31]
INFO 7264 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
INFO 7264 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 7118 ms
INFO 7264 --- [ main] ch.vorburger.mariadb4j.DB : Installation complete.
INFO 7264 --- [ main] ch.vorburger.mariadb4j.DB : Starting up the database...
INFO 7264 --- [ main] ch.vorburger.mariadb4j.DB : Database startup complete.
INFO 7264 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
INFO 7264 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
INFO 7264 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 51602 (http) with context path ''
INFO 7264 --- [ main] c.t.s.impl.TestProjectServiceImplTest : Started TestProjectServiceImplTest in 21.636 seconds (JVM running for 24.737)
INFO 7264 --- [ main] c.t.s.impl.TestProjectServiceImplTest :
我们可以看到,在Spring容器启动初期,会安装、启动一个mariadb4j并装载schema和data。
SpringBoot中的配置
在application-test.properties文件中使用如下配置,#Location of db files. delete this directory if you need to recreate from scratch
mariaDB4j.dataDir=./data/local
#Default is 3306, so using 3307 just in case it is already running on this machine
mariaDB4j.port=3307
app.mariaDB4j.databaseName=testdb
spring.datasource.url=jdbc:mariadb://localhost:3307/
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect