SpringBoot集成H2數(shù)據(jù)庫的實現(xiàn)示例
1. 引言
Spring Boot 以其簡潔的配置和快速開發(fā)能力,成為現(xiàn)代微服務(wù)架構(gòu)的首選框架之一。而H2數(shù)據(jù)庫作為一個輕量級的內(nèi)存數(shù)據(jù)庫,非常適合開發(fā)階段作為嵌入式數(shù)據(jù)庫進行單元測試和功能驗證。本文將手把手教你如何在Spring Boot項目中集成H2數(shù)據(jù)庫,實現(xiàn)數(shù)據(jù)的快速存取與管理。
2. H2數(shù)據(jù)庫所需依賴
<dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>compile</scope> </dependency>
3. H2數(shù)據(jù)庫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ù)庫控制臺 enabled: true
4. 初始化h2數(shù)據(jù)庫
注意:
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ù)庫 */ @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 { //初始化本地數(shù)據(jù)庫 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ù)庫已經(jīng)存在----------------------"); } } }
5. 初始化sql語句
在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. 啟動項目
訪問路徑為:http://localhost:8080/h2-console
7. 訪問數(shù)據(jù)庫
1. 填寫登錄信息,如下圖所示:
2. 如果有student表,并且有數(shù)據(jù),說明數(shù)據(jù)庫配置成功
到此這篇關(guān)于SpringBoot集成H2數(shù)據(jù)庫的實現(xiàn)示例的文章就介紹到這了,更多相關(guān)SpringBoot集成H2數(shù)據(jù)庫內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
IDEA配置靜態(tài)資源熱加載操作(Springboot修改靜態(tài)資源不重啟)
這篇文章主要介紹了IDEA配置靜態(tài)資源熱加載操作(Springboot修改靜態(tài)資源不重啟),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10Java將json對象轉(zhuǎn)換為map鍵值對案例詳解
這篇文章主要介紹了Java將json對象轉(zhuǎn)換為map鍵值對案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-09-09SpringBoot前后端交互、全局異常處理之后端異常信息拋到前端顯示彈窗
Spring Boot是一個用于構(gòu)建獨立的、基于生產(chǎn)級別的Spring應(yīng)用程序的框架,下面這篇文章主要給大家介紹了關(guān)于SpringBoot前后端交互、全局異常處理之后端異常信息拋到前端顯示彈窗的相關(guān)資料,需要的朋友可以參考下2024-08-08java集合collection接口與子接口及實現(xiàn)類
這篇文章主要介紹了java集合collection接口與子接口及實現(xiàn)類,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的朋友可以參考一下2022-07-07mybatis配置文件簡介_動力節(jié)點Java學(xué)院整理
這篇文章主要為大家詳細介紹了mybatis配置文件簡介的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-09-09