日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

mysql的内存数据库,MySQL内存数据库的新选择-MariaDB4J

發布時間:2024/10/8 数据库 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。