您的位置:首页 > 编程语言 > Java开发

SpringBoot默认的H2数据库如何查看?

2017-01-11 16:31 489 查看
如果大家用SpringBoot的话,如果在pom.xml文件里面加入了H2 数据库的引用的话,其将会把数据持久化到H2 内存数据库中。

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


那么,我们如何查看其被持久化到H2数据库中的数据,且不需要安装第三发的软件?

首先,我们需要知道H2数据的连接的URL,默认情况下,SpringBoot的Hibernate打印的是Info级别的信息,其是查看不到H2数据连接相关的信息。所以首先需要把SpringBoot的Hibernate的debug信息打开。方式很简单,创建一个application.properties文件,然后在其文件里面添加下面一行。

logging.level.org.hibernate=DEBUG




添加之后,就可以启动SpringBoot,(注,笔者的开发环境是STS),这样就能看到打印出的log信息了。

2017-01-11 16:15:19.868 DEBUG 8340 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : PersistenceUnitInfo [
name: default
persistence provider classname: null
classloader: sun.misc.Launcher$AppClassLoader@764c12b6
excludeUnlistedClasses: true
JTA datasource: null
Non JTA datasource: org.apache.tomcat.jdbc.pool.DataSource@2e554a3b{ConnectionPool[defaultAutoCommit=null; defaultReadOnly=null; defaultTransactionIsolation=-1; defaultCatalog=null; driverClassName=org.h2.Driver; maxActive=100; maxIdle=100; minIdle=10; initialSize=10; maxWait=30000; testOnBorrow=true; testOnReturn=false; timeBetweenEvictionRunsMillis=5000; numTestsPerEvictionRun=0; minEvictableIdleTimeMillis=60000; testWhileIdle=false; testOnConnect=false; password=********; url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE; username=sa; validationQuery=SELECT 1; validationQueryTimeout=-1; validatorClassName=null; validationInterval=3000; accessToUnderlyingConnectionAllowed=true; removeAbandoned=false; removeAbandonedTimeout=60; logAbandoned=false; connectionProperties=null; initSQL=null; jdbcInterceptors=null; jmxEnabled=true; fairQueue=true; useEquals=true; abandonWhenPercentageFull=0; maxAge=0; useLock=false; dataSource=null; dataSourceJNDI=null; suspectTimeout=0; alternateUsernameAllowed=false; commitOnReturn=false; rollbackOnReturn=false; useDisposableConnectionFacade=true; logValidationErrors=false; propagateInterruptState=false; ignoreExceptionOnPreLoad=false; }
Transaction type: RESOURCE_LOCAL
PU root URL: file:/D:/4.Trainning/Springboot/gs-accessing-data-jpa-master/gs-accessing-data-jpa-master/complete/target/classes/
Shared Cache Mode: UNSPECIFIED
Validation Mode: AUTO
Jar files URLs []
Managed classes names [
hello.Customer]
Mapping files names []
Properties []


从上面的debug的日志信息,我可以知道,其H2的连接的URL为jdbc:h2:mem:testdb,默认的用户名为sa,密码为空。

然后,打开浏览器,输入http://localhost:8080/h2-console/



输入相应的信息,如上。然进入,就能看到刚刚的SpringBoot创建的信息的了。



如果想自己制定H2的web Console,可以用下面的配置文件。

import java.sql.SQLException;

import org.h2.tools.Server;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

@Configuration
//@Profile("dev") // Only activate this in the "dev" profile
public class H2ServerConfiguration {

// TCP port for remote connections, default 9092
@Value("${h2.tcp.port:9092}")
private String h2TcpPort;

// Web port, default 8082
@Value("${h2.web.port:8082}")
private String h2WebPort;

@Bean
@ConditionalOnExpression("${h2.tcp.enabled:false}")
public Server h2TcpServer() throws SQLException {
return Server.createTcpServer("-tcp", "-tcpAllowOthers", "-tcpPort", h2TcpPort).start();
}

@Bean
@ConditionalOnExpression("${h2.web.enabled:true}")
public Server h2WebServer() throws SQLException {
return Server.createWebServer("-web", "-webAllowOthers", "-webPort", h2WebPort).start();
}

}

然后输入http://localhost:8082/ 就能登录到H2的数据库管理平台了。



具体的项目的代码地址,请到Github上下载:https://github.com/spring-guides/gs-accessing-data-jpa
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息