从H2控制台连接到H2数据库

时间:2017-07-28 20:59:20

标签: spring spring-boot h2

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

enter image description here

application.properties

public function getMemberships(){
    $pvListID = [REDACTED];
    $listName = "[REDACTED]";
    $inPVList = false;
    $contactEmail = $this->getRequest()->postVars('contactemail');
    $converted_result ="";
    $pvSubscription = null;
    $return = [];
    $sharpSpringService = null;
    $this->sharpSpringService = new SharpSpringService("[REDACTED", "[REDACTED");

    if($contactEmail != null && $contactEmail !=""){
        $lists = $this->sharpSpringService->makeCall('getListMemberships', [
            'emailAddress' => $contactEmail,
        ]);


        if (count($lists) > 0) {
            $listArray = json_decode(json_encode($lists), true);

            foreach($listArray as $list){

                if($list = $pvListID){
                    //the user is subscribed
                    $inPVList = true;
                    $converted_result = ($inPVList) ? 'true' : 'false';
                }
            }
        }
        $return[] = array(
            "status" => $converted_result,
            "list" => $listName
        );
        return json_encode($return);
    }
    return $return;

}

我在H2控制台的JDBC URL中使用了字符串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 来登录。但我没有看到任何表格

以下是我的Graddle文件

jdbc:h2:mem:challenge;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE;MV_STORE=FALSE

以下是为我的春季启动应用程序实现Springsecurity的类

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')
}

3 个答案:

答案 0 :(得分:3)

H2引擎提供了一个控制台,您可以在其中查看所有表及其数据。此控制台是一个Web应用程序。因此,访问H2控制台所需的是将from django.db.models.signals import post_save from django.dispatch import receiver @receiver(post_save, sender=Person) def populate_parents(sender, instance, created, **kwargs): if created: instance.parentA = instance.id instance.parentB = instance.id instance.save() pom依赖项包含在pom.xml中。

spring-boot-starter-web

您还需要将以下属性添加到<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> 文件中。

src/main/resources/application.properties

可以在此处访问H2 Web控制台(默认链接):http://localhost:8080/h2-console

enter image description here

您应该看到Driver类,JDBC URL和 证书。如果JDBC URL不相同,请将其值修改为spring.h2.console.enabled=true

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

http.headers()frameOptions()禁用();

或者,可以使用以下行:

http.headers()frameOptions()SAMEORIGIN();

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

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控制台中,并能够成功连接。没有此输出,将需要很长时间才能弄清楚,因为我在myDb中调用了内存数据库application.properties,但显然这被忽略了,所有H2内存数据库都称为{ {1}}。

我正在使用这些testdbspring-boot-starter-web的Maven依赖项,以防您发现依赖项的另一个版本确实在应用程序启动时提供了以下输出:

h2