h2 database spring boot

  1. Create a new Spring Boot project1. Setting up the H2 Database Dependency in pom.xml:
  2. Open the pom.xml file.
  3. Add the H2 database dependency in the dependencies section.

  4. Configuring the H2 Database in application.properties:

  5. Open the application.properties file.
  6. Configure the H2 database settings including URL, username, and password.

  7. Creating Entity Class and Repository Interface:

  8. Create a Java entity class with annotations like @Entity, @Id, and @GeneratedValue.
  9. Create a repository interface that extends JpaRepository.

  10. Writing Service and Controller Classes:

  11. Create a service class with business logic and annotate it with @Service.
  12. Create a controller class with request mappings and annotate it with @RestController.

  13. Testing the H2 Database:

  14. Run the Spring Boot application.
  15. Access the H2 console using the provided URL.
  16. Verify the data by executing SQL queries.

  17. Using H2 Database in Spring Boot Application:

  18. Use the H2 database in the Spring Boot application to perform CRUD operations.
  19. Utilize the JpaRepository methods for database operations.

  20. Production Considerations:

  21. When moving to production, configure a different database like PostgreSQL or MySQL.
  22. Update the application.properties file with the production database settings.