fankao / eStore

Sample Ecommerce application to practice Modern API Specification and Microservices with Spring Boot inspired on https://github.com/PacktPublishing/Modern-API-Development-with-Spring-and-Spring-Boot
0 stars 0 forks source link

Adding a Repository component #3

Closed fankao closed 2 years ago

fankao commented 2 years ago

We'll use the bottom-to-top approach to add a @Repository component. Let's start implementing the domain layer with a @Repository component. We'll implement the service and enhance the Controller component in subsequent sections accordingly. We will code the @Repository component first, then use it in the @Service component using constructor injection. The @Controller component will be enhanced using the @Service component, which will also be injected into the Controller using constructor injection.

fankao commented 2 years ago

We'll use the following libraries as database dependencies: • H2 database for persisting data: We are going to use H2's memory instance, however, you can also use a file-based instance. • Hibernate Object Relational Mapping (ORM): For database object mapping. • Flyway for database migration: This helps maintain the database and maintains a database changes history that allows rollbacks, version upgrades, and so on. https://github.com/PacktPublishing/Modern-API-Development-with-Spring-and-Spring-Boot/blob/main/Chapter04/build.gradle#:~:text=//%20DB%20Starts,//%20DB%20Ends

fankao commented 2 years ago

Database and JPA configuration

We also need to modify the application.properties file with the following configuration: 1. Data source configuration The following is the Spring data source configuration:

spring.datasource.name=ecomm
spring.datasource.url=jdbc:h2:mem:ecomm;DB_CLOSE_DELAY=-1;IGNORECASE=TRUE;DATABASE_TO_UPPER=false
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=

We need to add H2-specific properties to the data source. The URL value suggests that a memory-based H2 database instance will be used.

2. H2 database configuration The following are the two H2 database configurations:

spring.h2.console.enabled=true
spring.h2.console.settings.web-allow-others=false

The H2 console is enabled for local access only; it means you can access the H2 console only on localhost. Also, remote access is disabled by setting web-allowothers to false.

3. JPA configuration The following are the JPA/Hibernate configurations:

spring.jpa.properties.hibernate.default_schema=ecomm
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.show-sql=true
spring.jpa.format_sql=true
spring.jpa.generate-ddl=false
spring.jpa.hibernate.ddl-auto=none

We don't want to generate the DDL or to process the SQL file, because we want to use Flyway for database migrations. Therefore, generate-ddl is marked with false and ddl-auto is set to none. 4. Flyway configuration The following are the Flyway configurations:

spring.flyway.url=jdbc:h2:mem:ecomm
spring.flyway.schemas=ecomm
spring.flyway.user=sa
spring.flyway.password=
fankao commented 2 years ago

The database and seed data script

https://github.com/PacktPublishing/Modern-API-Development-with-Spring-and-Spring-Boot/blob/main/Chapter04/src/main/resources/db/migration/V1.0.0__Init.sql

fankao commented 2 years ago

Adding entities

https://github.com/PacktPublishing/Modern-API-Development-with-Spring-and-Spring-Boot/tree/main/Chapter04/src/main/java/com/packt/modern/api/entity

fankao commented 2 years ago

Adding repositories

https://github.com/PacktPublishing/Modern-API-Development-with-Spring-and-Spring-Boot/tree/main/Chapter04/src/main/java/com/packt/modern/api/repository