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

springboot集成h2的更多相关文章

  1. springboot集成h2以及可视化操作

    1.新建项目

  2. springboot集成liquibase,h2数据库

    Liquibase是一个用于跟踪.管理和应用数据库变化的开源的数据库重构工具.它将所有数据库的变化(包括结构和数据)都保存在XML文件中,便于版本控制. Liquibase具备如下特性:* 不依赖于特 ...

  3. Springboot集成Spring Batch

    Spring官网 (https://spring.io/projects/spring-batch#overview)对Spring  Batch的解释: 一个轻量级的.全面的批处理框架,用于开发对企 ...

  4. SpringBoot集成MybatisPlus解决Mapper文件修改后动态刷新的问题

    很多人在使用SpringBoot集成Mybatis或者MybatisPlus的时候在查询复杂的情况下会写mapper文件,虽然说MyBatisPlus提供了常用的增删查改,但还是难以应付复杂的查询.关 ...

  5. SpringBoot集成Mybatis配置动态数据源

    很多人在项目里边都会用到多个数据源,下面记录一次SpringBoot集成Mybatis配置多数据源的过程. pom.xml <?xml version="1.0" encod ...

  6. springboot集成websocket的两种实现方式

    WebSocket跟常规的http协议的区别和优缺点这里大概描述一下 一.websocket与http http协议是用在应用层的协议,他是基于tcp协议的,http协议建立链接也必须要有三次握手才能 ...

  7. SpringBoot集成邮件发送

    一:简述 在日常中的工作中难免会遇到程序集成邮件发送功能.接收功能:此篇文章我将使用SpringBoot集成邮件发送功能和接收功能:若对邮件一些基本协议和发送流程不懂的请务必参考我之前写的博客或者浏览 ...

  8. 【springBoot】springBoot集成redis的key,value序列化的相关问题

    使用的是maven工程 springBoot集成redis默认使用的是注解,在官方文档中只需要2步; 1.在pom文件中引入即可 <dependency> <groupId>o ...

  9. SpringBoot集成security

    本文就SpringBoot集成Security的使用步骤做出解释说明.

随机推荐

  1. MATLABR 2016 b 安装教程

    1.下载相应的 MATLABR 2016 b 版本如下: 主要是下面三个文件,其中, Matlab 2016b Win64 Crack.rar 是破解文件.另两个为安装包.(本软件在win8/10上不 ...

  2. Python3 range()函数

    Python3 range() 函数用法  Python3 内置函数 Python3 range() 函数返回的是一个可迭代对象(类型是对象),而不是列表类型, 所以打印的时候不会打印列表. Pyth ...

  3. 记一起和前端没什么卵关系的OPTION 405问题

    记一起和前端没什么卵关系的后端405问题 问题的关键点在于本来是POST请求,会变成OPTION请求,并且提示405报错,会类似跨域.并且只有某些手机机型才会(如Oppo系列). 其实跨域的问题,如果 ...

  4. SpringCloud系列十三:Feign对继承、压缩、日志的支持以及构造多参数请求

    1. 回顾 上文讲解了手动创建Feign,比默认的使用更加灵活. 本文将讲解Feign对继承.压缩的支持以及日志和多参数请求的构造等. 2. Feign对继承的支持 Feign支持继承.使用继承,可将 ...

  5. 【转】jQuery.extend 函数详解

    原文地址:http://www.cnblogs.com/RascallySnake/archive/2010/05/07/1729563.html JQuery的extend扩展方法:      Jq ...

  6. RabbitMQ基础概念

    转至:http://blog.csdn.net/whycold/article/details/41119807 RabbitMQ简介 AMQP,即Advanced Message Queuing P ...

  7. C++语言基础(13)-抽象类和纯虚函数

    一.基本语法 在C++中,可以将虚函数声明为纯虚函数,语法格式为: ; 纯虚函数没有函数体,只有函数声明,在虚函数声明的结尾加上=0,表明此函数为纯虚函数. 最后的=0并不表示函数返回值为0,它只起形 ...

  8. PHP预定义常量DIRECTORY_SEPARATOR

    PHP预定义常量DIRECTORY_SEPARATOR BY 天涯 · // DIRECTORY_SEPARATOR是一个显示系统分隔符的命令,DIRECTORY_SEPARATOR是PHP的内部常量 ...

  9. CentOS设置sendmail发件人,让sendmail不显示通过XXX代发

    0.有一个十分快速的方法 命令:hostname itzhanzhang.com,但是重启后会失效,于是请接着往下看一劳永逸的方法: 1.设置你的主机名 默认的主机名是类似于“VM_24_76_cen ...

  10. Google Chrome v48.0.2564.

    http://www.pcpop.com/doc/1/1819/1819996.shtml 摘要:谷歌浏览器Chrome Stable 稳定版迎来例行升级,新版本号为v48.0.2564.82,上一版 ...