h2是一种内存数据库,可以方便的整合进spring-boot项目内。
1. application.properties
#配置数据库连接地址
spring.datasource.primary.jdbc-url=jdbc:h2:file:./h2/code-generator;AUTO_SERVER=TRUE;DB_CLOSE_ON_EXIT=FALSE
#配置数据库驱动
spring.datasource.primary.driver-class-name=org.h2.Driver
#配置数据库用户名
spring.datasource.primary.username=root
#配置数据库密码
spring.datasource.primary.password=root
#配置能远程访问
spring.h2.console.settings.web-allow-others=true
#配置访问地址
spring.h2.console.path=/h2-console
#配置项目启动 h2就启动
spring.h2.console.enabled=true
jbdc-url的配置:
- file:./h2/code-generator; 表示在项目根路径下保存数据库文件,数据库的名称为code-generator,
- AUTO_SERVER=true 启动自动混合模式,允许开启多个连接,该参数不支持在内存中运行模式
- DB_CLOSE_ON_EXIT 当虚拟机退出时并不关闭数据库
2. h2控制台
访问http://localhost:8089/h2-console,页面如下图