SpringBoot集成H2數(shù)據(jù)庫(kù)的實(shí)現(xiàn)示例
1. 引言
Spring Boot 以其簡(jiǎn)潔的配置和快速開發(fā)能力,成為現(xiàn)代微服務(wù)架構(gòu)的首選框架之一。而H2數(shù)據(jù)庫(kù)作為一個(gè)輕量級(jí)的內(nèi)存數(shù)據(jù)庫(kù),非常適合開發(fā)階段作為嵌入式數(shù)據(jù)庫(kù)進(jìn)行單元測(cè)試和功能驗(yàn)證。本文將手把手教你如何在Spring Boot項(xiàng)目中集成H2數(shù)據(jù)庫(kù),實(shí)現(xiàn)數(shù)據(jù)的快速存取與管理。
2. H2數(shù)據(jù)庫(kù)所需依賴
<dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>compile</scope> </dependency>
3. H2數(shù)據(jù)庫(kù)application.yaml配置文件
spring:
datasource:
url: jdbc:h2:~/user
driver-class-name: org.h2.Driver
username: root
password: 123456
h2:
console:
path: /h2-console #h2嵌入式數(shù)據(jù)庫(kù)控制臺(tái)
enabled: true4. 初始化h2數(shù)據(jù)庫(kù)
注意:
schema為初始化sql路徑
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.ApplicationContext;
import org.springframework.core.io.Resource;
import org.springframework.jdbc.datasource.init.ScriptUtils;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import javax.sql.DataSource;
import java.io.File;
/**
* 初始化h2數(shù)據(jù)庫(kù)
*/
@Slf4j
@Service
// DataSource創(chuàng)建完后才初始化此類
@AutoConfigureAfter(DataSource.class)
public class H2DataSourceConfig {
//初始化sql
private static final String schema = "classpath:db/schema-h2.sql";
@Autowired
DataSource dataSource;
@Autowired
ApplicationContext applicationContext;
@PostConstruct
public void init() throws Exception {
//初始化本地?cái)?shù)據(jù)庫(kù)
String userHome = System.getProperty("user.home");//獲取系統(tǒng)用戶目錄
File f = new File(userHome + File.separator + "h2.lck");
if (!f.exists()) {
log.info("--------------初始化h2數(shù)據(jù)----------------------");
f.createNewFile();
Resource resource = applicationContext.getResource(schema);
ScriptUtils.executeSqlScript(dataSource.getConnection(), resource);
} else {
log.info("--------------h2數(shù)據(jù)庫(kù)已經(jīng)存在----------------------");
}
}
}5. 初始化sql語(yǔ)句
在resources目錄下創(chuàng)建db/schema-h2.sql文件。
DROP TABLE IF EXISTS student;
CREATE TABLE student
(
id BIGINT NOT NULL COMMENT 'id',
name VARCHAR(30) COMMENT '姓名',
age INT COMMENT '年齡',
PRIMARY KEY (id)
);
insert into student(id,name,age) values(1, 'zhangsan',23);
insert into student(id,name,age) values(2, 'lisi',45);
insert into student(id,name,age) values(3, 'wangwu',12);6. 啟動(dòng)項(xiàng)目
訪問(wèn)路徑為:http://localhost:8080/h2-console
7. 訪問(wèn)數(shù)據(jù)庫(kù)
1. 填寫登錄信息,如下圖所示:

2. 如果有student表,并且有數(shù)據(jù),說(shuō)明數(shù)據(jù)庫(kù)配置成功

到此這篇關(guān)于SpringBoot集成H2數(shù)據(jù)庫(kù)的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)SpringBoot集成H2數(shù)據(jù)庫(kù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
IDEA配置靜態(tài)資源熱加載操作(Springboot修改靜態(tài)資源不重啟)
這篇文章主要介紹了IDEA配置靜態(tài)資源熱加載操作(Springboot修改靜態(tài)資源不重啟),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-10-10
Java后臺(tái)接口開發(fā)初步實(shí)戰(zhàn)教程
下面小編就為大家分享一篇 Java后臺(tái)接口開發(fā)初步實(shí)戰(zhàn)教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01
Java將json對(duì)象轉(zhuǎn)換為map鍵值對(duì)案例詳解
這篇文章主要介紹了Java將json對(duì)象轉(zhuǎn)換為map鍵值對(duì)案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-09-09
SpringBoot前后端交互、全局異常處理之后端異常信息拋到前端顯示彈窗
Spring Boot是一個(gè)用于構(gòu)建獨(dú)立的、基于生產(chǎn)級(jí)別的Spring應(yīng)用程序的框架,下面這篇文章主要給大家介紹了關(guān)于SpringBoot前后端交互、全局異常處理之后端異常信息拋到前端顯示彈窗的相關(guān)資料,需要的朋友可以參考下2024-08-08
java集合collection接口與子接口及實(shí)現(xiàn)類
這篇文章主要介紹了java集合collection接口與子接口及實(shí)現(xiàn)類,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下2022-07-07
mybatis配置文件簡(jiǎn)介_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了mybatis配置文件簡(jiǎn)介的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09

