“Spring Boot 集成 H2”的版本间的差异

来自姬鸿昌的知识库
跳到导航 跳到搜索
(建立内容为“1”的新页面)
 
 
(未显示同一用户的5个中间版本)
第1行: 第1行:
1
+
=== 添加依赖 ===
 +
<syntaxhighlight lang="xml">
 +
    <parent>
 +
        <groupId>org.springframework.boot</groupId>
 +
        <artifactId>spring-boot-starter-parent</artifactId>
 +
        <version>2.4.13</version>
 +
    </parent>
 +
 
 +
    <dependencies>
 +
 
 +
        <dependency>
 +
            <groupId>com.h2database</groupId>
 +
            <artifactId>h2</artifactId>
 +
        </dependency>
 +
 
 +
    </dependencies>
 +
</syntaxhighlight>
 +
 
 +
 
 +
 
 +
 
 +
 
 +
=== 配置 ===
 +
<syntaxhighlight lang="yaml">
 +
spring:
 +
  datasource:
 +
    url: jdbc:h2:mem:testdb
 +
    driver-class-name: org.h2.Driver
 +
    schema: classpath:db/schema-h2.sql
 +
    username: root
 +
    password: test
 +
  h2:
 +
    console:
 +
      enabled: true
 +
      path: /h2-console
 +
</syntaxhighlight>默认还是使用了 Hikari 作为数据库连接池;
 +
 
 +
H2 可以选择将数据存储在内存还是磁盘上,用 <code>spring.datasource.url</code> 配置:
 +
 
 +
* <code>jdbc:h2:mem:testdb</code> 这种就是内存,然后库名是 testdb
 +
 
 +
* <code>jdbc:h2:file:/path/to/db/file</code> 这种就是文件
 +
 
 +
和 Spring Boot 内置的 Web 容器一起用的时候,可以使用 H2 的 Web 控制台,但需要先启用:
 +
 
 +
<code>spring.h2.console.enabled=true</code>,否则访问就会 404
 +
 
 +
<code>spring.h2.console.path</code> 配置控制台,默认就是 /h2-console
 +
 
 +
 
 +
 
 +
其他常见配置:
 +
 
 +
https://docs.spring.io/spring-boot/docs/2.4.13/reference/html/appendix-application-properties.html#spring.h2.console.enabled

2023年2月17日 (五) 01:41的最新版本

添加依赖

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.13</version>
    </parent>

    <dependencies>

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

    </dependencies>



配置

spring:
  datasource:
    url: jdbc:h2:mem:testdb
    driver-class-name: org.h2.Driver
    schema: classpath:db/schema-h2.sql
    username: root
    password: test
  h2:
    console:
      enabled: true
      path: /h2-console

默认还是使用了 Hikari 作为数据库连接池;

H2 可以选择将数据存储在内存还是磁盘上,用 spring.datasource.url 配置:

  • jdbc:h2:mem:testdb 这种就是内存,然后库名是 testdb
  • jdbc:h2:file:/path/to/db/file 这种就是文件

和 Spring Boot 内置的 Web 容器一起用的时候,可以使用 H2 的 Web 控制台,但需要先启用:

spring.h2.console.enabled=true,否则访问就会 404

spring.h2.console.path 配置控制台,默认就是 /h2-console


其他常见配置:

https://docs.spring.io/spring-boot/docs/2.4.13/reference/html/appendix-application-properties.html#spring.h2.console.enabled