Spring Boot 使用 H2 数据库的控制台(Console)
如果你希望在 Spring Boot 启用 H2 数据库的话,这篇文章适合你进行了解。
概述
在这个指南中,我们将会考虑如何在 Spring Boot 中使用 H2 数据库。
与其他数据库相似,Spring Boot 生态系统中能够完全支持 H2 数据库
依赖(Dependencies)
让我们首先需要定义 H2 数据库依赖:https://search.maven.org/search?q=g:com.h2database
同时我们还需要使用 spring-boot-starter-data-jpa 依赖:https://search.maven.org/search?q=a:spring-boot-starter-data-jpa
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> <version> 2.1 . 4 .RELEASE</version> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> <version> 1.4 . 199 </version> </dependency> |
与其他的 Spring Boot 管理相似,通常你不需要为你使用的 Spring Boot 指定版本号。
数据库配置
在默认的情况下,Spring Boot 将会配置 H2 数据库使用 sa 为用户名,用户名密码为空。
当然你可以可以通过修改 application.properties 文件中配置文件来为你的 H2 数据库指定登录的用户名和密码。
spring.datasource.url=jdbc:h2:mem:testdb spring.datasource.driverClassName=org.h2.Driver spring.datasource.username=sa spring.datasource.password=password spring.jpa.database-platform=org.hibernate.dialect.H2Dialect |
如果你使用的 H2 基于内存的数据库的话,你所有存储在内存中的数据将会在你重新启动服务器的时候完全丢失。
如果你希望避免这个情况的话,你可以使用基于文件存储的数据库,你可以通过下面的参数修改为基于文件使用的数据库,修改 spring.datasource.url: 参数为
spring.datasource.url=jdbc:h2:file:/data/demo |
文件系统使用的路径为绝对路径,有关 H2 数据库配置的方法,请参考 http://www.h2database.com/html/features.html#connection_modes 页面中的内容。
数据库选项
Spring Boot 整合 H2 提供的增删改查(CRUD)与普通数据库的增删改查是相似的。请参考 https://www.baeldung.com/persistence-with-spring-series 页面中获得更多的有关数据持久化相关的信息。
在这里,请将 data.sql 文件添加到 src/main/resources 目录下面:
DROP TABLE IF EXISTS billionaires; CREATE TABLE billionaires ( id INT AUTO_INCREMENT PRIMARY KEY , first_name VARCHAR (250) NOT NULL , last_name VARCHAR (250) NOT NULL , career VARCHAR (250) DEFAULT NULL ); INSERT INTO billionaires (first_name, last_name, career) VALUES ( 'Aliko' , 'Dangote' , 'Billionaire Industrialist' ), ( 'Bill' , 'Gates' , 'Billionaire Tech Entrepreneur' ), ( 'Folrunsho' , 'Alakija' , 'Billionaire Oil Magnate' ); |
Spring Boot 将会在系统应用程序启动后,将会自动选择 data.sql 文件然后在我们的 H2 数据库中进行配置。
这种方式是对使用的数据库进行进行测试的比较好的办法。
访问 H2 控制台
H2 数据库有一个嵌入式 GUI 控制台能够让你对数据库的内容进行查询和运行 SQL。
在默认情况下,H2 的控制台没有嵌入到 Spring 中。所以你需要对这个控制台工具进行启用,请在 application.properties 文件中添加下面的参数:
spring.h2.console.enabled= true |
在启用上面的参数后,如果你启用了 Spring Boot 应用程序,你可以导航到 http://localhost:8080/h2-console界面中,在这个界面中首先将会显示登录界面。
在登录界面中适用的登录信息是你在 application.properties 文件中指定的登录信息。
一旦你成功连接到控制台后,我们将会看到一个完整的控制台界面。
在这个完整的控制台界面中的作出,你将会看到 H2 数据库中所有的数据表,同时还包含有一个文本对话框中包含了可以运行的 SQL 查询:
在这个 Web 的控制台界面中,具有自动完成 SQL 关键字的功能。这个功能能够让给控制台更加轻量的运行并且具有 SQL 关键字帮助的功能,能够让你在文本对话框中直接运行 SQL 脚本。
更进一步,我们将会在 application.properties 中配置更多的参数能够满足你的对当前项目的需求:
spring.h2.console.path=/h2-console spring.h2.console.settings.trace= false spring.h2.console.settings.web-allow-others= false |
在上面的代码中,我们设置了 H2 的控制台访问控制台的 URL 为: /h2-console,这个链接是针对你当前项目运行的服务器地址和端口的相对地址。
例如你当前服务器运行的 URL 为 http://localhost:9001 那么 H2 控制台访问的地址为 http://localhost:9001/h2-console。
同时我们设置了 spring.h2.console.settings.trace 参数为 false,这样我们能够避免在系统控制台中输出 trace 级别的日志信息。
通过设置 spring.h2.console.settings.web-allow-others=false 参数,我们能够禁止远程 Web 访问 H2 数据库的信息。
结论
H2 是完全与 Spring Boot 兼容的,通过这篇文章,你能够了解如何使用 H2 控制台来管理整治运行的数据库。
有关完整的代码情况参考我们提交到 GitHub 中的代码示例:https://github.com/cwiki-us-demo/java-tutorials/tree/master/persistence-modules/spring-boot-persistence-h2-console 中的内容。
相关文章
- STS创建spring boot项目,pom.xml文件第一行报错
- 在Spring Boot中使用内存数据库
- 在Spring Boot使用H2内存数据库
- Spring boot 自定义banner
- Prometheus + Spring Boot 应用监控
- Spring Boot @Async 异步任务执行
- Spring学习【Spring概述】
- Spring Cloud : 如何优雅下线微服务?
- 《深入实践Spring Boot》一1.4 运行与发布
- Is Spring Integration 5.5.10 must be based on Java 9 to use or depend on spring integration? #3761
- Spring Boot 中使用@DateTimeFormat和@JsonFormat注解
- CSDN日报190815:spring boot 项目实战,热门技术干货分享!
- 【Spring Boot 四】启动之准备系统环境environmentPrepared
- Spring Boot 2.4 对多环境配置的支持更改
- Spring Boot 2.x基础教程:使用国产数据库连接池Druid
- Spring Boot(一)之快速入门
- Spring+Quartz实现动态添加定时任务
- Spring Boot 2 实战:使用 Flyway 管理你数据库的版本变更
- Spring Boot 2 实战:根据条件来自动配置不同逻辑的Bean
- 在Spring Boot使用H2内存数据库
- Spring Boot filter
- spring 配置文件 获取变量(PropertyPlaceholderConfigurer)
- Spring Boot用Cxf的jax-ws开发WebService
- Spring IOC(四)总结升华篇
- Spring MVC简单的HelloWorld例子
- Spring Boot—08Jackson处理JSON
- 【转】redis 消息队列发布订阅模式spring boot实现
- Spring Boot应用连接数据库MySQL、及一个简单的demo
- Spring Boot 整合 Cache
- Spring boot实现定时任务二:使用注解@scheduled和@EnableScheduling
- spring boot 2 + shiro 实现简单的身份验证例子
- 曹工谈Spring Boot:Spring boot中怎么进行外部化配置,一不留神摔一跤;一路debug,原来是我太年轻了
- 事物Spring boot @Transactional
- 14 Spring Boot Shiro限制登录尝试次数