1. 程式人生 > 實用技巧 >H2資料庫

H2資料庫

H2資料庫

  • H2是一個Java編寫的關係型資料庫,它可以被嵌入Java應用程式中使用,或者作為一個單獨的資料庫伺服器執行。
  • 純Java編寫,不受平臺的限制;
  • 只有一個jar檔案,適合作為嵌入式資料庫使用;
  • h2提供了一個十分方便的web控制檯用於操作和管理資料庫內容;
  • 功能完整,支援標準SQL和JDBC。麻雀雖小五臟俱全;
  • 支援內嵌模式、伺服器模式和叢集。
  • 下載安裝可以去官網下載:官網連線:官網連結

連線

  1. 下載後解壓開啟
  2. 輸入使用者名稱密碼
  3. 出現這個就是連線成功,工作列會出現黃色的圖示:

H2資料庫單元測試:

  1. PDM生成SQL
  2. 資料匯入
  3. SQL匯出
  4. 建testresources目錄,匯入SQL:先從單元測試的目錄檔案資源去找資料庫連線檔案 否則會回去找resources目錄下的資料庫連線檔案
  5. 配置jdbc(修改drver,url):URL後面要加 jdbc:h2:~/test;AUTO_SERVER=TRUE
  6. AUTO_SERVER =TRUE 多個程序可以訪問同一個資料庫,而不必手動啟動伺服器。無論資料庫是否已經開啟,您都可以使用相同的資料庫網址。

配置

<!-- https://mvnrepository.com/artifact/com.h2database/h2 -->
    <dependency>
      <groupId>com.h2database</groupId>
      <artifactId>h2</artifactId>
      <version>1.4.200</version>
      <scope>test</scope>
 </dependency>
匯入Junit5
<dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter-engine</artifactId>
      <version>5.5.2</version>
      <scope>test</scope>
</dependency>
<dependency>
      <groupId>org.junit.platform</groupId>
      <artifactId>junit-platform-runner</artifactId>
      <version>1.5.1</version>
</dependency>