嵌入式數(shù)據(jù)庫H2的簡單介紹

你們在開發(fā)中是否遇到過這樣的場景,需要數(shù)據(jù)存儲,但是又沒有必要部署專門的數(shù)據(jù)存儲服務(wù),這是可以使用一種內(nèi)嵌式的h2,讓我們一起來看看這個h2數(shù)據(jù)庫。

H2官網(wǎng)

http://www.h2database.com/
這個是H2數(shù)據(jù)的官網(wǎng)

官網(wǎng)簡介

上面也說了H2數(shù)據(jù)庫的特點:
●非???,開源,JDBC API
●嵌入式和服務(wù)器模式;內(nèi)存數(shù)據(jù)庫
●基于瀏覽器的控制臺應(yīng)用程序
●占用空間?。捍蠹s1.5 MB JAR文件大小

內(nèi)嵌模式

內(nèi)嵌模式,就是把在應(yīng)用中引入H2,啟動應(yīng)用的同時,會把H2數(shù)據(jù)服務(wù)也啟動,應(yīng)用中既包含了H2數(shù)據(jù)庫的服務(wù)端,同時應(yīng)用又作為客戶端來連接H2數(shù)據(jù)庫

舉個例子

使用springBoot做一個內(nèi)嵌式H2的栗子

先創(chuàng)建一個springBoot的項目
依賴選擇H2
H2的依賴:
<dependency>
       <groupId>com.h2database</groupId>
       <artifactId>h2</artifactId>
       <scope>runtime</scope>
</dependency>
在配置文件中配置開啟H2數(shù)據(jù)的控制臺顯示
spring.datasource.platform=h2
#開啟h2數(shù)據(jù)庫console訪問
spring.h2.console.enabled=true
logging.level.root=INFO
項目啟動后,輸入http://localhost:8080/h2-console,進入H2數(shù)據(jù)的控制臺
H2數(shù)據(jù)庫的控制臺
默認密碼是sa,可以在控制臺進行數(shù)據(jù)庫的操作
H2控制臺界面
使用建表語句創(chuàng)建一個信息info表
CREATE TABLE info
(id bigint,
name varchar);
創(chuàng)建信息info表
H2內(nèi)存模式連接配置

內(nèi)存模式就是數(shù)據(jù)庫文件存在于內(nèi)存中,沒有持久化,當(dāng)應(yīng)用進程關(guān)閉時數(shù)據(jù)庫與數(shù)據(jù)表會消失,配置文件如下:

spring.datasource.platform=h2
#開啟h2數(shù)據(jù)庫console訪問
spring.h2.console.enabled=true
logging.level.root=INFO
#驅(qū)動
spring.datasource.driver-class-name=org.h2.Driver
#數(shù)據(jù)庫URL 內(nèi)存模式
spring.datasource.url=jdbc:h2:mem:hello
#spring.datasource.url=jdbc:h2:file:F:/H2/hello
#數(shù)據(jù)庫賬號密碼
spring.datasource.username=sa
spring.datasource.password=sa

spring.jpa.hibernate.ddl-auto=none
# 是否打印sql語句
spring.jpa.show-sql=true

做了上面的建表操作


內(nèi)存模式建表

當(dāng)重啟我們的應(yīng)用時,數(shù)據(jù)沒有持久化,消失了


應(yīng)用重啟后
H2嵌入模式連接配置

嵌入模式就是數(shù)據(jù)庫文件存在于應(yīng)用當(dāng)前的硬盤內(nèi),進行了持久化,當(dāng)應(yīng)用進程關(guān)閉時數(shù)據(jù)庫與數(shù)據(jù)表不會消失,配置文件如下:

spring.datasource.platform=h2
#開啟h2數(shù)據(jù)庫console訪問
spring.h2.console.enabled=true
logging.level.root=INFO
#驅(qū)動
spring.datasource.driver-class-name=org.h2.Driver
#數(shù)據(jù)庫URL
#spring.datasource.url=jdbc:h2:mem:hello
#數(shù)據(jù)庫URL嵌入模式
spring.datasource.url=jdbc:h2:file:F:/H2/hello
#數(shù)據(jù)庫賬號密碼
spring.datasource.username=sa
spring.datasource.password=sa

spring.jpa.hibernate.ddl-auto=none
# 是否打印sql語句
spring.jpa.show-sql=true

做了上面同樣的建表操作、并且添加數(shù)據(jù),應(yīng)用重啟后,數(shù)據(jù)仍然還在


應(yīng)用重啟后數(shù)據(jù)表、數(shù)據(jù)仍在

說明H2數(shù)據(jù)庫進行了持久化,應(yīng)用重啟后,數(shù)據(jù)還在,按照url上面的路勁jdbc:h2:file:F:/H2/hello去查詢了一下,果然在我的電腦上生成了持久化的文件


H2持久化的文件
利用spring data連接H2數(shù)據(jù)庫

編寫實體類

package com.ftd.h2.entity;

import javax.persistence.*;
import java.io.Serializable;

/**
 * @author ZhaoWeinan
 */
@Entity
public class Info implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    @Column
    private String name;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Info{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}

編寫Repository類

package com.ftd.h2.mapper;

import com.ftd.h2.entity.Info;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

/**
 *
 * @author ZhaoWeinan
 * @date 2018/3/14
 */
@Repository
public interface InfoRepository extends JpaRepository<Info,Long> {
}

編寫controller類

package com.ftd.h2.controlloer;

import com.ftd.h2.entity.Info;
import com.ftd.h2.mapper.InfoRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;


/**
 *
 * @author ZhaoWeinan
 * @date 2018/3/14
 */
@RestController
public class InfoController {

    @Autowired
    private InfoRepository repository;

    @RequestMapping(value = "info/{id}",method = RequestMethod.GET)
    public Info getInfoById(@PathVariable Long id){
        return repository.getOne(id);
    }
}

看一下效果:


服務(wù)器模式

服務(wù)器模式就是把H2數(shù)據(jù)庫與應(yīng)用單獨部署,應(yīng)用作為客戶端去連接H2數(shù)據(jù)庫

舉個例子

先下載安裝H2數(shù)據(jù)庫
去官網(wǎng)下載安裝
找到安裝bin目錄下的批處理文件運行
H2數(shù)據(jù)庫的服務(wù)啟動起來了,默認控制臺端口為8082
用這個服務(wù)來連接我們已經(jīng)創(chuàng)建的數(shù)據(jù)
修改程序中的數(shù)據(jù)庫連接配置

修改如下:

spring.datasource.platform=h2
#開啟h2數(shù)據(jù)庫console訪問
spring.h2.console.enabled=true
logging.level.root=INFO
#驅(qū)動
spring.datasource.driver-class-name=org.h2.Driver
#數(shù)據(jù)庫URL內(nèi)存模式
#spring.datasource.url=jdbc:h2:mem:hello
#數(shù)據(jù)庫URL嵌入模式
#spring.datasource.url=jdbc:h2:file:F:/H2/hello
#數(shù)據(jù)庫URL服務(wù)模式
spring.datasource.url=jdbc:h2:tcp://localhost//F:/H2/hello
#數(shù)據(jù)庫賬號密碼
spring.datasource.username=sa
spring.datasource.password=sa

spring.jpa.hibernate.ddl-auto=none
# 是否打印sql語句
spring.jpa.show-sql=true
spring.jackson.serialization.FAIL_ON_EMPTY_BEANS=false
利用剛才的spring boot程序測試
查詢到數(shù)據(jù)

H2總結(jié)

內(nèi)存、嵌入、服務(wù)器三種模式在開發(fā)中的不同主要體現(xiàn)在,數(shù)據(jù)庫連接URL配置這一塊

#數(shù)據(jù)庫URL內(nèi)存模式
spring.datasource.url=jdbc:h2:mem:hello
#數(shù)據(jù)庫URL嵌入模式
spring.datasource.url=jdbc:h2:file:F:/H2/hello
#數(shù)據(jù)庫URL服務(wù)模式
spring.datasource.url=jdbc:h2:tcp://localhost//F:/H2/hello

至于選用那種模式看你們開發(fā)中的具體情況而試,沒有最好的技術(shù)選型,只有更合適的技術(shù)選型

H2數(shù)據(jù)庫就給大家簡單介紹到這里,歡迎大家來交流,指出文中一些說錯的地方,讓我加深認識,謝謝!

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 47,290評論 6 342
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,715評論 19 139
  • 國家電網(wǎng)公司企業(yè)標(biāo)準(Q/GDW)- 面向?qū)ο蟮挠秒娦畔?shù)據(jù)交換協(xié)議 - 報批稿:20170802 前言: 排版 ...
    庭說閱讀 12,556評論 6 13
  • 1活動范圍:廣東省內(nèi)的會員 線下活動主題: 【身體先得在路上】 #周六夜跑深圳灣#歡迎全體深圳的會員一起開跑,預(yù)計...
    凱瑞理想生活代言人閱讀 569評論 1 3
  • 女孩醒來的時候,并不能判斷自己究竟蘇醒與否。她睜開雙眼看到的景象一如她闔上雙眼般漆黑。她的雙手雙腳不得動彈,也無法...
    莎樂_Cheryl閱讀 438評論 0 0

友情鏈接更多精彩內(nèi)容