mysql的内存数据库,MySQL内存数据库的新选择-MariaDB4J
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
總結
以上是生活随笔為你收集整理的mysql的内存数据库,MySQL内存数据库的新选择-MariaDB4J的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 陆军机务体制分为哪些?
- 下一篇: matlab冲击噪声,如何用MATLAB