【问题标题】:Connecting to H2 database from H2 Console从 H2 控制台连接到 H2 数据库
【发布时间】:2017-07-28 20:59:19
【问题描述】:

我在我的应用程序属性文件中使用了以下设置。但仍然无法在 h2 控制台中看到我的表格。

application.properties

logging.level.org.springframework.web=INFO
spring.datasource.url=jdbc:h2:mem:challenge;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE;MV_STORE=FALSE
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.schema=classpath:/schema.sql
spring.datasource.data=classpath:/data.sql
spring.h2.console.enabled=true
spring.h2.console.path=/h2
server.port = 8080

我在 H2 控制台的 JDBC URL 中使用字符串 jdbc:h2:mem:challenge;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE;MV_STORE=FALSE 登录。但是我没有看到任何表格

以下是我的 Gradle 文件

buildscript {
    ext {
        springBootVersion = '1.4.4.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'

jar {
    baseName = 'challenge'
    version = '0.0.1-SNAPSHOT'
}

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}


dependencies {
    compile('org.springframework.boot:spring-boot-starter-jdbc')
    compile('org.springframework.boot:spring-boot-starter-security')
    compile('org.springframework.boot:spring-boot-starter-web')
    runtime('org.springframework.boot:spring-boot-devtools')
    runtime('com.h2database:h2')
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

以下是为我的 Spring Boot 应用实现 Springsecurity 的类

@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {

     http.authorizeRequests().antMatchers("/h2/*").permitAll();


        http.csrf().disable();
        http.headers().frameOptions().disable();


    }
}

【问题讨论】:

  • 感谢您的 cmets。但是我确实阅读了所有其他帖子,它们都没有解决我的问题。我已经启动了我的 Spring-boot 应用程序,它基本上是一个连接到 H2 数据库并获取所有信息的 Web 服务。网络服务正在显示信息,但是当我登录 H2 控制台时,我看不到任何表或数据库
  • 请出示你的 pom 或 gradle
  • @georgesvan : 更新了我的 gradle 文件
  • @Vinayak 请参阅下面的答案 - 似乎在创建 H2 内存数据库时,您在属性文件中提供的数据库名称将被忽略,默认情况下命名为 testdb。当 Spring 应用启动时,URL 和用户名应该输出到控制台。

标签: spring spring-boot h2


【解决方案1】:

H2 引擎提供了一个控制台,您可以在其中查看所有表格及其数据。此控制台是一个 Web 应用程序。因此,您需要访问 H2 控制台是将 spring-boot-starter-web pom 依赖项包含到您的 pom.xml 中。

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

您还需要将以下属性添加到src/main/resources/application.properties 文件中。

spring.h2.console.enabled=true

H2 web 控制台可以在这里访问(默认链接):http://localhost:8080/h2-console

您应该会看到 Driver 类、JDBC URL 和 证书。如果 JDBC URL 不同,则将其值修改为 jdbc:h2:mem:yourdbName

现在,如果您的项目中有 spring-boot-starter-security 依赖项,则需要在项目中 SecurityConfig 的配置方法中添加一行,否则您将看到一个空白页面登录H2控制台后:

http.headers().frameOptions().disable();

或者,可以使用以下行:

http.headers().frameOptions().sameOrigin();

【讨论】:

  • 我已经完成了上述操作,包括以下内容: spring.h2.console.enabled=true (Application.properties 文件)在 gradle 中也包含了 spring-boot-starter-web。但我仍然面临这个问题——
  • 首先尝试删除spring security进行测试,看看它是否有效
  • 我已经通过在 Application.properties 文件中包含“security.basic.enabled=false”来禁用 Spring Security,并且我还附上了我的 Spring Security 文件供您参考
  • 我的问题是代码在断点等待时服务器没有响应..
  • 没有时间了,如果代码在断点处停止时 UI 没有响应,则使用 WEB UI。
【解决方案2】:

使用 pre-2019 版本的 H2 数据库依赖项,它将在您每次运行独立应用程序时自动创建数据库。

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

【讨论】:

    【解决方案3】:

    当 Spring Boot 应用程序启动时,您应该会输出类似这样的输出:

    2020-08-12 07:30:10.902  INFO 44404 --- [           main] net.querybuilder4j.Application           : Starting Application on sm7chrisjones with PID 44404 (/Users/chris.jones/repos/qb4j-api/target/classes started by chris.jones in /Users/chris.jones/repos/qb4j-api)
    2020-08-12 07:30:10.905  INFO 44404 --- [           main] net.querybuilder4j.Application           : No active profile set, falling back to default profiles: default
    2020-08-12 07:30:11.906  INFO 44404 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
    2020-08-12 07:30:11.913  INFO 44404 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
    2020-08-12 07:30:11.913  INFO 44404 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.30]
    2020-08-12 07:30:11.985  INFO 44404 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
    2020-08-12 07:30:11.986  INFO 44404 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1032 ms
    2020-08-12 07:30:11.999  INFO 44404 --- [           main] o.s.j.d.e.EmbeddedDatabaseFactory        : Starting embedded database: url='jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=false', username='sa'
    2020-08-12 07:30:12.085  INFO 44404 --- [           main] o.s.b.a.h2.H2ConsoleAutoConfiguration    : H2 console available at '/h2-console'. Database available at 'jdbc:h2:mem:testdb'
    2020-08-12 07:30:12.532  INFO 44404 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
    2020-08-12 07:30:12.681  WARN 44404 --- [           main] ion$DefaultTemplateResolverConfiguration : Cannot find template location: classpath:/templates/ (please add some templates or check your Thymeleaf configuration)
    2020-08-12 07:30:12.801  INFO 44404 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
    2020-08-12 07:30:12.804  INFO 44404 --- [           main] net.querybuilder4j.Application           : Started Application in 2.228 seconds (JVM running for 2.816)
    

    我注意到以下几行:

    2020-08-12 07:30:11.999  INFO 44404 --- [           main] o.s.j.d.e.EmbeddedDatabaseFactory        : Starting embedded database: url='jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=false', username='sa'
    2020-08-12 07:30:12.085  INFO 44404 --- [           main] o.s.b.a.h2.H2ConsoleAutoConfiguration    : H2 console available at '/h2-console'. Database available at 'jdbc:h2:mem:testdb'
    

    我将urlusername 放在H2 控制台中,并能够成功连接。如果没有这个输出,要花很长时间才能弄清楚,因为我在application.properties 中调用了内存数据库myDb,但显然这被忽略了,所有H2 内存数据库都被称为testdb

    我正在使用这些 spring-boot-starter-webh2 maven 依赖项,以防您发现另一个版本的依赖项在应用程序启动时不提供此输出:

    <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>2.2.3.RELEASE</version>
    </dependency>
    
    <!-- https://mvnrepository.com/artifact/com.h2database/h2 -->
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <version>1.4.200</version>
        <scope>runtime</scope>
    </dependency>
    

    【讨论】:

      猜你喜欢
      • 2020-04-12
      • 1970-01-01
      • 2015-03-27
      • 1970-01-01
      • 2014-08-30
      • 2019-01-15
      • 1970-01-01
      • 1970-01-01
      • 2023-02-08
      相关资源
      最近更新 更多