前言
记录springboot环境下使用liquibase管理h2数据库升级脚本,代替默认直接使用sql脚本方式,定义和加载数据
- 好处:
- 不同数据库sql建表方式语法略有差异,统一使用liquibase管理,方便项目从内嵌式数据库切换成mysql等关系型数据库, 更有灵活性。
- 能够使用版本管理而不是一直往schema和data文件中加数据
正文
1. maven依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
</parent>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<!-- liquibase管理升级脚本(org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration 自动装配)-->
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
</dependency>
<!-- log4jdbc -->
<dependency>
<groupId>com.googlecode.log4jdbc</groupId>
<artifactId>log4jdbc</artifactId>
<version>1.2</version>
<!-- <scope>runtime</scope>-->
</dependency>
2. 默认方式:直接管理schema和data
2.1 application.yaml
spring:
datasource:
driver-class-name: net.sf.log4jdbc.DriverSpy
schema: classpath:db/schema-h2.sql
data: classpath:db/data-h2.sql
url: jdbc:log4jdbc:h2:mem:test
username: root
password: test
2.2 db/schema-h2.sql
CREATE TABLE `sys_log` (
`log_id` varchar(32) NOT NULL COMMENT '日志主键',
`type` varchar(20) DEFAULT NULL COMMENT '日志类型',
`title` varchar(100) DEFAULT NULL COMMENT '日志标题',
`description` varchar(500) DEFAULT NULL COMMENT '日志描述',
`ip` varchar(20) DEFAULT NULL COMMENT '请求IP',
`request_uri` varchar(300) DEFAULT NULL COMMENT 'URI',
`method` varchar(300) DEFAULT NULL COMMENT '请求方式',
`params` text COMMENT '提交参数',
`exception` text COMMENT '异常',
`operate_date` datetime NULL DEFAULT NULL COMMENT '操作时间',
`timeout` varchar(10) DEFAULT NULL COMMENT '请求时长',
`login_name` varchar(32) DEFAULT NULL COMMENT '用户登入名',
`request_id` varchar(36) DEFAULT NULL COMMENT 'requestID',
`data_snapshot` text COMMENT '历史数据',
`request_timestamp` bigint(32) DEFAULT NULL COMMENT '请求时间戳',
`status` int(11) DEFAULT NULL COMMENT '日志状态',
PRIMARY KEY (`log_id`)
) ENGINE=InnoDB DEFAULT;
CREATE TABLE `sys_user` (
`user_id` varchar(32) NOT NULL COMMENT '用户ID',
`organization_id` varchar(32) DEFAULT NULL COMMENT '机构ID',
`username` varchar(32) DEFAULT NULL COMMENT '用户名',
`name` varchar(20) DEFAULT NULL COMMENT '姓名',
`password` varchar(32) DEFAULT NULL COMMENT '密码',
`sex` char(1) DEFAULT NULL COMMENT '性别',
`phone` varchar(100) DEFAULT NULL COMMENT '手机',
`email` varchar(100) DEFAULT NULL COMMENT '邮件',
`create_date` datetime DEFAULT NULL COMMENT '创建日期',
`create_by` varchar(32) DEFAULT NULL COMMENT '创建人ID',
`locked` int(2) DEFAULT NULL COMMENT '是否被锁定',
`login_ip` varchar(20) DEFAULT NULL COMMENT '最后登入IP',
`login_date` datetime DEFAULT NULL COMMENT '最后登入日期',
PRIMARY KEY (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
2.3 db/data-h2.sql
INSERT INTO `sys_log` (`log_id`, `type`, `title`, `ip`, `request_uri`, `method`, `params`, `exception`, `operate_date`, `timeout`, `login_name`, `request_id`, `data_snapshot`, `request_timestamp`, `status`) VALUES ('111', 'http', 'dfdfd', '1.1.1.1', '/hdhdh', 'get', NULL, NULL, '2019-08-30 10:00:12.000000', '1', NULL, NULL, NULL, NULL, NULL);
3. liquibase新方式:liquibase间接管理
3.1 application.yaml
# 使用h2快速测试--使用liquibase管理h2的数据
spring:
datasource:
driver-class-name: net.sf.log4jdbc.DriverSpy
url: jdbc:log4jdbc:h2:mem:test #内存模式
# url: jdbc:log4jdbc:h2:file:D:\git\demo\demo-aop-log\target\temp\h2 # 文件模式
username: root
password: test
# liquibase 支持h2对应的changelog.xml要写对
liquibase:
enabled: true
change-log: classpath:db/liquibase/changelog-master.xml
3.2 classpath:db/liquibase/changelog-master.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.6.xsd">
<!-- 指定文件加载-->
<include file="./changelog/changelog_init.xml" relativeToChangelogFile="true" />
</databaseChangeLog>
3.3 db/liquibase/changelog/changelog_init.xml
<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.5.xsd">
<changeSet author="zhangsan" id="create.sys_log" >
<createTable tableName="sys_log">
<column name="log_id" remarks="日志主键" type="VARCHAR(32)">
<constraints nullable="false" primaryKey="true"/>
</column>
<column name="type" remarks="日志类型" type="VARCHAR(20)"/>
<column name="title" remarks="日志标题" type="VARCHAR(100)"/>
<column name="description" remarks="日志描述" type="VARCHAR(500)"/>
<column name="ip" remarks="请求IP" type="VARCHAR(20)"/>
<column name="request_uri" remarks="URI" type="VARCHAR(300)"/>
<column name="method" remarks="请求方式" type="VARCHAR(300)"/>
<column name="params" remarks="提交参数" type="TEXT"/>
<column name="exception" remarks="异常" type="TEXT"/>
<column name="operate_date" remarks="操作时间" type="datetime(6)"/>
<column name="timeout" remarks="请求时长" type="VARCHAR(10)"/>
<column name="login_name" remarks="用户登入名" type="VARCHAR(32)"/>
<column name="request_id" remarks="requestID" type="VARCHAR(36)"/>
<column name="data_snapshot" remarks="历史数据" type="TEXT"/>
<column name="request_timestamp" remarks="请求时间戳" type="BIGINT"/>
<column name="status" remarks="日志状态" type="INT"/>
</createTable>
</changeSet>
<changeSet author="zhangsan" id="create.sys_user" >
<createTable tableName="sys_user">
<column name="user_id" remarks="用户ID" type="VARCHAR(32)">
<constraints nullable="false" primaryKey="true"/>
</column>
<column name="organization_id" remarks="机构ID" type="VARCHAR(32)"/>
<column name="username" remarks="用户名" type="VARCHAR(32)"/>
<column name="name" remarks="姓名" type="VARCHAR(20)"/>
<column name="password" remarks="密码" type="VARCHAR(32)"/>
<column name="sex" remarks="性别" type="CHAR(1)"/>
<column name="phone" remarks="手机" type="VARCHAR(100)"/>
<column name="email" remarks="邮件" type="VARCHAR(100)"/>
<column name="create_date" remarks="创建日期" type="datetime(6)"/>
<column name="create_by" remarks="创建人ID" type="VARCHAR(32)"/>
<column name="locked" remarks="是否被锁定" type="INT"/>
<column name="login_ip" remarks="最后登入IP" type="VARCHAR(20)"/>
<column name="login_date" remarks="最后登入日期" type="datetime(6)"/>
</createTable>
</changeSet>
</databaseChangeLog>
4. 使用h2-console测试
springboot集成的h2内嵌了一个web数据管理界面,默认关闭。开启可以很方便的观察数据和对数据操作,相当于一个迷你的数据库客户端。
4.1 开启h2-console
自动装配类:org.springframework.boot.autoconfigure.h2.H2ConsoleAutoConfiguration
spring:
# 【开启h2界面访问】
h2:
console:
# 默认关闭 http://host:port/h2-console (url=jdbc:h2:mem:test / 账号密码=root/test 和上面的datasource配置保持一致)
enabled: true
settings:
# 不开启报错: Sorry, remote connections ('webAllowOthers') are disabled on this server.
web-allow-others: true
4.2 登录配置
- 连接配置参考datasource配置,保持一致即可。file模式也一样
- url:jdbc:h2:mem:test
- username: root
- password: test
4.3 效果
5. 问题
5.1 liquibase生成的原始changelog.xml对h2不可用
通过maven插件liquibase-maven-plugin方向生成原始changelog.xml,在h2数据库环境下不可直接用
原始xml如下
<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.5.xsd">
<changeSet author="Administrator (generated)" id="1643274861695-1" objectQuotingStrategy="QUOTE_ALL_OBJECTS">
<createTable tableName="sys_log">
<column name="log_id" remarks="日志主键" type="VARCHAR(32)">
<constraints nullable="false"/>
</column>
<column name="type" remarks="日志类型" type="VARCHAR(20)"/>
<column name="title" remarks="日志标题" type="VARCHAR(100)"/>
<column name="description" remarks="日志描述" type="VARCHAR(500)"/>
<column name="ip" remarks="请求IP" type="VARCHAR(20)"/>
<column name="request_uri" remarks="URI" type="VARCHAR(300)"/>
<column name="method" remarks="请求方式" type="VARCHAR(300)"/>
<column name="params" remarks="提交参数" type="TEXT"/>
<column name="exception" remarks="异常" type="TEXT"/>
<column name="operate_date" remarks="操作时间" type="datetime(6)"/>
<column name="timeout" remarks="请求时长" type="VARCHAR(10)"/>
<column name="login_name" remarks="用户登入名" type="VARCHAR(32)"/>
<column name="request_id" remarks="requestID" type="VARCHAR(36)"/>
<column name="data_snapshot" remarks="历史数据" type="TEXT"/>
<column name="request_timestamp" remarks="请求时间戳" type="BIGINT"/>
<column name="status" remarks="日志状态" type="INT"/>
</createTable>
</changeSet>
<changeSet author="Administrator (generated)" id="1643274861695-2" objectQuotingStrategy="QUOTE_ALL_OBJECTS">
<createTable tableName="sys_user">
<column name="user_id" remarks="用户ID" type="VARCHAR(32)">
<constraints nullable="false"/>
</column>
<column name="organization_id" remarks="机构ID" type="VARCHAR(32)"/>
<column name="username" remarks="用户名" type="VARCHAR(32)"/>
<column name="name" remarks="姓名" type="VARCHAR(20)"/>
<column name="password" remarks="密码" type="VARCHAR(32)"/>
<column name="sex" remarks="性别" type="CHAR(1)"/>
<column name="phone" remarks="手机" type="VARCHAR(100)"/>
<column name="email" remarks="邮件" type="VARCHAR(100)"/>
<column name="create_date" remarks="创建日期" type="datetime(6)"/>
<column name="create_by" remarks="创建人ID" type="VARCHAR(32)"/>
<column name="locked" remarks="是否被锁定" type="INT"/>
<column name="login_ip" remarks="最后登入IP" type="VARCHAR(20)"/>
<column name="login_date" remarks="最后登入日期" type="datetime(6)"/>
</createTable>
</changeSet>
<changeSet author="Administrator (generated)" id="1643274861695-3" objectQuotingStrategy="QUOTE_ALL_OBJECTS">
<addPrimaryKey columnNames="log_id" constraintName="PRIMARY" tableName="sys_log"/>
</changeSet>
<changeSet author="Administrator (generated)" id="1643274861695-4" objectQuotingStrategy="QUOTE_ALL_OBJECTS">
<addPrimaryKey columnNames="user_id" constraintName="PRIMARY" tableName="sys_user"/>
</changeSet>
</databaseChangeLog>
- 改造点
- 删除独立加主键语句,在建表语句中定义主键
- 删除changSet中生成的objectQuotingStrategy=“QUOTE_ALL_OBJECTS” 配置