SpringBoot 整合 H2 数据库

Maven

H2 依赖

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

JPA 依赖,可根据实体类自动建表

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

YAML

spring:
  h2:
    console:
      # h2 web console 路径
      path: /h2
      # 开启 h2 web consloe,默认为 false
      enabled: true
      settings:
        # 允许远程访问 h2 web console
        web-allow-others: true
        # h2 web console 管理员密码
        web-admin-password: root
        # 开启 h2 console 跟踪,方便调试 ,默认 false
        trace: true
  datasource:
    username: sa
    # 数据库连接密码,默认为空,需要在 SQL 脚本内使用 SQL 语句设置
    data-password: root

    # 使用 H2 内存模式的数据库连接方式
    # url: jdbc:h2:mem:hedis

    # 项目启动该自动创建数据库, ~表示数据库创建在用户目录下
    url: jdbc:h2:file:~/h2DB/SpringBootH2DBTest;AUTO_SERVER=TRUE;DB_CLOSE_ON_EXIT=FALSE

    driver-class-name: org.h2.Driver
    # 自动初始化建表
    initialization-mode: always
    # SQL 建表自动执行脚本
    schema: classpath:schema.sql
    # SQL 
    data: classpath:data.sql
原文地址:https://www.cnblogs.com/Haidnor/p/14355516.html