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. Quartz的cron表达式 (spring定时器 crontab)

    http://tangshuo.iteye.com/blog/184824 表达式位数最少六位,如每天凌晨一点启动:"0 0 1 * * ?" 顺序按   秒 分 时 日期 月份 ...

  2. 二维数组,锯齿数组和集合 C# 一维数组、二维数组(矩形数组)、交错数组(锯齿数组)的使用 C# 数组、多维数组(矩形数组)、锯齿数组(交叉数组)

    二维数组,锯齿数组和集合 一.二维数组 二维数组:一维数组----豆角二维数组----表格 定义:1.一维数组:数据类型[] 数组变量名 = new 数据类型[数组长度];数据类型[] 数组变量名 = ...

  3. oracle加入not null约束

    在创建表时.为列加入not null约束,形式例如以下: column_name data_type [constraint constraint_name] not null 当中,constrai ...

  4. Hackerrank - Game Of Rotation 题解

    旋转一个数组以得到最大值. 陷阱就是:不能排序.须要模拟操作旋转,并设计公式计算旋转后的和. 要求是O(n)时间完毕. 原题: https://www.hackerrank.com/challenge ...

  5. kafka快速开始教程

    此教程假设你刚刚开始没有任何 Kafka 或 ZooKeeper 数据.Kafka的控制台脚本在类Unix和Windows平台不同,Windows平台使用bin\windows\\代替bin/,脚本的 ...

  6. VC驿站黑客编程(关机,重新启动,注销)

    此程序在VS2013下编译通过,假设换到编译器,大家能够稍作改动使用 #include<Windows.h> #include<tchar.h> #include"r ...

  7. 459. Repeated Substring Pattern【easy】

    459. Repeated Substring Pattern[easy] Given a non-empty string check if it can be constructed by tak ...

  8. 李洪强经典面试题41-iOS选择题

    1.及时聊天app不会采用的网络传输方式是 DA UDP B TCP C Http D FTP 2.下列技术不属于多线程的是 AA Block B NSThread C NSOperation D G ...

  9. python简单C/S模式示例

    服务器端代码: #!/usr/bin/python import time, socket, threading # thread handle function def tcplink(sock, ...

  10. C++避免内存泄漏的一种技巧

    C++ Primer 4th中在section 13.5中的U_Ptr就是一种实用的例子 通过计数的方式,并提供自己的抽象类型的Pointer,从而实现内存管理.在一定的范围内还是非常有效的,比如说在 ...