Implementing JpaRepository for CRUD capabilities in your Spring Project
Part 2 in the Full Stack Development Series
In my last tutorial in this series we went over how to connect with your MySQL database. We used the Spring Initializr to create our spring application, created our database, used our spring decorators/annotations to provide metadata about which classes should be used in our database, used the Data Source Explorer to connect our application to our created database, and ran the application to see that our Entity class was being mapped to the database.
In this part we will use a service class and repository interface in order to give our application full CRUD capabilities. CRUD stands for create, read, update, and destroy. These are the four basic operations when talking about persisting data. At the end of the demo your application will have functional methods to be able to do those four operations in your database.
Grab a plate, grab a glass.
You’ll remember from the previous demo we strapped Spring Data JPA to our application giving us access to the Java Persistence Api. This has many tools which can be found here but we just need a few to get our application to have full CRUD capability.
FIRST. We need to understand the flow of our application. Looking ahead in our application we are going to have some sort of user interface such as a create user form. The info a user puts in that form will get passed to our controller. Our controller sends our request to be handled to the service class. Depending on the type of request our service class will utilize our repository. The repository manages the database and sends the info back up the line to the user.
We have already created our database and connected to our application. We will continue with that end of the process and work backwards.
Next would be our repository interface to interact with our database. Create a new package in the main package and call it com.dinner.Athlete.Repository. Inside of this package we will make our class AthleteRepo that implements JpaRepository.