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 文件)对数据库进行连接配置

 spring.datasource.url = jdbc:h2:file:~/.h2/testdb //配置h2数据库连接地址
spring.datasource.driverClassName =org.h2.Driver //配置JDBC Driver
spring.datasource.username = sa //配置数据库用户名
spring.datasource.password = //配置数据库密码

  完成依赖配置和连接配置以后,就可以在项目里使用h2数据库了,Spring会自动完成Datasource的注入,之后无论是用jpa还是mybatis都可以

3.数据初始化配置

如果需要在程序启动时对数据库进行初始化操作,在 application.peoperties 或yml文件里对数据库进行配置

 spring.datasource.schema=classpath:db/schema.sql   //进行该配置后,每次启动程序,程序都会运行
resources/db/schema.sql //sql文件,对数据库的结构进行操作。xml文件也行
spring.datasource.data=classpath:db/data.sql //进行该配置后,每次启动程序,程序都会运行
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,当然也可以进行如下的配置:

 spring.h2.console.settings.web-allow-others=true        //进行该配置后,h2 web consloe就可以在远程访问了。否则只能在本机访问。
spring.h2.console.path=/h2-console //进行该配置,你就可以通过YOUR_URL/h2-console访问h2 web consloe。YOUR_URL是你程序的访问URl。
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 文件:

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

changelog.xml 文件:

 <?xml version="1.0" encoding="utf-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.4.xsd"> <property name="autoIncrement" value="true" dbms="h2"/>
<changeSet id="init-schema" author="jinzhe" >
<comment>init schema</comment> <createTable tableName="user">
<column name="id" type="bigint" autoIncrement="${autoIncrement}">
<constraints primaryKey="true" nullable="false"/>
</column>
<column name="username" type="varchar(20)" >
<constraints nullable="false" uniqueConstraintName="username"/>
</column>
<column name="password" type="varchar(20)">
<constraints nullable="false"/>
</column>
<column name="email" type="varchar(20)">
<constraints nullable="false"/>
</column>
<column name="phone" type="varchar(11)">
<constraints nullable="false"/>
</column>
<column name="sex" type="varchar(2)">
<constraints nullable="false"/>
</column>
<column name="creat_time" type="java.util.Date">
<constraints nullable="false"/>
</column>
<column name="update_time" type="java.util.Date">
<constraints nullable="false"/>
</column>
</createTable>
</changeSet>
</databaseChangeLog>

data.xml 文件:

 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-2.0.xsd"> <changeSet id="001" author="jin"> <insert tableName="user" schemaName="public" >
<column name="id" value="1"></column>
<column name="username" value="admin123"></column>
<column name="password" value="123456"></column>
<column name="email" value="165464614@qq.com"></column>
<column name="phone" value="15330774444"></column>
<column name="sex" value="男"></column>
<column name="creat_time" value="2017-12-01 00:00:00"></column>
<column name="update_time" value="2017-12-01 00:00:00"></column>
</insert> </changeSet> </databaseChangeLog>

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

最新文章

  1. ASP.NET获取客户端IP地址
  2. 与Android应用程序相关的文件目录都有哪些?(转载)
  3. RMQ
  4. 关键字static和this
  5. jfinal框架教程-学习笔记(二)
  6. ZOJ Monthly, July 2015
  7. Django自定义模型(model)中的字段标签
  8. 让LINQ中的查询语法使用自定义的查询方法
  9. jQuery也能舞出绚丽的界面(完结篇)
  10. 对jsp的初步了解及规范问题(二)
  11. 推荐一款接口文档在线管理系统-MinDoc
  12. dp水一天
  13. 为什么有那么多人愿意喝&quot;鸡汤&quot;?
  14. Linux,Ubuntu,Mint 环境下 navicat 乱码问题
  15. LINUX capability概念及配置
  16. JS ES6中的箭头函数(Arrow Functions)使用
  17. 词云wordcloud入门示例
  18. ActiveMQ的作用总结(应用场景及优势)
  19. Java逻辑运算
  20. python中logging模块的一些简单用法

热门文章

  1. 檢查php文件中是否含有bom的php文件
  2. Cron表达式中特殊字符解释
  3. oracle加入not null约束
  4. python C example:encode mp3 code
  5. GoWeb编程之HelloWorld
  6. RTT驱动实现步骤
  7. 我的第八个java程序--读取word内容
  8. Spring4 MVC+ AngularJS CRUD使用$http实例
  9. 一步步学习python
  10. 【转】Visual studio 快捷键大全