【问题标题】:Setting H2 database name in Spring Boot project在 Spring Boot 项目中设置 H2 数据库名称
【发布时间】:2018-03-30 15:54:23
【问题描述】:

我有一个使用 H2 的 Spring Boot 项目。无论我在 application.properties 文件中将数据库名称设置为什么,当我打开 H2 控制台时,数据库的名称都是“test”。我可以登录并查看我的架构,但不能将数据库的名称设置为“test”以外的任何内容。无论我在 application.properties 文件中使用什么名称,H2 控制台上的名称都是“test”。

我的 POM 是

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.example.repository</groupId>
<artifactId>repository</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>repository</name>
<description>repository project for Spring Boot</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.2.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

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

    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>


</project>

我的 application.properties 文件是

spring.datasource.url=jdbc:h2:file:~/test
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driver-class-name=org.h2.Driver

spring.h2.console.path=/h2-console
spring.h2.console.enabled=true
spring.jpa.show-sql=true

server.port=8081

【问题讨论】:

    标签: spring spring-boot h2


    【解决方案1】:

    您需要将“generate-unique-name”设置为 false 并设置名称如下:

    spring.datasource.generate-unique-name=false
    spring.datasource.name=my-new-db-name
    

    【讨论】:

      【解决方案2】:
      • 我尝试了以下设置,它按预期工作:rcp_h2.mv.db 文件已创建。
      • 对于 H2 URL:正如 Anton 建议的那样 - 只需将默认 URL 替换为您的即“jdbc:h2:mem:rcp_h2”,它将被记住,因此下次您不需要这样做。李>

      如果问题仍然存在,您应该尝试更改其他设置,例如:端口号或“spring.h2.console.path=/h2-console”->“spring.h2.console.path=/h2”为查看应用重启后是否生效。如果不是,那么您的项目设置可能有问题。

      spring.datasource.url=jdbc:h2:file:c:/temp/rcp_h2
      spring.datasource.username=sa
      spring.datasource.password=
      spring.datasource.driver-class-name=org.h2.Driver
      
      spring.h2.console.path=/h2-console
      spring.h2.console.enabled=true
      spring.jpa.show-sql=true
      
      server.port=8081
      

      【讨论】:

        【解决方案3】:

        以下是对数据库名称的引用 - file:~/test。您需要将测试更改为其他内容。

        【讨论】:

        • 对不起,我不清楚。无论我将其更改为什么,H2 登录控制台中的 URL 都会显示为“jdbc:h2:~/test”,并且数据库文件的名称将是 test.mv.db 和 test.trace.db。它似乎忽略了 application.properties 中设置的值。
        • H2 控制台它只是一个帮助应用程序,您可以连接到任何 h2 文件基础数据库。基本上毫不犹豫地在控制台中更改 url 值。应用程序属性和 h2 控制台根本没有连接,因此您需要手动更改 url
        猜你喜欢
        • 1970-01-01
        • 2016-05-15
        • 2019-10-21
        • 2020-12-30
        • 1970-01-01
        • 2018-11-18
        • 2019-01-26
        • 2017-09-15
        • 1970-01-01
        相关资源
        最近更新 更多