Skip to content

Commit 9ba553b

Browse files
committed
Finished JPA exercise
1 parent 69fc46c commit 9ba553b

File tree

10 files changed

+404
-18
lines changed

10 files changed

+404
-18
lines changed

13-jpa-in-spring-boot/README.md

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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+
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,67 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<project xmlns="http://maven.apache.org/POM/4.0.0"
3-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
54
<modelVersion>4.0.0</modelVersion>
6-
5+
<parent>
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starter-parent</artifactId>
8+
<version>3.3.5</version>
9+
<relativePath/> <!-- lookup parent from repository -->
10+
</parent>
711
<groupId>org.example</groupId>
8-
<artifactId>jpa-in-spring-boot</artifactId>
9-
<version>1.0-SNAPSHOT</version>
10-
12+
<artifactId>spring-boot</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
<name>spring-boot</name>
15+
<description>Demo project for Spring Boot</description>
16+
<url/>
17+
<licenses>
18+
<license/>
19+
</licenses>
20+
<developers>
21+
<developer/>
22+
</developers>
23+
<scm>
24+
<connection/>
25+
<developerConnection/>
26+
<tag/>
27+
<url/>
28+
</scm>
1129
<properties>
12-
<maven.compiler.source>23</maven.compiler.source>
13-
<maven.compiler.target>23</maven.compiler.target>
14-
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
30+
<java.version>17</java.version>
1531
</properties>
32+
<dependencies>
33+
<dependency>
34+
<groupId>org.springframework.boot</groupId>
35+
<artifactId>spring-boot-starter-data-jdbc</artifactId>
36+
</dependency>
37+
<dependency>
38+
<groupId>org.springframework.boot</groupId>
39+
<artifactId>spring-boot-starter-jdbc</artifactId>
40+
</dependency>
41+
<dependency>
42+
<groupId>org.springframework.boot</groupId>
43+
<artifactId>spring-boot-starter-web</artifactId>
44+
</dependency>
45+
46+
<dependency>
47+
<groupId>com.mysql</groupId>
48+
<artifactId>mysql-connector-j</artifactId>
49+
<scope>runtime</scope>
50+
</dependency>
51+
<dependency>
52+
<groupId>org.springframework.boot</groupId>
53+
<artifactId>spring-boot-starter-test</artifactId>
54+
<scope>test</scope>
55+
</dependency>
56+
</dependencies>
57+
58+
<build>
59+
<plugins>
60+
<plugin>
61+
<groupId>org.springframework.boot</groupId>
62+
<artifactId>spring-boot-maven-plugin</artifactId>
63+
</plugin>
64+
</plugins>
65+
</build>
1666

17-
</project>
67+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
create database if not exists todo_lists;
2+
use todo_lists;
3+
4+
drop table if exists todo_lists, todo_list_items;
5+
6+
create table todo_lists (
7+
id int primary key auto_increment,
8+
name varchar(255)
9+
);
10+
11+
create table todo_list_items (
12+
id int primary key auto_increment,
13+
text varchar(255),
14+
todo_list_id int,
15+
completed boolean,
16+
foreign key (todo_list_id) references todo_lists(id)
17+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package org.example.Controllers;
2+
3+
import org.example.Models.ToDoList;
4+
import org.springframework.jdbc.core.JdbcTemplate;
5+
import org.springframework.web.bind.annotation.*;
6+
7+
import javax.sql.DataSource;
8+
import java.util.List;
9+
import java.util.ArrayList;
10+
11+
12+
/**
13+
* The controller for to-do lists.
14+
*/
15+
@RestController
16+
@RequestMapping("/todo-lists")
17+
public class ToDoListController {
18+
/**
19+
* The JDBC template created at construction for querying the database.
20+
*/
21+
private JdbcTemplate jdbcTemplate;
22+
23+
/**
24+
* Creates a new to-do list controller.
25+
*
26+
* @param dataSource The data source for the controller. (Provided by Spring with the
27+
* values from application.properties.)
28+
*/
29+
public ToDoListController(DataSource dataSource) {
30+
jdbcTemplate = new JdbcTemplate(dataSource);
31+
}
32+
33+
/**
34+
* Gets all to-do lists.
35+
*
36+
* @return All to-do lists.
37+
*/
38+
@GetMapping
39+
public List<ToDoList> getToDoLists() {
40+
// TODO: Use the JDBC template to get all to-do lists from the database and map them to ToDoList objects.
41+
return new ArrayList<>();
42+
}
43+
44+
/**
45+
* Gets a to-do list by ID.
46+
*
47+
* @param id The ID of the to-do list.
48+
* @return The to-do list with the given ID.
49+
*/
50+
@GetMapping("/{id}")
51+
public ToDoList getToDoList(@PathVariable int id) {
52+
// TODO: Use the JDBC template to get the to-do list with the given ID from the database and map it to a ToDoList object.
53+
return new ToDoList();
54+
}
55+
56+
/**
57+
* Creates a new to-do list.
58+
*
59+
* @param toDoList The to-do list to create.
60+
*/
61+
@PostMapping
62+
public void createToDoList(@RequestBody ToDoList toDoList) {
63+
// TODO: Use the JDBC template to insert a new to-do list with the given title into the database.
64+
}
65+
66+
/**
67+
* Updates a to-do list.
68+
*
69+
* @param id The ID of the to-do list.
70+
* @param toDoList The updated to-do list.
71+
*/
72+
@PostMapping("/{id}")
73+
public void updateToDoList(@PathVariable int id, @RequestBody ToDoList toDoList) {
74+
// TODO: Use the JDBC template to update the to-do list with the given ID in the database with the given title.
75+
}
76+
77+
/**
78+
* Deletes a to-do list.
79+
*
80+
* @param id The ID of the to-do list.
81+
*/
82+
@PostMapping("/{id}/delete")
83+
public void deleteToDoList(@PathVariable int id) {
84+
// TODO: Use the JDBC template to delete the to-do list with the given ID from the database.
85+
}
86+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package org.example.Controllers;
2+
import org.example.Models.ToDoListItem;
3+
import org.springframework.jdbc.core.JdbcTemplate;
4+
import org.springframework.web.bind.annotation.*;
5+
import org.springframework.web.bind.annotation.RequestMapping;
6+
import org.springframework.web.bind.annotation.RestController;
7+
8+
import javax.sql.DataSource;
9+
import java.util.List;
10+
import java.util.ArrayList;
11+
12+
/**
13+
* The controller for to-do list items.
14+
*/
15+
@RestController
16+
@RequestMapping("/todo-list-items")
17+
public class ToDoListItemController {
18+
private JdbcTemplate jdbcTemplate;
19+
20+
/**
21+
* Creates a new to-do list item controller.
22+
*
23+
* @param dataSource The data source for the controller. (Provided by Spring with the
24+
* values from application.properties.)
25+
*/
26+
public ToDoListItemController(DataSource dataSource) {
27+
jdbcTemplate = new JdbcTemplate(dataSource);
28+
}
29+
30+
/**
31+
* Gets all to-do list items.
32+
*
33+
* @return All to-do list items.
34+
*/
35+
@GetMapping
36+
public List<ToDoListItem> getToDoListItems() {
37+
// TODO: Use the JDBC template to get all to-do list items from the database and map them to ToDoListItem objects.
38+
return new ArrayList<>();
39+
}
40+
41+
/**
42+
* Gets a to-do list item by ID.
43+
*
44+
* @param id The ID of the to-do list item.
45+
* @return The to-do list item with the given ID.
46+
*/
47+
@GetMapping("/{id}")
48+
public ToDoListItem getToDoListItem(@PathVariable int id) {
49+
// TODO: Use the JDBC template to get the to-do list item with the given ID from the database and map it to a ToDoListItem object.
50+
return new ToDoListItem();
51+
}
52+
53+
/**
54+
* Creates a new to-do list item.
55+
*
56+
* @param toDoListItem The to-do list item to create.
57+
*/
58+
@PostMapping
59+
public void createToDoListItem(@RequestBody ToDoListItem toDoListItem) {
60+
// TODO: Use the JDBC template to insert the to-do list item into the database.
61+
}
62+
63+
/**
64+
* Updates a to-do list item.
65+
*
66+
* @param id The ID of the to-do list item.
67+
* @param toDoListItem The updated to-do list item.
68+
*/
69+
@PostMapping("/{id}")
70+
public void updateToDoListItem(@PathVariable int id, @RequestBody ToDoListItem toDoListItem) {
71+
// TODO: Use the JDBC template to update the to-do list item in the database.
72+
}
73+
74+
/**
75+
* Deletes a to-do list item.
76+
*
77+
* @param id The ID of the to-do list item.
78+
*/
79+
@PostMapping("/{id}/delete")
80+
public void deleteToDoListItem(@PathVariable int id) {
81+
// TODO: Use the JDBC template to delete the to-do list item from the database.
82+
}
83+
}

13-jpa-in-spring-boot/jpa-in-spring-boot/src/main/java/org/example/Main.java

-7
This file was deleted.

0 commit comments

Comments
 (0)