Spring Boot Project using Kafka and R2DBC (Part III )
Now, we can create the entities classes, repositories and persistence adapters for the application.
Entity Classes
The PersonEntity class:
The GenderData enum:
The MeasurementEntity class:
As you can see, there is an id for MeasurementEntity instead of a composite key. This was a workaround because the R2DBC doesn’t yet support composite key.
The MeasurementTypeData enum:
The MeasurementClassificationData enum:
Mapper Classes
We need classes to map database information to domain classes and vice-versa, so we can create mapper classes and for that we will use the ModelMapper framework:
The PersonPersistenceMapper will be something like that:
The MeasurementPersistenceMapper will be something like that:
Repositories
Now, let’s create the repositories:
The PersonRepository interface:
The MeasurementRepository interface:
Service Classes
We could use repositories directly in adapter classes, but I think would be better to isolate these classes using an intermediate service layer like the following example:
The SavePersonPersistenceService class:
The SaveMeasurementService class:
Adapter Classes
Finally, we are able to create the persistence adapters that will implement the ports:
The SavePersonPersistenceAdapter:
And SaveMeasurementPersistenceAdapter:
SQL Scripts
If you want to write unit tests for R2DC you can follow these instructions:
First of all, we need to create a DDL script because R2DB isn’t able to generate schemas automatically.
The schema.sql file:
And the data.sql file:
After that, just create a configuration class in test folder classes: