简体   繁体   English

spring jpa存储库在哪里存储数据?

[英]where does spring jpa repository store data?

Does it store it in cache?它是否将其存储在缓存中? I have an application and nowhere in the application.properties are db details mentioned.我有一个应用程序,但在 application.properties 中没有提到数据库详细信息。 I am able to store data and query it via Postman.我能够存储数据并通过 Postman 查询它。

Spring Boot uses auto-configuration to configure persistence based on what dependencies are present on the class path. Spring Boot 使用自动配置根据类路径上存在的依赖项来配置持久性。 For example, If you provide a dependency to spring-boot-starter-data-jpa in pom.xml with no other config, JPA/Hibernate uses an in-memory H2 database by default.例如,如果您在pom.xml提供对spring-boot-starter-data-jpa的依赖,而没有其他配置,则JPA/Hibernate默认使用内存中的H2数据库。 You can make this explicit by adding the following to application.properties :您可以通过将以下内容添加到application.properties来明确这一点:

spring.datasource.url=jdbc:h2:mem:testdb
spring.data.jpa.repositories.bootstrap-mode=default
spring.datasource.driverClassName=org.h2.Driver
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect

By default, the contents of the in-memory H2 database are stored in volatile memory, so will be lost when your application terminates.默认情况下,内存中H2数据库的内容存储在易失性内存中,因此在您的应用程序终止时会丢失。 You can store data to a local file by adding this to application.properties :您可以通过将其添加到application.properties将数据存储到本地文件:

spring.datasource.url=jdbc:h2:file:/path/to/my/data

To view the contents of the H2 database in a console, add the following to application.properties and go to http://localhost:8080/h2-console :要在控制台中查看H2数据库的内容,请将以下内容添加到application.properties并转到 http://localhost:8080/h2-console :

spring.h2.console.enabled=true

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

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