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. Log4j介绍,log4j.properties配置详解

    http://www.cnblogs.com/simle/archive/2011/09/29/2195341.html本文主要解释log4j的配置文件各个配置项的含义,内容是从网上转载的 1.Log ...

  2. C# socket编程 使用fleck轻松实现对话 https://github.com/statianzo/Fleck

    class Program { static void Main(string[] args) { FleckLog.Level = LogLevel.Debug; var allSockets = ...

  3. Drawable资源的初步使用

    刚開始接触到Android的时候,看到类似以下的一个Button: 当时感觉这种button有点像Material Design风格.真的以为是裁剪好的图片,好奇心驱使我上网查找实现的方法,原来不是裁 ...

  4. 转:关于安卓多线程while(true)方法占用CPU高的原因及其解决方法

    由于项目需要用到安卓多线程操作,结果开了四条线程,下载到平板一直很卡,CPU占用率暴涨.于是开始查找原因,发现是线程run()方法里的while(true)导致的, 下图是为解决时开启一条while( ...

  5. cpu分析简介

    进程占用CPU过高,一般有以下两种原因:          1.    业务量过大导致进程处理负荷高,占用CPU资源:2.    程序BUG导致,比如死循环:    初步查看cpu占用情况top进一步 ...

  6. 557. Reverse Words in a String III【easy】

    557. Reverse Words in a String III[easy] Given a string, you need to reverse the order of characters ...

  7. strex,ldrex

    volatile bool lock = false; void func(void) { int i; while(lock);   lock = true; for(i = 0; i < 4 ...

  8. LeetCode543. Diameter of Binary Tree

    Description Given a binary tree, you need to compute the length of the diameter of the tree. The dia ...

  9. jQuery 尺寸 方法

    jQuery 提供多个处理尺寸的重要方法: width() height() innerWidth() innerHeight() outerWidth() outerHeight()

  10. Android-ViewPagerIndicator框架使用——IconPageIndicator

    前言:IconPageIndicator是将自定义的图片作为指示图标的,这里的图片使用xml实现的. 1.自己定义图片: <?xml version="1.0" encodi ...