一、H2简介
H2数据库特点:
- H2数据库是一个开源的关系型数据库。H2是一个嵌入式数据库引擎,采用java语言编写,不受
平台的限制,同时支持网络版和嵌入式版本,有比较好的兼容性,支持相当标准的sql标准,支持集群 - 提供JDBC、ODBC访问接口,提供了非常友好的基于web的数据库管理界面
二、JAVA中使用H2数据库
-
以嵌入式(本地)连接方式连接H2数据库
这种连接方式默认情况下只允许有一个客户端连接到H2数据库,有客户端连接到H2数据库之后,此时数据库文件就会被锁定,那么其他客户端就无法再连接了。连接语法:
jdbc:h2:[file:][<path>]<databaseName>
例如:
jdbc:h2:~/test //连接位于用户目录下的test数据库 jdbc:h2:file:/data/sample jdbc:h2:file:E:/H2/gacl(Windows only)
-
使用TCP/IP的服务器模式(远程连接)方式连接H2数据库(推荐).这种连接方式就和其他数据库类似了,是基于Service的形式进行连接的,因此允许多个客户端同时连接到H2数据库
连接语法:
jdbc:h2:tcp://<server>[:<port>]/[<path>]<databaseName>
范例:
jdbc:h2:tcp://localhost/~/test
-
H2数据库的内存模式。H2数据库被称为内存数据库,因为它支持在内存中创建数据库和表。注意:如果使用H2数据库的内存模式,那么我们创建的数据库和表都只是保存在内存中,一旦服务器重启,那么内存中的数据库和表就不存在了。
三、Spring Boot 整合 H2数据库
3.1 依赖注入
除了工程所需的必要依赖外,还需要添加H2和MyBatis依赖。
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
3.2 初始化数据库脚本
在resources中建立两个sql文件,schema.sql与data.sql。
create table ftp_sync_log
(
id varchar2(36) not null,
syncDate TIMESTAMP,
content varchar2(200),
constraint ftp_sync_log_pk
primary key (id)
);
comment on table ftp_sync_log is 'ftp同步日志';
comment on column ftp_sync_log.id is '主键ID';
comment on column ftp_sync_log.syncDate is '同步时间';
comment on column ftp_sync_log.content is '同步内容';
create table websocket_message
(
msg_id varchar2(36) not null,
msg_content varchar2(500),
create_time TIMESTAMP,
aircraft_num varchar2(50),
msg_type int,
state int,
constraint websocket_message_pk
primary key (msg_id)
);
comment on table websocket_message is '发送的消息内容';
comment on column websocket_message.msg_id is '消息ID';
comment on column websocket_message.msg_content is '消息内容';
comment on column websocket_message.create_time is '消息时间';
comment on column websocket_message.aircraft_num is '航班号';
comment on column websocket_message.msg_type is '消息类型';
comment on column websocket_message.state is '同步状态';
INSERT INTO FTP_SYNC_LOG ("ID", "SYNCDATE", "CONTENT") VALUES ('2211122', '2019-07-03 08:51:24.067000000', '332')
3.3 配置
#服务端口
server.port=9200
#thymeleaf开发环境下禁止缓冲
spring.thymeleaf.cache=false
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
###########################mybatis配置###########################
mybatis.mapper-locations=classpath:mapping/*Mapper.xml
mybatis.type-aliases-package=com.siniswift.efb.acars.entity
###########################h2配置#################################
#db schema
spring.datasource.schema=classpath:db/schema.sql
#db data
spring.datasource.data=classpath:db/data.sql
#remote visit
spring.h2.console.settings.web-allow-others=true
#console url
spring.h2.console.path=/h2
#default true
spring.h2.console.enabled=true
spring.h2.console.settings.trace=true
#db url,default :jdbc:h2:mem:testdbsa
spring.datasource.url=jdbc:h2:mem:acars
#driver default:org.h2.Driver
spring.datasource.driver-class-name=org.h2.Driver
#default sa
spring.datasource.username=sa
#default null
spring.datasource.password=
###########################日志配置#################################
logging.file=log/acars.log
除了url、驱动、用户名、密码外,我们关注一下 spring.h2.console.path 这个配置,它表示H2管理界面的相对路径。
3.4 测试访问
访问 http://localhost:9300/h2 可以看到H2的web界面。
新增web api 接口,可测试H2数据增删改查等操作。