springboot集成h2

2018-06-17 20:03:14来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

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

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:MongoDB的使用

下一篇:springboot集成liquibase,h2数据库