文章目录


前言

H2是一个Java编写的关系型数据库,它可以被嵌入Java应用程序中使用,或者作为一个单独的数据库服务器运行。在测试、编写Demo等场景能够起到很好的作用。

H2官网:http://www.h2database.com/html/main.html


Springboot项目引用H2非常简单,仅需引入相关依赖和增加相应配置即可

<dependency>
   <groupId>com.h2database</groupId>
   <artifactId>h2</artifactId>
   <version>2.1.212</version>
</dependency>
spring:
  datasource:
    # mem表示使用内存存储方式,inbox_msg为默认使用数据库
    url: jdbc:h2:mem:inbox_msg
    # 用户名
    username: sa
    # 密码
    password: sa
    driverClassName: org.h2.Driver
  sql:
    init:
      platform: h2
      # 项目启动后自动执行的DDL语句
      schema-locations: classpath:db/schema.sql
      # 项目启动后自动执行的DML语句
      data-locations: classpath:db/data.sql
  h2:
    console:
      # 是否开启UI控制台
      enabled: true
      # 控制台地址
      path: /h2
      settings:
        trace: false
        # 是否允许远程连接,false表示只能本机连接
        web-allow-others: false

控制台展示:

在这里插入图片描述

执行SQL语句
在这里插入图片描述

完整源码:https://gitee.com/hongqianli/inbox-demo

更多推荐