> 文章列表 > springboot引入H2数据库

springboot引入H2数据库

springboot引入H2数据库

首先加入H2的依赖

<dependency><groupId>com.h2database</groupId><artifactId>h2</artifactId><scope>runtime</scope>
</dependency>

然后加入mybatis的依赖(除了使用mybatis,也可以使用JPA等操作数据库的东西,必须引入其中一个,否则sql文件无法初始化)

<dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.2.0</version>
</dependency>

目录,注意,sql文件必须放在resources下面

 

 

application.properties中的配置

spring.datasource.schema=classpath:/db/schema.sql
spring.datasource.data=classpath:/db/data.sql
spring.datasource.url=jdbc:h2:mem:test
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driverClassName=org.h2.Driver
spring.h2.console.path=/h2
spring.h2.console.enabled=true
spring.h2.console.settings.web-allow-others=true

schema.sql


drop table user if exists;
create table user(id bigint not null auto_increment,name varchar(40),age int,primary key(id)
)

data.sql

insert into user(name,age) values ('何伟',23);
insert into user(name,age) values ('马宁',16)

启动项目,打开地址:http://localhost:8080/h2/

剩下的就是关于通过mybatis来操作的CRUD了,略过