Spring boot2.x中集成H2數(shù)據(jù)庫代碼實(shí)例
這篇文章主要介紹了Spring boot2.x中集成H2數(shù)據(jù)庫代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
在spring boot中集成
1.添加依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency>
2.添加H2相關(guān)配置,修改application.properties文件
spring.jpa.database=h2 spring.jpa.show-sql=true #ddl執(zhí)行方式,update create 等 spring.datasource.url=jdbc:h2:./data/test;AUTO_SERVER=TRUE spring.jpa.hibernate.ddl-auto=update spring.datasource.username=sa spring.datasource.password=123456 spring.datasource.driverClassName=org.h2.Driver spring.h2.console.path=/h2-console spring.h2.console.enabled=true
說明:
spring.datasource.url
數(shù)據(jù)庫文件
(1)內(nèi)存數(shù)據(jù)庫
jdbc:h2:mem:DBName
內(nèi)存數(shù)據(jù)庫的數(shù)據(jù)存在內(nèi)存中,當(dāng)程序停止時(shí),不會被保存會丟失
eg:
spring.datasource.url=jdbc:h2:mem:test
(2)文件數(shù)據(jù)庫
jdbc:h2:file:{FilePath} 也可以簡化為 jdbc:h2:{FilePath}
FilePath的格式
- a) ./{path}/{fileName} 在當(dāng)前程序的根目錄下創(chuàng)建目錄和數(shù)據(jù)庫文件
- b) ~/{path}/{fileName} 在當(dāng)前用戶的根目錄下創(chuàng)建目錄和數(shù)據(jù)庫文件
- c) C:/{path}/{fileName} 在指定盤符的指定目錄下創(chuàng)建數(shù)據(jù)庫文件
(3)遠(yuǎn)程數(shù)據(jù)庫
jdbc:h2:tcp://<{IP|hostname}>[:{Port}]/[]<{dbName}>
附加參數(shù):
- AUTO_SERVER=TRUE 啟動自動混合模式,允許開啟多個(gè)連接,該參數(shù)不支持在內(nèi)存中運(yùn)行模式
- DB_CLOSE_ON_EXIT=FALSE,當(dāng)虛擬機(jī)退出時(shí)并不關(guān)閉數(shù)據(jù)庫
3.代碼
domain層,即User類(entity)
package com.example.demo.domain; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import javax.persistence.*; @Entity @Table(name = "user") @Data public class User { @Id @GeneratedValue(strategy= GenerationType.AUTO) private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
dao層,即UserRepository 接口
package com.example.demo.dao; import com.example.demo.domain.User; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; import java.util.List; @Repository public interface UserRepository extends JpaRepository<User,Integer> { List<User> getUsersByName(String Name); }
controller層,即Demo
package com.example.demo.controller; import com.example.demo.dao.UserRepository; import com.example.demo.domain.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController public class Demo { @Autowired private UserRepository repo; @RequestMapping("find") public List<User> find() { return (List<User>) repo.findAll(); } }
編寫DemoApplication
package com.example.demo; import com.example.demo.dao.UserRepository; import com.example.demo.domain.User; import org.springframework.beans.factory.InitializingBean; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; @SpringBootApplication public class DemoApplication { @Bean InitializingBean saveData(UserRepository repo){ return ()->{ User u = new User(); u.setName("abc"); repo.save(u); User u1 = new User(); u1.setName("zyx"); repo.save(u1); }; } public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
啟動項(xiàng)目,打開瀏覽器訪問http://localhost:8080/find
訪問http://localhost:8080/h2-console/
連接上后查詢數(shù)據(jù)
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- springboot整合H2內(nèi)存數(shù)據(jù)庫實(shí)現(xiàn)單元測試與數(shù)據(jù)庫無關(guān)性
- springboot配置內(nèi)存數(shù)據(jù)庫H2教程詳解
- springboot集成springsecurity 使用OAUTH2做權(quán)限管理的教程
- h2database在springboot中的使用教程
- Spring-boot oauth2使用RestTemplate進(jìn)行后臺自動登錄的實(shí)現(xiàn)
- 基于SpringBoot整合oauth2實(shí)現(xiàn)token認(rèn)證
- SpringBoot配置web訪問H2的方法
- springboot2.x實(shí)現(xiàn)oauth2授權(quán)碼登陸的方法
- SpringBoot配置使用H2數(shù)據(jù)庫的簡單教程
相關(guān)文章
SpringBoot+MDC實(shí)現(xiàn)鏈路調(diào)用日志的方法
MDC是 log4j 、logback及l(fā)og4j2 提供的一種方便在多線程條件下記錄日志的功能,這篇文章主要介紹了SpringBoot+MDC實(shí)現(xiàn)鏈路調(diào)用日志,需要的朋友可以參考下2022-12-12使用Netty實(shí)現(xiàn)類似Dubbo的遠(yuǎn)程接口調(diào)用的實(shí)現(xiàn)方法
本文介紹了如何使用Netty框架實(shí)現(xiàn)類似Dubbo的遠(yuǎn)程接口調(diào)用,通過自定義編解碼器、通信協(xié)議和服務(wù)注冊中心等實(shí)現(xiàn)遠(yuǎn)程通信和服務(wù)治理。文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-04-04聊聊BeanUtils.copyProperties和clone()方法的區(qū)別
這篇文章主要介紹了聊聊BeanUtils.copyProperties和clone()方法的區(qū)別,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09Java面向?qū)ο蠡A(chǔ)之多態(tài)性,抽象類和接口
這篇文章主要介紹了Java面向?qū)ο蠡A(chǔ):多態(tài)性,抽象類和接口,文中代碼可以幫助各位更好的理解學(xué)習(xí),有需求的小伙伴可以參考下2020-05-05Jersey Restful接口如何獲取參數(shù)的問題
這篇文章主要介紹了Jersey Restful接口如何獲取參數(shù)的操作,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06