Spring Boot FAQ: Top Questions
14. How do you connect a Spring Boot application to a database?
Spring Boot makes it easy to connect to a database using Spring Data JPA. You just configure the datasource properties and include the appropriate starter.
πΊοΈ Steps:
- Add
spring-boot-starter-data-jpa
dependency. - Set datasource properties in
application.properties
. - Create an entity and repository interface.
π₯ Example:
spring.datasource.url=jdbc:h2:mem:test
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
π Expected Output:
Database auto-configured and ready for use via repository interfaces.
π οΈ Use Cases:
- Rapid database application setup.
- Using in-memory databases for testing.