H2數(shù)據(jù)庫(kù)入門以及實(shí)際開(kāi)發(fā)時(shí)的使用
注意:可以直接移步至【2. H2數(shù)據(jù)庫(kù)實(shí)戰(zhàn)】目錄下進(jìn)行對(duì)H2數(shù)據(jù)庫(kù)的快速使用
1. H2數(shù)據(jù)庫(kù)的初識(shí)
1.1 H2數(shù)據(jù)庫(kù)介紹
官方文檔地址:
http://www.h2database.com/html/main.html
H2的開(kāi)發(fā)始于2004年5月, 但它在2005年12月14日首次發(fā)表。 H2的原作者Thomas Mueller也是Hypersonic SQL的原始開(kāi)發(fā)者。 2001年,他加入PointBase公司。在那里他編寫了商業(yè)JavaSQL數(shù)據(jù)庫(kù)PointBaseMicro。 在這一點(diǎn)上,他不得不停止Hypersonic SQL。HSQLDB集團(tuán)成立 繼續(xù)從事Hypersonic SQL代碼庫(kù)的工作。 名稱H2代表Hypersonic 2號(hào),但H2不與Hypersonic SQL或HSQLDB共享代碼 。H2是從頭開(kāi)始構(gòu)建的。
原作者Thomas Mueller在StackOverFlow主頁(yè):https://stackoverflow.com/users/382763/thomas-mueller
1.2 為什么要使用嵌入式數(shù)據(jù)庫(kù)?
在小型的應(yīng)用程序中,例如小型掌上游戲機(jī),不適合部署高達(dá)幾百兆的數(shù)據(jù)庫(kù)服務(wù)器,同時(shí)也沒(méi)有聯(lián)網(wǎng)的必要,一種輕量級(jí)的數(shù)據(jù)庫(kù)需求由此誕生!
與常見(jiàn)的數(shù)據(jù)庫(kù)相比,嵌入式數(shù)據(jù)庫(kù)具有體積小、功能齊備、可移植性、健壯性等特點(diǎn),例如我們所熟知的 SVN 版本控制軟件就使用到了 SQLite 作為內(nèi)置數(shù)據(jù)庫(kù),SQLite 的安裝包只有不到 350 KB,在微型機(jī)中也有著廣泛的應(yīng)用,例如安卓、IOS 等移動(dòng)設(shè)備操作系統(tǒng)都內(nèi)置了 SQLite 數(shù)據(jù)庫(kù)!
鑒于嵌入式數(shù)據(jù)庫(kù)的種類比較多,有商業(yè)收費(fèi)的、也有開(kāi)源免費(fèi)的!本文主要介紹開(kāi)源免費(fèi)版的,例如 Derby、SQLite、H2、Berkeley DB 、HSQLDB 等,本文主要介紹H2數(shù)據(jù)庫(kù)的應(yīng)用,下面就一起來(lái)看看吧!
1.3 嵌入式數(shù)據(jù)庫(kù)對(duì)比
1.3.1 性能對(duì)比
在許多情況下,H2比其他(開(kāi)源和非開(kāi)源)數(shù)據(jù)庫(kù)引擎更快。請(qǐng)注意,這主要是在一臺(tái)計(jì)算機(jī)上運(yùn)行的單個(gè)連接基準(zhǔn)測(cè)試,其中有許多非常簡(jiǎn)單的針對(duì)數(shù)據(jù)庫(kù)的操作。此基準(zhǔn)測(cè)試不包括非常復(fù)雜的查詢。H2的嵌入式模式比客戶端-服務(wù)器模式更快,因?yàn)槊總€(gè)語(yǔ)句的開(kāi)銷大大減少。
援引官方文檔做出的性能測(cè)試報(bào)告和可能性分析:
Embedded:
Test Case | Unit | H2 | HSQLDB | Derby |
---|---|---|---|---|
Simple: Init | ms | 1021 | 2510 | 6762 |
Simple: Query (random) | ms | 513 | 653 | 2035 |
Simple: Query (sequential) | ms | 1344 | 2210 | 7665 |
Simple: Update (sequential) | ms | 1642 | 3040 | 7034 |
Simple: Delete (sequential) | ms | 1697 | 2310 | 9981 |
Simple: Memory Usage | MB | 18 | 15 | 13 |
BenchA: Init | ms | 801 | 2877 | 6576 |
BenchA: Transactions | ms | 1369 | 2629 | 4987 |
BenchA: Memory Usage | MB | 12 | 15 | 9 |
BenchB: Init | ms | 966 | 2544 | 7161 |
BenchB: Transactions | ms | 341 | 2316 | 815 |
BenchB: Memory Usage | MB | 14 | 10 | 10 |
BenchC: Init | ms | 2630 | 3144 | 7420 |
BenchC: Transactions | ms | 1732 | 1742 | 2735 |
BenchC: Memory Usage | MB | 19 | 34 | 11 |
Executed statements | # | 2222032 | 2222032 | 2222032 |
Total time | ms | 14056 | 25975 | 63171 |
Statements per second | #/s | 158084 | 85545 | 35174 |
Client-Server
Test Case | Unit | H2 | HSQLDB | Derby | PostgreSQL | MySQL |
---|---|---|---|---|---|---|
Simple: Init | ms | 27989 | 48055 | 47142 | 32972 | 109482 |
Simple: Query (random) | ms | 4821 | 5984 | 14741 | 4089 | 15140 |
Simple: Query (sequential) | ms | 33656 | 49112 | 95999 | 35676 | 143536 |
Simple: Update (sequential) | ms | 9878 | 23565 | 31418 | 26113 | 50676 |
Simple: Delete (sequential) | ms | 13056 | 28584 | 43955 | 20985 | 64647 |
Simple: Memory Usage | MB | 18 | 15 | 15 | 2 | 4 |
BenchA: Init | ms | 20993 | 42525 | 38335 | 27794 | 107723 |
BenchA: Transactions | ms | 16549 | 29255 | 28995 | 23113 | 65036 |
BenchA: Memory Usage | MB | 12 | 18 | 11 | 1 | 4 |
BenchB: Init | ms | 26785 | 48772 | 39756 | 32369 | 115398 |
BenchB: Transactions | ms | 898 | 10046 | 1916 | 818 | 1794 |
BenchB: Memory Usage | MB | 16 | 11 | 12 | 2 | 5 |
BenchC: Init | ms | 18266 | 26865 | 39325 | 24547 | 70531 |
BenchC: Transactions | ms | 6569 | 7783 | 9412 | 8916 | 19150 |
BenchC: Memory Usage | MB | 17 | 35 | 13 | 2 | 7 |
Executed statements | # | 2222032 | 2222032 | 2222032 | 2222032 | 2222032 |
Total time | ms | 179460 | 320546 | 390994 | 237392 | 763113 |
Statements per second | #/s | 12381 | 6932 | 5683 | 9360 | 2911 |
H2
Version 2.0.202 (2021-11-25) was used for the test. For most operations, the performance of H2 is about the same as for HSQLDB. One situation where H2 is slow is large result sets, because they are buffered to disk if more than a certain number of records are returned. The advantage of buffering is: there is no limit on the result set size.
測(cè)試使用了版本2.0.202(2021年11月25日)。對(duì)于大多數(shù)操作,H2的性能與HSQLDB大致相同。H2比較慢的一種情況是結(jié)果集比較大,因?yàn)槿绻祷氐挠涗洺^(guò)一定數(shù)量,結(jié)果集就會(huì)被緩沖到磁盤。緩沖的優(yōu)點(diǎn)是:對(duì)結(jié)果集大小沒(méi)有限制。
HSQLDB
Version 2.5.1 was used for the test. Cached tables are used in this test (hsqldb.default_table_type=cached), and the write delay is 1 second (SET WRITE_DELAY 1).
測(cè)試使用了版本2.5.1。此測(cè)試中使用緩存表(hsqldb.default_table_type=cached),寫入延遲為1秒(SET WRITE_DELAY 1)。
Derby
Version 10.14.2.0 was used for the test. Derby is clearly the slowest embedded database in this test. This seems to be a structural problem, because all operations are really slow. It will be hard for the developers of Derby to improve the performance to a reasonable level. A few problems have been identified: leaving autocommit on is a problem for Derby. If it is switched off during the whole test, the results are about 20% better for Derby. Derby calls FileChannel.force(false), but only twice per log file (not on each commit). Disabling this call improves performance for Derby by about 2%. Unlike H2, Derby does not call FileDescriptor.sync() on each checkpoint. Derby supports a testing mode (system property derby.system.durability=test) where durability is disabled. According to the documentation, this setting should be used for testing only, as the database may not recover after a crash. Enabling this setting improves performance by a factor of 2.6 (embedded mode) or 1.4 (server mode). Even if enabled, Derby is still less than half as fast as H2 in default mode.
版本10.14.2.0用于測(cè)試。Derby顯然是這個(gè)測(cè)試中最慢的嵌入式數(shù)據(jù)庫(kù)。這似乎是一個(gè)結(jié)構(gòu)性的問(wèn)題,因?yàn)樗械牟僮鞫己苈erby的開(kāi)發(fā)人員將很難將性能提高到合理的水平。已經(jīng)查明了一些問(wèn)題:保持自動(dòng)提交是Derby的一個(gè)問(wèn)題。如果在整個(gè)測(cè)試過(guò)程中關(guān)閉它,結(jié)果對(duì)德比來(lái)說(shuō)要好20%左右。Derby調(diào)用FileChannel.force(false),但每個(gè)日志文件只調(diào)用兩次(而不是在每次提交時(shí))。禁用此調(diào)用可以將Derby的性能提高大約2%。與H2不同,Derby不在每個(gè)檢查點(diǎn)調(diào)用FileDescriptor.sync()。Derby支持禁用耐久性的測(cè)試模式(系統(tǒng)屬性derby.system.durability=test)。根據(jù)文檔,此設(shè)置應(yīng)僅用于測(cè)試,因?yàn)閿?shù)據(jù)庫(kù)在崩潰后可能無(wú)法恢復(fù)。啟用此設(shè)置可將性能提高2.6倍(嵌入式模式)或1.4倍(服務(wù)器模式)。即使啟用,Derby在默認(rèn)模式下的速度仍不到H2的一半。
PostgreSQL
Version 13.4 was used for the test. The following options where changed in postgresql.conf: fsync = off, commit_delay = 100000 (microseconds). PostgreSQL is run in server mode. The memory usage number is incorrect, because only the memory usage of the JDBC driver is measured.
測(cè)試使用了版本13.4。postgresql.conf中更改了以下選項(xiàng):fsync =關(guān)閉,提交延遲= 100000(微秒)。PostgreSQL在服務(wù)器模式下運(yùn)行。內(nèi)存使用數(shù)量不正確,因?yàn)橹粶y(cè)量JDBC驅(qū)動(dòng)程序的內(nèi)存使用。
MySQL
Version 8.0.27 was used for the test. MySQL was run with the InnoDB backend. The setting innodb_flush_log_at_trx_commit and sync_binlogcode> (found in the my.ini / community-mysql-server.cnf file) was set to 0. Otherwise (and by default), MySQL is slow (around 140 statements per second in this test) because it tries to flush the data to disk for each commit. For small transactions (when autocommit is on) this is really slow. But many use cases use small or relatively small transactions. Too bad this setting is not listed in the configuration wizard, and it always overwritten when using the wizard. You need to change those settings manually in the file my.ini / community-mysql-server.cnf, and then restart the service. The memory usage number is incorrect, because only the memory usage of the JDBC driver is measured.
測(cè)試使用了版本8.0.27。MySQL與InnoDB后端一起運(yùn)行。設(shè)置innodb_flush_log_at_trx_commit和sync_binlogcode〉(可在my.ini/community-mysql-server. cnf文件中找到)被設(shè)置為0。否則(默認(rèn)情況下),MySQL會(huì)很慢(在本測(cè)試中大約每秒140條語(yǔ)句),因?yàn)樗鼑L試在每次提交時(shí)將數(shù)據(jù)刷新到磁盤。對(duì)于小事務(wù)(當(dāng)自動(dòng)提交打開(kāi)時(shí)),這確實(shí)很慢。但許多用例使用的是小型或相對(duì)小型的事務(wù)。糟糕的是,配置向?qū)е袥](méi)有列出此設(shè)置,而且在使用向?qū)r(shí)總是覆蓋它。您需要在文件my.ini/community-mysql-server.cnf中手動(dòng)更改這些設(shè)置,然后重新啟動(dòng)服務(wù)。內(nèi)存使用數(shù)量不正確,因?yàn)橹粶y(cè)量JDBC驅(qū)動(dòng)程序的內(nèi)存使用。
SQLite
SQLite 3.36.0.3, configured to use WAL and with synchronous=NORMAL was tested in a separate, less reliable run. A rough estimate is that SQLite performs approximately 2-5x worse in the simple benchmarks, which perform simple work in the database, resulting in a low work-per-transaction ratio. SQLite becomes competitive as the complexity of the database interactions increases. The results seemed to vary drastically across machine, and more reliable results should be obtained. Benchmark on your production hardware.
SQLite3.36.0.3,配置為使用WAL,并且synchronous=NORMAL,在一個(gè)單獨(dú)的、不太可靠的運(yùn)行中進(jìn)行了測(cè)試。粗略估計(jì),SQLite在簡(jiǎn)單基準(zhǔn)測(cè)試中的性能要差2- 5倍,因?yàn)檫@些基準(zhǔn)測(cè)試在數(shù)據(jù)庫(kù)中執(zhí)行簡(jiǎn)單的工作,導(dǎo)致每事務(wù)工作量比率較低。隨著數(shù)據(jù)庫(kù)交互復(fù)雜性的增加,SQLite變得越來(lái)越有競(jìng)爭(zhēng)力。不同機(jī)器的結(jié)果似乎差異很大,應(yīng)獲得更可靠的結(jié)果。在您的生產(chǎn)硬件上進(jìn)行基準(zhǔn)測(cè)試。
The benchmarks used include multi-threaded scenarios, and we were not able to get the SQLite JDBC driver we used to work with them. Help with configuring the driver for multi-threaded usage is welcome.
使用的基準(zhǔn)測(cè)試包括多線程場(chǎng)景,我們無(wú)法獲得用于處理它們的SQLite JDBC驅(qū)動(dòng)程序。歡迎提供有關(guān)配置驅(qū)動(dòng)程序以用于多線程的幫助。
Firebird
Firebird 3.0 (default installation) was tested, but failed on multi-threaded part of the test. It is likely possible to run the performance test with the Firebird database, and any information on how to configure Firebird for this are welcome.
測(cè)試了Firebird 3.0(默認(rèn)安裝),但在測(cè)試的多線程部分失敗。很可能可以使用Firebird數(shù)據(jù)庫(kù)運(yùn)行性能測(cè)試,歡迎提供有關(guān)如何為此配置Firebird的任何信息。
Why Oracle / MS SQL Server / DB2 are Not Listed
The license of these databases does not allow to publish benchmark results. This doesn’t mean that they are fast. They are in fact quite slow, and need a lot of memory. But you will need to test this yourself.
這些數(shù)據(jù)庫(kù)的許可證不允許發(fā)布基準(zhǔn)測(cè)試結(jié)果。但這并不意味著他們的速度很快。它們實(shí)際上相當(dāng)慢,并且需要大量?jī)?nèi)存。但你需要自己測(cè)試一下。
1.4 技術(shù)選型思考
從官網(wǎng)測(cè)試性能分析中可以發(fā)現(xiàn),基于java的嵌入式數(shù)據(jù)庫(kù)針對(duì)于內(nèi)存來(lái)說(shuō)總體H2數(shù)據(jù)庫(kù)速度較快,運(yùn)行效率較高。其中復(fù)雜數(shù)據(jù)庫(kù)交互復(fù)雜度上升,可以進(jìn)行考慮SQLite。H2針對(duì)于輕量化,和小數(shù)據(jù)的保存和查詢比其他數(shù)據(jù)庫(kù)有一定優(yōu)勢(shì)。詳情見(jiàn)【1.3.1 性能對(duì)比 】
2. H2數(shù)據(jù)庫(kù)實(shí)戰(zhàn)
2.1 H2數(shù)據(jù)庫(kù)下載搭建以及部署
2.1.1 H2數(shù)據(jù)庫(kù)的下載
數(shù)據(jù)庫(kù)下載:http://www.h2database.com/html/download.html
h2
|—bin
| |—h2-2.1.214.jar //H2數(shù)據(jù)庫(kù)的jar包(驅(qū)動(dòng)也在里面)
| |—h2.bat //Windows控制臺(tái)啟動(dòng)腳本
| |—h2.sh //Linux控制臺(tái)啟動(dòng)腳本
| |—h2w.bat //Windows控制臺(tái)啟動(dòng)腳本(不帶黑屏窗口)
|—docs //H2數(shù)據(jù)庫(kù)的幫助文檔(內(nèi)有H2數(shù)據(jù)庫(kù)的使用手冊(cè))
|—service //通過(guò)wrapper包裝成服務(wù)
|—src //H2數(shù)據(jù)庫(kù)的源代碼
|—build.bat //windows構(gòu)建腳本
|—build.sh //linux構(gòu)建腳本
2.1.2 數(shù)據(jù)庫(kù)啟動(dòng)
注意:
在file模式下只支持一個(gè)進(jìn)程連接,如果有多個(gè)進(jìn)程需要連接同一個(gè)url可以用Automatic Mixed Mode模式,關(guān)鍵字AUTO_SERVER=TRUE
進(jìn)行處理通過(guò)AUTO_SERVER_PORT=9090
進(jìn)行指定端口。當(dāng)然此方法不適用于內(nèi)存數(shù)據(jù)庫(kù)即:jdbc:h2:mem:test_mem
此類連接模式。
2.1.2.1 windows系統(tǒng)可以在bin目錄下執(zhí)行h2.bat
上圖為執(zhí)行的批處理文件詳情
2.1.2.2 同理可以通過(guò)cmd直接使用命令進(jìn)行啟動(dòng):
啟動(dòng)命令:
java -cp h2-2.1.214.jar org.h2.tools.Server
2.1.2.3 啟動(dòng)后控制臺(tái)頁(yè)面:
注意:
第一次登陸可以用sa進(jìn)行,密碼可以不填。使用其他用戶名登陸會(huì)自動(dòng)創(chuàng)建用戶,如果指定密碼則下次登陸需要輸入指定后的密碼。
登陸后可以點(diǎn)擊快速創(chuàng)建數(shù)據(jù)庫(kù)腳本進(jìn)行demo:
2.1.3 spring整合H2數(shù)據(jù)庫(kù)
2.1.3.1 引入依賴文件
<!-- H2 -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.200</version>
<scope>runtime</scope>
</dependency>
對(duì)應(yīng)yml文件如下:
server:
port: 8081
servlet:
context-path: /test
spring:
datasource:
#tcp配置
# url: jdbc:h2:tcp://127.0.0.1:9092/D:/db/medicine;MODE=MYSQL
# CASE_INSENSITIVE_IDENTIFIERS=TRUE;不進(jìn)行區(qū)分大小寫配置,可以看數(shù)據(jù)庫(kù)需要選擇配置
url: jdbc:h2:file:./db/medicine;MODE=MYSQL;
driver-class-name: org.h2.Driver
username: lijiaheng
password: 123123
schema: classpath:databaseCreation
h2:
console:
# 開(kāi)啟web控制臺(tái)
enabled: true
# 訪問(wèn)路徑url+/h2
path: /h2
settings:
web-allow-others: true
# mybatis-plus 配置
mybatis-plus:
mapper-locations: classpath*:/mapper/*.xml
#實(shí)體掃描,多個(gè)package用逗號(hào)或者分號(hào)分隔
typeAliasesPackage: com.test.eida.model
global-config:
#數(shù)據(jù)庫(kù)相關(guān)配置
db-config:
#主鍵類型 AUTO:"數(shù)據(jù)庫(kù)ID自增", INPUT:"用戶輸入ID", ID_WORKER:"全局唯一ID (數(shù)字類型唯一ID)", UUID:"全局唯一ID UUID";
id-type: AUTO
#字段策略 IGNORED:"忽略判斷",NOT_NULL:"非 NULL 判斷"),NOT_EMPTY:"非空判斷"
field-strategy: NOT_NULL
#駝峰下劃線轉(zhuǎn)換
column-underline: false
logic-delete-value: -1
logic-not-delete-value: 0
banner: false
2.1.4 數(shù)據(jù)庫(kù)通過(guò)file模式實(shí)際保存數(shù)據(jù)的位置
1.在指定本地路徑后即可在當(dāng)前指定的目錄下構(gòu)建medicine.mv.db文件,進(jìn)行數(shù)據(jù)的保存。
2.medicine.trace.db此文件打開(kāi)后可以看到通過(guò)file模式執(zhí)行的sql信息和錯(cuò)誤日志。
3.下文會(huì)詳細(xì)介紹H2的配置
2.2 H2數(shù)據(jù)庫(kù)操作
2.2.1 Mysql兼容模式
當(dāng)需要不區(qū)分大小寫的標(biāo)識(shí)符時(shí),將CASE_INSENSITIVE_IDENTIFIERS = TRUE附加到URL,創(chuàng)建數(shù)據(jù)庫(kù)后不要更改DATABASE_TO_LOWER的值
示例:
jdbc:h2:~/test;MODE=MySQL;DATABASE_TO_LOWER=TRUE
- Creating indexes in the CREATE TABLE statement is allowed using
INDEX(…) or KEY(…). 創(chuàng)建表時(shí)允許使用 INDEX(…)或者 KEY(…)創(chuàng)建索引或者唯一鍵。create table test(id int primary key, name varchar(255), key idx_name(name));
- 將浮點(diǎn)數(shù)轉(zhuǎn)換為整數(shù)時(shí),小數(shù) 數(shù)字不被截?cái)啵当簧崛搿?/li>
- ON DUPLICATE KEY UPDATE在INSERT語(yǔ)句中是受支持的。
- INSERT IGNORE 部分支持,如果未指定 ON DUPLICATE KEY UPDATE,則可用于跳過(guò)具有重復(fù)鍵的行。
- 從CHAR值的右側(cè)刪除空格。
- REGEXP_REPLACE()使用\進(jìn)行反向引用。
- 日期時(shí)間值函數(shù)在命令中返回相同的值。0x文字被分析為二進(jìn)制字符串文字。
- DISTINCT查詢的ORDER BY子句中允許使用不相關(guān)的表達(dá)式。
- 部分支持某些特定于MySQL的ALTER TABLE命令。
- TRUNCATE TABLE重新啟動(dòng)生成列的下一個(gè)值。
- 如果手動(dòng)指定標(biāo)識(shí)列的值,則其序列將更新為在以下時(shí)間之后生成值插入。
- NULL值的工作原理與DEFAULT值一樣,都是對(duì)標(biāo)識(shí)列的賦值。
- 引用約束不需要被引用列上的現(xiàn)有主鍵或唯一約束 并且如果這樣的約束不存在則自動(dòng)創(chuàng)建唯一約束。
- 支持LIMIT / OFFSET子句。
- 可以使用AUTO_INCREMENT子句。
- YEAR數(shù)據(jù)類型被視為SMALLINT數(shù)據(jù)類型。
- GROUP BY子句可以包含SELECT列表中表達(dá)式的從1開(kāi)始的位置。
- 允許在數(shù)值和布爾值之間使用不安全的比較運(yùn)算符。
- 默認(rèn)情況下,MySQL中的文本比較不區(qū)分大小寫,而H2中的文本比較區(qū)分大小寫(與大多數(shù)其他數(shù)據(jù)庫(kù)一樣)。 H2確實(shí)支持不區(qū)分大小寫的文本比較,但需要單獨(dú)設(shè)置,使用設(shè)置忽略實(shí)例SET IGNORECASE TRUE 。這會(huì)影響使用=、LIKE、REGEXP進(jìn)行的比較。
2.2.2 Mysql模式的使用
sql示例:
CREATE TABLE IF NOT EXISTS medicine_goods
(
id bigint auto_increment PRIMARY KEY COMMENT '主鍵',
GoodsName VARCHAR(100) DEFAULT NULL COMMENT '商品名稱',
GoodsID VARCHAR(100) not NULL COMMENT '商品編碼',
UNIQUE KEY UNIQUE_KEY_GOODSID (GoodsID)
);
COMMENT ON TABLE PUBLIC.MEDICINE_GOODS IS '藥品表';
注意:
由于對(duì)于alter table支持的不夠完善,建議創(chuàng)建時(shí)進(jìn)行多次實(shí)驗(yàn)。
配置可以通過(guò)mybatis-plus進(jìn)行相關(guān)開(kāi)發(fā)。當(dāng)然由于部分H2數(shù)據(jù)庫(kù)語(yǔ)法mybatis-plus未能很好的適配映射,因此更推薦直接在xml中寫相對(duì)復(fù)雜的sql語(yǔ)句。
2.3 H2數(shù)據(jù)庫(kù)URL詳細(xì)配置以及使用
Topic | URL Format and Examples |
---|---|
Embedded (local) connection 嵌入式本地連接 |
jdbc:h2:[file:][
] jdbc:h2:~/test jdbc:h2:file:/data/sample jdbc:h2:file:C:/data/sample (Windows only) |
In-memory (private) 內(nèi)存連接方式 |
jdbc:h2:mem: |
In-memory (named) 內(nèi)存連接方式(已命名) |
jdbc:h2:mem: jdbc:h2:mem:test_mem |
Server mode (remote connections) using TCP/IP 使用TCP/IP進(jìn)行遠(yuǎn)程連接 |
jdbc:h2:tcp://[:]/[
] jdbc:h2:tcp://localhost/~/test jdbc:h2:tcp://dbserv:8084/~/sample jdbc:h2:tcp://localhost/mem:test |
Server mode (remote connections) using TLS 使用TLS遠(yuǎn)程加密連接 |
jdbc:h2:ssl://[:]/[
] jdbc:h2:ssl://localhost:8085/~/sample; |
Using encrypted files 使用加密文件 |
jdbc:h2:;CIPHER=AES jdbc:h2:ssl://localhost/~/test;CIPHER=AES jdbc:h2:file:~/secure;CIPHER=AES |
File locking methods 文件鎖定 |
jdbc:h2:;FILE_LOCK={FILE|SOCKET|FS|NO} jdbc:h2:file:~/private;CIPHER=AES;FILE_LOCK=SOCKET |
Only open if it already exists 僅在存在時(shí)打開(kāi) |
jdbc:h2:;IFEXISTS=TRUE jdbc:h2:file:~/sample;IFEXISTS=TRUE |
Don’t close the database when the VM exits VM退出時(shí)不關(guān)閉數(shù)據(jù)庫(kù) |
jdbc:h2:;DB_CLOSE_ON_EXIT=FALSE |
Execute SQL on connection 連接時(shí)執(zhí)行SQL |
jdbc:h2:;INIT=RUNSCRIPT FROM ‘~/create.sql’ jdbc:h2:file:~/sample;INIT=RUNSCRIPT FROM ‘~/create.sql’;RUNSCRIPT FROM ‘~/populate.sql’ |
User name and/or password 使用用戶名或密碼通過(guò)url登陸數(shù)據(jù)庫(kù) |
jdbc:h2:[;USER=][;PASSWORD=] jdbc:h2:file:~/sample;USER=sa;PASSWORD=123 |
Debug trace settings 調(diào)試跟蹤 |
jdbc:h2:;TRACE_LEVEL_FILE=<level 0…3> jdbc:h2:file:~/sample;TRACE_LEVEL_FILE=3 |
Ignore unknown settings 忽略未知設(shè)置 |
jdbc:h2:;IGNORE_UNKNOWN_SETTINGS=TRUE |
Custom file access mode 自定義文件訪問(wèn)模式 |
jdbc:h2:;ACCESS_MODE_DATA=rws |
Database in a zip file 壓縮文件中的數(shù)據(jù)庫(kù) |
jdbc:h2:zip:!/ jdbc:h2:zip:~/db.zip!/test |
Compatibility mode 兼容模式 |
jdbc:h2:;MODE= jdbc:h2:~/test;MODE=MYSQL;DATABASE_TO_LOWER=TRUE |
Auto-reconnect 自動(dòng)重新連接 |
jdbc:h2:;AUTO_RECONNECT=TRUE jdbc:h2:tcp://localhost/~/test;AUTO_RECONNECT=TRUE |
Automatic mixed mode 自動(dòng)混合模式 |
jdbc:h2:;AUTO_SERVER=TRUE jdbc:h2:~/test;AUTO_SERVER=TRUE |
Page size 頁(yè)面大小 |
jdbc:h2:;PAGE_SIZE=512 |
Changing other settings 更改其他設(shè)置 |
jdbc:h2:;=[;=…] jdbc:h2:file:~/sample;TRACE_LEVEL_SYSTEM_OUT=3 |
以上均可通過(guò)連接在官方文檔中定位相關(guān)模式詳情,在此不再一一贅述。
3. H2數(shù)據(jù)庫(kù)開(kāi)發(fā)使用時(shí)注意問(wèn)題和細(xì)節(jié)
3.1 逆向工程細(xì)節(jié)和踩過(guò)的坑
3.1.1 mybatis-plus-generator
如果使用mybatis-plus-generator建議使用最新版本,3.4.0版本會(huì)因?yàn)閳?zhí)行唯一主鍵sql找不到對(duì)應(yīng)表的Extra
(mysql中有)字段,因此會(huì)生成不出來(lái)對(duì)應(yīng)實(shí)體類屬性,在拉取最新版本后可以看到查詢語(yǔ)句已經(jīng)修改。
3.4.0逆向工程代碼H2查詢主鍵sql:
select * from INFORMATION_SCHEMA.INDEXES WHERE TABLE_NAME = '%s'
如果不用最新的直接修改把查詢類H2Query
中方法isKeyIdentity
修改直接返回true即可解決問(wèn)題,因?yàn)橥ㄟ^(guò)PRIMARY_KEY已經(jīng)可以找到對(duì)應(yīng)主鍵。
3.1.2 使用H2原生提供的數(shù)據(jù)庫(kù)初始化方法
Execute SQL on connection 連接時(shí)執(zhí)行SQL
jdbc:h2:;INIT=RUNSCRIPT FROM ‘~/create.sql’
jdbc:h2:file:~/sample;INIT=RUNSCRIPT FROM ‘~/create.sql’;RUNSCRIPT FROM ‘~/populate.sql’
在實(shí)際使用中放置于classpath目錄下總是識(shí)別不到,無(wú)論配置相對(duì)路徑和絕對(duì)路徑都無(wú)法識(shí)別,包括修改最新的H2版本。通過(guò)搜索該問(wèn)題
https://stackoverflow.com/questions/4490138/problem-with-init-runscript-and-relative-paths?noredirect=1&lq=1
發(fā)現(xiàn)原作者Thomas Mueller也未能復(fù)現(xiàn),并且此問(wèn)題未被解決,因此通過(guò)spring的掃描進(jìn)行處理了數(shù)據(jù)腳本初始化的問(wèn)題。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-646519.html
spring:
datasource:
#tcp配置
# url: jdbc:h2:tcp://127.0.0.1:9092/D:/db/medicine;MODE=MYSQL
# CASE_INSENSITIVE_IDENTIFIERS=TRUE;不進(jìn)行區(qū)分大小寫配置,可以看數(shù)據(jù)庫(kù)需要選擇配置
url: jdbc:h2:file:./db/medicine;MODE=MYSQL;
driver-class-name: org.h2.Driver
username: lijiaheng
password: 123123
schema: classpath:databaseCreation
4. 總結(jié)
在使用H2數(shù)據(jù)庫(kù)過(guò)程中,發(fā)現(xiàn)有部分語(yǔ)法雖然兼容了Mysql但還有些不太一樣,例如在創(chuàng)建唯一鍵的時(shí)候,或者使用alter table 時(shí)部分語(yǔ)法不支持,因此在使用過(guò)程中應(yīng)當(dāng)多進(jìn)行實(shí)驗(yàn),可以通過(guò)官網(wǎng)找到錯(cuò)誤語(yǔ)法標(biāo)簽查看并進(jìn)行修改。
當(dāng)然還有很多的坑因?yàn)闃I(yè)務(wù)的簡(jiǎn)單未遇到,但是總的來(lái)說(shuō)通過(guò)stackoverflow基本能解決大部分問(wèn)題。在國(guó)內(nèi)H2數(shù)據(jù)并不是大眾化的數(shù)據(jù),因此生態(tài)會(huì)差一些,這也是我想寫這篇文章的初衷。
以上就是H2數(shù)據(jù)庫(kù)配置及相關(guān)使用方式一站式介紹,歡迎大家多多點(diǎn)贊,收藏,分享~文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-646519.html
到了這里,關(guān)于H2數(shù)據(jù)庫(kù)配置及相關(guān)使用方式一站式介紹(極為詳細(xì)并整理官方文檔)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!