Spring Boot集成H2数据库

2019-02-23  本文已影响0人  felix1982

原创文章, 转载请注明出处
博客链接地址

在日常开发过程中,特别是敏捷开发过程以及测试过程中,我们有时候不想操纵真实的数据库.那么,H2数据库是个不错的选择。

H2是一个开源的、纯java实现的关系数据库。
h2数据库特点
(1)性能、小巧
(2)同时支持网络版和嵌入式版本,另外还提供了内存版
(3)有比较好的兼容性,支持相当标准的sql标准
(4)提供了非常友好的基于web的数据库管理界面

在spring boot项目中配置H2数据库

application.properies

spring.datasource.url=jdbc:h2:mem:test_db
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.schema=classpath:db/schema.sql
spring.datasource.data=classpath:db/data.sql
spring.h2.console.enabled=true

schema.sql

CREATE TABLE student(
 id int not null,
 name varchar(20) null,
 age int null
);

data.sql

INSERT  INTO  student VALUES (1,'张三',10);
INSERT  INTO  student VALUES (2,'李四',16);

项目目录结构变为如下:

1.png
配置完成后,启动项目后,浏览器中输入http://localhost:8080/h2-console/
2.png
登录后页面为:
3.png

项目代码地址: https://github.com/felix1982/spring-boot-practice/tree/master/spring-boot-h2

上一篇 下一篇

猜你喜欢

热点阅读