SpringBoot集成H2內(nèi)存數(shù)據(jù)庫(kù)的方法_第1頁(yè)
SpringBoot集成H2內(nèi)存數(shù)據(jù)庫(kù)的方法_第2頁(yè)
SpringBoot集成H2內(nèi)存數(shù)據(jù)庫(kù)的方法_第3頁(yè)
SpringBoot集成H2內(nèi)存數(shù)據(jù)庫(kù)的方法_第4頁(yè)
全文預(yù)覽已結(jié)束

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

第SpringBoot集成H2內(nèi)存數(shù)據(jù)庫(kù)的方法SpringDataJPA包含了一些內(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

console:

enabled:true

path:/console

settings:

trace:false

web-allow-others:false

logging:

level:debug

連接配置

在application.yml文件中對(duì)數(shù)據(jù)庫(kù)進(jìn)行連接配置

spring.datasource.url=jdbc:h2:mem:h2test,配置h2數(shù)據(jù)庫(kù)的連接地址

spring.datasource.driver-class-name=org.h2.Driver,配置JDBCDriver

spring.datasource.username=sa,配置數(shù)據(jù)庫(kù)用戶(hù)名

spring.datasource.password=,配置數(shù)據(jù)庫(kù)密碼

當(dāng)你完成依賴(lài)和連接配置這兩步之后,你就可以在程序種使用h2了。spring會(huì)自動(dòng)幫你完成DataSource的注入。

數(shù)據(jù)初始化配置

如果你需要在程序啟動(dòng)時(shí)對(duì)數(shù)據(jù)庫(kù)進(jìn)行初始化操作,則在perties文件中對(duì)數(shù)據(jù)庫(kù)進(jìn)接配置

spring.datasource.schema=classpath:db/schema.sql,進(jìn)行該配置后,每次啟動(dòng)程序,程序都會(huì)運(yùn)行resources/db/schema.sql文件,對(duì)數(shù)據(jù)庫(kù)的結(jié)構(gòu)進(jìn)行操作。

spring.datasource.data=classpath:db/data.sql,進(jìn)行該配置后,每次啟動(dòng)程序,程序都會(huì)運(yùn)行resources/db/data.sql文件,對(duì)數(shù)據(jù)庫(kù)的數(shù)據(jù)操作。

該配置非常適合開(kāi)發(fā)環(huán)境,我會(huì)把數(shù)據(jù)庫(kù)的結(jié)構(gòu)構(gòu)建sql放在resources/db/schema.sql,數(shù)據(jù)sql放在resources/db/data.sql中。這樣每次運(yùn)行程序我都可以得到一個(gè)新的數(shù)據(jù)庫(kù)。這樣就不需要我每次為了測(cè)試而修改數(shù)據(jù)中的內(nèi)容了。

h2webconsloe配置

h2webconsloe是一個(gè)數(shù)據(jù)庫(kù)GUI管理應(yīng)用,就和phpMyAdmin類(lèi)似。程序運(yùn)行時(shí),會(huì)自動(dòng)啟動(dòng)h2webconsloe。當(dāng)然你也可以進(jìn)行如下的配置。

spring.h2.console.settings.web-allow-others=true,進(jìn)行該配置后,h2webconsloe就可以在遠(yuǎn)程訪問(wèn)了。否則只能在本機(jī)訪問(wèn)。

spring.h2.console.path=/h2-console,進(jìn)行該配置,你就可以通過(guò)YOUR_URL/h2-console訪問(wèn)h2webconsloe。YOUR_URL是你程序的訪問(wèn)URl。

spring.h2.console.enabled=true,進(jìn)行該配置,程序開(kāi)啟時(shí)就會(huì)啟動(dòng)h2webconsloe。當(dāng)然這是默認(rèn)的,如果你不想在啟動(dòng)程序時(shí)啟動(dòng)h2webconsloe,那么就設(shè)置為false。

UserRepositoryTest

@SpringBootTest

@RunWith(SpringRunner.class)

@Slf4j

publicclassUserRepositoryTest{

@Autowired

privateUserRepositoryuserRepository;

@Test

publicvoidsaveTest()throwsException{

Useruser=newUser();

user.setName("鄭龍飛");

user.setUrl("");

Userresult=userRepository.save(user);

(result.toString());

Assert.assertNotNull(user.getId());

@Test

publicvoidfindOneTest()throwsException{

Useruser=userRepository.findOne(1l);

(user.toString());

Assert.assertNotNull(user);

Assert.assertTrue(1l==user.get

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論