繁体   English   中英

如何在Spring Boot中启用H2数据库服务器模式

[英]How to enable H2 Database Server Mode in Spring Boot

我正在使用带有Spring Boot文件的H2数据库。

在我的application.properties中,我有以下条目:

spring.datasource.url = jdbc:h2:file:c:/ Testprojekte / spring-boot-h2-db

但是现在我希望能够在运行应用程序时查看数据库,目前这是不可能的,因为我需要使数据库以服务器模式运行。 在文档中,我发现必须将AUTO_SERVER = TRUE添加到URL,但这不能解决问题。

那么,我必须更改什么才能能够同时从不同进程连接到该数据库?

谢谢你的帮助! 索斯滕

您可以使h2 Web控制台使用浏览器中的Web界面访问内存或文件数据库中的h2。

为此,在application.properties中添加以下行:

spring.h2.console.enabled=true
spring.h2.console.path=/h2-console

之后,重新启动Spring Boot应用程序,并使用浏览器检查http://localhost:8080/h2-console

您可以将H2 TCP服务器作为Bean启动:

<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <!-- <scope>runtime</scope> -->
</dependency>
@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Bean(initMethod = "start", destroyMethod = "stop")
    public Server h2Server() throws SQLException {
        return Server.createTcpServer("-tcp", "-tcpAllowOthers", "-tcpPort", "9092");
    }
}

然后使用以下参数从您的IDE连接到它(密码-空):

url: jdbc:h2:tcp://localhost:9092/mem:testdb
user: sa

更多信息在这里这里

我们只需要在application.properties文件中进行以下配置。

spring.h2.console.enabled=true

默认情况下,h2将在http://localhost:8080/h2-console/

但是可以在application.properties中定义spring.h2.console.path=/h2 ,然后可以使用http://localhost:8080/h2访问http://localhost:8080/h2

现在,如果您已在应用程序中实现了SecurityConfig,则需要添加

.and().csrf().ignoringAntMatchers("/h2/**") // Make H2-Console non-secured; for debug purposes
.and().headers().frameOptions().sameOrigin() // Allow pages to be loaded in frames from the same origin; needed for H2-Console

http.authorizeRequests()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM