springboot集成h2


h2數據庫是常用的開源數據庫,與HSQLDB類似,十分適合作為嵌入式數據庫使用,其他的數據庫大部分都需要安裝獨立的客戶端和服務器端
 h2的優勢
  (1)h2采用純java編寫,因此不受平台的限制
  (2)h2只有一個jar文件,十分適合作為嵌入式數據庫使用
  (3)h2提供了一個十分方便的web控制台用於操作和管理數據庫內容。

下面介紹下h2數據庫的簡單使用

1.添加依賴

  創建項目的時候,在數據庫選項里直接勾選h2選項,如果是二次項目,在pom文件里添加以下依賴

<dependency>
   <groupId>com.h2database</groupId>
   <artifactId>h2</artifactId>
   <scope>runtime</scope>
</dependency>

2.連接配置

在 application.properties 文件(或者 applocation.yml 文件)對數據庫進行連接配置

1 spring.datasource.url = jdbc:h2:file:~/.h2/testdb //配置h2數據庫連接地址 
2 spring.datasource.driverClassName =org.h2.Driver //配置JDBC Driver
3 spring.datasource.username = sa //配置數據庫用戶名
4 spring.datasource.password = //配置數據庫密碼

  完成依賴配置和連接配置以后,就可以在項目里使用h2數據庫了,Spring會自動完成Datasource的注入,之后無論是用jpa還是mybatis都可以

3.數據初始化配置

如果需要在程序啟動時對數據庫進行初始化操作,在 application.peoperties 或yml文件里對數據庫進行配置

1 spring.datasource.schema=classpath:db/schema.sql   //進行該配置后,每次啟動程序,程序都會運行
2 resources/db/schema.sql                            //sql文件,對數據庫的結構進行操作。xml文件也行
3 spring.datasource.data=classpath:db/data.sql       //進行該配置后,每次啟動程序,程序都會運行    
4 resources/db/data.sql                              //sql文件,對數據庫的數據操作。xml文件也行

這個配置十分適合開發環境,最好把數據庫結構的構建sql放在 resources/db/schema.sql ,數據sql放在 resources/db/data.sql 中,這樣每次
重啟項目都可以得到一個新的數據庫,這樣就不需要每次為了測試而修改數據中的內容了。

4.h2 web consloe

   h2 web consloe是一個數據庫GUI管理應用,和phpMyAdmin類似,程序運行時,會自動啟動h2 web consloe,當然也可以進行如下的配置:

1 spring.h2.console.settings.web-allow-others=true        //進行該配置后,h2 web consloe就可以在遠程訪問了。否則只能在本機訪問。
2 spring.h2.console.path=/h2-console                      //進行該配置,你就可以通過YOUR_URL/h2-console訪問h2 web consloe。YOUR_URL是你程序的訪問URl。
3 spring.h2.console.enabled=true                   //進行該配置,程序開啟時就會啟動h2 web consloe。當然這是默認的,如果你不想在啟動程序時啟動h2 web consloe,那么就設置為false。

配置以后,在瀏覽器輸入 http://localhost:8080/h2-console  然后輸入用戶名和密碼,選擇好合適的語言,就可以進入數據庫管理應用啦

5.實例

  在resource文件下新建包 changelog ,包里包含三個xml文件: changelog.xml ,  data.xml  , init.xml 
   其中 changelog.xml 寫的是數據庫表的結構 , data.xml 文件里寫的是數據庫表的初始化數據  init.xml 是初始化加載的xml文件,包含前兩個xml文件的路徑

 init.xml 文件:

1 <?xml version="1.0" encoding="utf-8"?>
2 <databaseChangeLog
3         xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
4         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5         xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.4.xsd">
6     <include file="changeLog.xml" relativeToChangelogFile="true"/>
7     <include file="data.xml" relativeToChangelogFile="true"/>
8 </databaseChangeLog>

 changelog.xml 文件:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <databaseChangeLog
 3         xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
 4         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 5         xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
 6         http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.4.xsd">
 7 
 8     <property name="autoIncrement" value="true" dbms="h2"/>
 9     <changeSet id="init-schema" author="jinzhe"  >
10         <comment>init schema</comment>
11 
12         <createTable tableName="user">
13             <column name="id" type="bigint" autoIncrement="${autoIncrement}">
14                 <constraints primaryKey="true" nullable="false"/>
15             </column>
16             <column name="username" type="varchar(20)" >
17                 <constraints  nullable="false" uniqueConstraintName="username"/>
18             </column>
19             <column name="password" type="varchar(20)">
20                 <constraints  nullable="false"/>
21             </column>
22             <column name="email" type="varchar(20)">
23                 <constraints  nullable="false"/>
24             </column>
25             <column name="phone" type="varchar(11)">
26                 <constraints  nullable="false"/>
27             </column>
28             <column name="sex" type="varchar(2)">
29                 <constraints  nullable="false"/>
30             </column>
31             <column name="creat_time" type="java.util.Date">
32                 <constraints  nullable="false"/>
33             </column>
34             <column name="update_time" type="java.util.Date">
35                 <constraints  nullable="false"/>
36             </column>
37         </createTable>
38     </changeSet>
39 </databaseChangeLog>

 data.xml 文件:

 1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
 2 <databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
 3                    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4                    xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-2.0.xsd">
 5     
 6     <changeSet id="001" author="jin">
 7         
 8         <insert tableName="user" schemaName="public" >
 9             <column name="id" value="1"></column>
10             <column name="username" value="admin123"></column>
11             <column name="password" value="123456"></column>
12             <column name="email" value="165464614@qq.com"></column>
13             <column name="phone" value="15330774444"></column>
14             <column name="sex" value="男"></column>
15             <column name="creat_time" value="2017-12-01 00:00:00"></column>
16             <column name="update_time" value="2017-12-01 00:00:00"></column>
17         </insert>
18 
19     </changeSet>
20 
21 </databaseChangeLog>

 參考:https://segmentfault.com/a/1190000007002140


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM