你們在開發(fā)中是否遇到過這樣的場景,需要數(shù)據(jù)存儲,但是又沒有必要部署專門的數(shù)據(jù)存儲服務(wù),這是可以使用一種內(nèi)嵌式的h2,讓我們一起來看看這個h2數(shù)據(jù)庫。
H2官網(wǎng)
http://www.h2database.com/
這個是H2數(shù)據(jù)的官網(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的依賴:
<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ù)的控制臺

默認密碼是sa,可以在控制臺進行數(shù)據(jù)庫的操作

使用建表語句創(chuàng)建一個信息info表
CREATE TABLE info
(id bigint,
name varchar);

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
做了上面的建表操作

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

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ù)仍然還在

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

利用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ù)庫

找到安裝bin目錄下的批處理文件運行

H2數(shù)據(jù)庫的服務(wù)啟動起來了,默認控制臺端口為8082

修改程序中的數(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程序測試

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ù)庫就給大家簡單介紹到這里,歡迎大家來交流,指出文中一些說錯的地方,讓我加深認識,謝謝!