SpringBoot集成H2內(nèi)存數(shù)據(jù)庫的方法
H2是Thomas Mueller提供的一個開源的、純java實(shí)現(xiàn)的關(guān)系數(shù)據(jù)庫。
前言
本篇文章引導(dǎo)你使用Spring Boot,Spring Data JPA集成H2內(nèi)存數(shù)據(jù)庫。更多關(guān)于H2數(shù)據(jù)參考:http://www.h2database.com/html/tutorial.html
準(zhǔn)備
- JDK 1.8 或更高版本
- Maven 3 或更高版本
技術(shù)棧
- Spring Data JPA
- Spring Boot
目錄結(jié)構(gòu)

pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>jpa-example</artifactId>
<groupId>cn.merryyou</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>h2-webconsole</artifactId>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.196</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
實(shí)體類 User
@Entity
@Table(name = "t_user")
@Data
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String url;
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", url='" + url + '\'' +
'}';
}
}
@Table聲明此對象映射到數(shù)據(jù)庫的數(shù)據(jù)表,通過它可以為實(shí)體指定表(talbe),目錄(Catalog)和schema的名字。該注釋不是必須的,如果沒有則系統(tǒng)使用默認(rèn)值(實(shí)體的短類名)。@Id聲明此屬性為主鍵。該屬性值可以通過應(yīng)該自身創(chuàng)建,但是Hibernate推薦通過Hibernate生成@GeneratedValue指定主鍵的生成策略。- TABLE:使用表保存id值
- IDENTITY:identitycolumn
- SEQUENCR :sequence
- AUTO:根據(jù)數(shù)據(jù)庫的不同使用上面三個
@Column 聲明該屬性與數(shù)據(jù)庫字段的映射關(guān)系。
AddressRepository
public interface UserRepository extends JpaRepository<User, Integer> {
}
Spring Data JPA包含了一些內(nèi)置的Repository,實(shí)現(xiàn)了一些常用的方法:findone,findall,save等。
application.yml
spring:
datasource:
url: jdbc:h2:mem:h2test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
platform: h2
username: sa
password:
driverClassName: org.h2.Driver
jpa:
database-platform: org.hibernate.dialect.H2Dialect
hibernate:
ddl-auto: update
properties:
hibernate:
show_sql: true
use_sql_comments: true
format_sql: true
h2:
console:
enabled: true
path: /console
settings:
trace: false
web-allow-others: false
logging:
level: debug
連接配置
在application.yml文件中對數(shù)據(jù)庫進(jìn)行連接配置
spring.datasource.url=jdbc:h2:mem:h2test,配置h2數(shù)據(jù)庫的連接地址spring.datasource.driver-class-name=org.h2.Driver,配置JDBC Driverspring.datasource.username=sa,配置數(shù)據(jù)庫用戶名spring.datasource.password=,配置數(shù)據(jù)庫密碼
當(dāng)你完成依賴和連接配置這兩步之后,你就可以在程序種使用h2了。spring會自動幫你完成DataSource的注入。
數(shù)據(jù)初始化配置
如果你需要在程序啟動時對數(shù)據(jù)庫進(jìn)行初始化操作,則在application.properties文件中對數(shù)據(jù)庫進(jìn)接配置
spring.datasource.schema=classpath:db/schema.sql,進(jìn)行該配置后,每次啟動程序,程序都會運(yùn)行resources/db/schema.sql文件,對數(shù)據(jù)庫的結(jié)構(gòu)進(jìn)行操作。spring.datasource.data=classpath:db/data.sql,進(jìn)行該配置后,每次啟動程序,程序都會運(yùn)行resources/db/data.sql文件,對數(shù)據(jù)庫的數(shù)據(jù)操作。
該配置非常適合開發(fā)環(huán)境,我會把數(shù)據(jù)庫的結(jié)構(gòu)構(gòu)建sql放在resources/db/schema.sql,數(shù)據(jù)sql放在resources/db/data.sql中。這樣每次運(yùn)行程序我都可以得到一個新的數(shù)據(jù)庫。這樣就不需要我每次為了測試而修改數(shù)據(jù)中的內(nèi)容了。
h2 web consloe配置
h2 web consloe是一個數(shù)據(jù)庫GUI管理應(yīng)用,就和phpMyAdmin類似。程序運(yùn)行時,會自動啟動h2 web consloe。當(dāng)然你也可以進(jìn)行如下的配置。
spring.h2.console.settings.web-allow-others=true,進(jìn)行該配置后,h2 web consloe就可以在遠(yuǎn)程訪問了。否則只能在本機(jī)訪問。spring.h2.console.path=/h2-console,進(jìn)行該配置,你就可以通過YOUR_URL/h2-console訪問h2 web consloe。YOUR_URL是你程序的訪問URl。spring.h2.console.enabled=true,進(jìn)行該配置,程序開啟時就會啟動h2 web consloe。當(dāng)然這是默認(rèn)的,如果你不想在啟動程序時啟動h2 web consloe,那么就設(shè)置為false。
UserRepositoryTest
@SpringBootTest
@RunWith(SpringRunner.class)
@Slf4j
public class UserRepositoryTest {
@Autowired
private UserRepository userRepository;
@Test
public void saveTest() throws Exception {
User user = new User();
user.setName("鄭龍飛");
user.setUrl("http://merryyou.cn");
User result = userRepository.save(user);
log.info(result.toString());
Assert.assertNotNull(user.getId());
}
@Test
public void findOneTest() throws Exception{
User user = userRepository.findOne(1l);
log.info(user.toString());
Assert.assertNotNull(user);
Assert.assertTrue(1l==user.getId());
}
}
h2 web consloe


代碼下載
從我的 github 中下載,https://github.com/longfeizheng/jpa-example/tree/master/h2-webconsole
到此這篇關(guān)于SpringBoot集成H2內(nèi)存數(shù)據(jù)庫的方法的文章就介紹到這了,更多相關(guān)SpringBoot集成H2內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Springboot實(shí)例講解實(shí)現(xiàn)專業(yè)材料認(rèn)證管理系統(tǒng)流程
這是一個基于java的畢業(yè)設(shè)計項目,畢設(shè)課題為springboot框架的知識產(chǎn)權(quán)服務(wù)平臺系統(tǒng),是一個采用b/s結(jié)構(gòu)的javaweb項目,需要的朋友可以參考下2022-06-06
JDK與JRE的下載和安裝以及配置JDK環(huán)境變量圖文教程
JRE也就是(Java?RuntimeEnvironment)Java運(yùn)行環(huán)境,是運(yùn)行JAVA程序所必須的環(huán)境的集合,包含各種類庫,下面這篇文章主要給大家介紹了關(guān)于JDK與JRE的下載和安裝以及配置JDK環(huán)境變量的相關(guān)資料,需要的朋友可以參考下2023-12-12
Springboot整合企業(yè)微信機(jī)器人助手推送消息的實(shí)現(xiàn)
本文主要介紹了Springboot整合企業(yè)微信機(jī)器人助手推送消息的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05
Java中JFinal框架動態(tài)切換數(shù)據(jù)庫的方法
這篇文章主要介紹了Java中JFinal框架動態(tài)切換數(shù)據(jù)庫的方法,本文通過兩種方法結(jié)合示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03
SpringBoot之通過BeanPostProcessor動態(tài)注入ID生成器案例詳解
這篇文章主要介紹了SpringBoot之通過BeanPostProcessor動態(tài)注入ID生成器案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-09-09
SpringBoot 改造成https訪問的實(shí)現(xiàn)
這篇文章主要介紹了SpringBoot 改造成https訪問的實(shí)現(xiàn),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10
Java動態(tài)線程池插件dynamic-tp集成zookeeper
ZooKeeper是一個分布式的,開放源碼的分布式應(yīng)用程序協(xié)調(diào)服務(wù),是Google的Chubby一個開源的實(shí)現(xiàn),是Hadoop和Hbase的重要組件。它是一個為分布式應(yīng)用提供一致性的軟件,提供的功能包括:配置維護(hù)、域名服務(wù)、分布式同步、組服務(wù)等2023-03-03
IDEA 2023創(chuàng)建JSP項目的完整步驟教程
這篇文章主要介紹了IDEA 2023創(chuàng)建JSP項目的完整步驟教程,創(chuàng)建項目需要經(jīng)過新建項目、設(shè)置項目名稱和路徑、選擇JDK版本、添加模塊和工件、配置Tomcat服務(wù)器等步驟,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2024-10-10

