|
| 1 | +# Spring Boot with JPA |
| 2 | + |
| 3 | +The exercises in this project are designed to help you understand integrating |
| 4 | +JPA with Spring Boot to create a complete database-backed REST API. It will |
| 5 | +also demonstrate a complete Spring Boot REST API application that uses JPA to |
| 6 | +interact with a mysql database. |
| 7 | + |
| 8 | +## Getting Started |
| 9 | + |
| 10 | +To get started, open the project file located in `jpa-in-spring-boot/pom.xml` |
| 11 | +in IntelliJ. |
| 12 | + |
| 13 | +## Project Overview |
| 14 | + |
| 15 | +This project contains a Spring Boot application with the necessary dependencies |
| 16 | +to create a REST API backed by a mysql database. |
| 17 | + |
| 18 | +You will be implementing most of the application yourself in this project |
| 19 | +using both the knowledge that you have gained from the previous exercises and |
| 20 | +the JPA skills that you learned throughout this lesson. |
| 21 | + |
| 22 | +## Exercise |
| 23 | + |
| 24 | +This exercise has many steps and will take some time to complete. This readme |
| 25 | +will guide you through the steps to complete the exercise. |
| 26 | + |
| 27 | +### Step 1: Create the Database |
| 28 | + |
| 29 | +The first step is to create the database that the application will use. You |
| 30 | +will need to open up mysql workbench, and then choose `File -> Open SQL Script` |
| 31 | +and open the file located in `jpa-in-spring-boot/sql/create-database.sql`. |
| 32 | + |
| 33 | +This script contains the SQL commands to create the database and the tables |
| 34 | +that the application will use, as well as some initial data to populate the |
| 35 | +tables. You should review this script and then run it to create the database. |
| 36 | + |
| 37 | +To verify that the database was created correctly, you can open a new query |
| 38 | +tab in mysql workbench and run the following SQL command: |
| 39 | + |
| 40 | +```sql |
| 41 | +select * from todo_lists; |
| 42 | +``` |
| 43 | + |
| 44 | +This should return a list of todo lists that were created by the script. |
| 45 | + |
| 46 | +### Step 2: Open and Review the Spring Boot Application |
| 47 | + |
| 48 | +Next, you should open the project located in `jpa-in-spring-boot/pom.xml` |
| 49 | +in IntelliJ and review the code that it current contains: |
| 50 | + |
| 51 | +- `src/main/java/org.example/SpringBootApplication.java` - This is the main |
| 52 | + class for the Spring Boot application. This is a very short, standard |
| 53 | + spring boot application class that starts up a Spring Boot application. |
| 54 | + You will not need to modify this class. |
| 55 | +- `pom.xml` - This is the project file for the Spring Boot application. Most |
| 56 | + notably, it contains the mysql driver and the JPA dependencies that allow |
| 57 | + the application to connect to the database and use JPA to interact with it. |
| 58 | + This dependency also requires that you have a `application.properties` file |
| 59 | + with the database connection information properly configured. |
| 60 | +- `src/main/resources/application.properties` - This file contains the |
| 61 | + configuration for the database connection. You will need to update the |
| 62 | + `spring.datasource.username`, and `spring.datasource.password` properties to |
| 63 | + match the username and password that you use to connect to your mysq server. |
| 64 | + |
| 65 | +With this configured properly, you should be able to run the application by |
| 66 | +right-clicking on the `SpringBootApplication` class and selecting `Run`. |
| 67 | + |
| 68 | +The REST endpoints do not respond properly yet, so you will not be able to |
| 69 | +test them yet, but you should see that the application starts up without any |
| 70 | +errors and that the endpoints are registered and responding with 404 errors. |
| 71 | + |
| 72 | +### Step 3: Populate the Model Classes |
| 73 | + |
| 74 | +The next step is to complete the model classes that will represent the data |
| 75 | +from the database. The model classes are included with this project, but they |
| 76 | +are curently empty. You will need to add the following items to each of the |
| 77 | +model classes: |
| 78 | + |
| 79 | +- private fields for each column in the table |
| 80 | +- a no-argument constructor |
| 81 | +- a constructor that takes all of the fields as arguments and sets them |
| 82 | +- getters and setters for each field |
| 83 | + |
| 84 | +### Step 4: Populate the Controller Classes |
| 85 | + |
| 86 | +The next step is to create the controller classes that will handle the REST |
| 87 | +endpoints for the application. You will need to create a controller class |
| 88 | +for each of the two model class that you created in the previous step. |
| 89 | + |
| 90 | +Each controller class should have methods for each of the following REST |
| 91 | +endpoints: |
| 92 | + |
| 93 | +- `GET /` - This should return a list of all of the items in the table |
| 94 | +- `GET /{id}` - This should return a single item from the table with the |
| 95 | + specified id |
| 96 | +- `POST /` - This should create a new item in the table |
| 97 | +- `PUT /{id}` - This should update an existing item in the table |
| 98 | +- `DELETE /{id}` - This should delete an existing item in the table |
| 99 | + |
| 100 | +The controller classes are already created with stub methods for each of these |
| 101 | +endpoints. You will need to implement the methods to interact with the |
| 102 | +database using JPA through the included `jdbcTemplate` member of the class. |
| 103 | +You will need to execute the appropriate SQL queries and then map the results |
| 104 | +to the model classes that you created in the previous step. |
| 105 | + |
| 106 | +## Testing / Verification |
| 107 | + |
| 108 | +You can verify that the application is working correctly by running the |
| 109 | +application and then launching Postman to test the REST endpoints. You should |
| 110 | +be able to create, read, update, and delete items in the database using the |
| 111 | +REST endpoints that you created. |
| 112 | + |
0 commit comments