MySQL JPA Implementation
This blog will help to understand main Spring boot and JPA Hibernate annotations. There will be a detailed discussion over Spring Boot + JPA + MySQL integration and how it works with RESTFul web services.
This article will help to learn, revise and prepare for interview on how Spring Boot and JPA actually works step by Step.
Prerequisite but not mandatory, It require Java and MySQL setup before start implementation.
Below is the Spring Boot and JPA project code structure which we are going to build as part of this article. We will be making changes inside highlighted package and files.
As part of this article we will be using Spring tool kit and to download and setup Spring Tool Kit please click on this link.
Follow below steps to create our very first Spring application project.
- Open Spring Tool kit we installed here.
- Click on File in top navigation menu and than click on new. There will be an option Spring Starter Project start appearing once we click on create new option.
Below window will start appearing.
Provide below highlighted value for Group and Artifact for the project and click on Next.
3. In the below screen select open Web option and select Spring Web. Click on Finish will create Spring project.
4. Create User.java class as mentioned in below screenshot with id and name variable with setter getter.
User class having @Entity and @Table annotations to map the User class with user table in MySQL database.
@Entity annotation will mark class as an Entity.
@Table annotation will map the class with provided table. name attribute can be use to map class name with database table name. It will by default take table name as class name if not provided.
@Id annotation is an Entity identifier.
@GeneratedValue use to generate value. This mainly helps to automatically create unique primary key.
5. Create UserRepository.java class which extends JpaRepository interface which contains all CRUD operations.
@Repository annotation provides the mechanism to perform all CRUD operations on objects.
6. Create one more class UserDao.java with below content
@Service annotation helps us to convert any class into service and all other classes can consume it easily with the help of @Autowired annotation.
repository.findAll() is a in-built function from JpaRepository interface which will help us to get data from User table.
repository.findById(id).get() is also a in-built function from JpaRepository interface which will fetch user record for specific id.
repository.save(user) is also a in-built function from JpaRepository and allows us to save or update user object in MySQL database.
repository.deleteById(id) is also a in-built function from JpaRepository and allows us to delete user object of specific id from MySQL database.
7. Create one more UserController.java class with below content
8. Below class by default get created when we create project using STS or initializr.
9. Update application.properties file to provide details to connect with MySQL database.
10. Lets create a database and insert data into MySQL database using below queries
CREATE TABLE user(id int NOT NULL AUTO_INCREMENT, name varchar(255) NOT NULL, PRIMARY KEY (id));
insert into user values(1, “John”);
11. Select root project folder, right click and select Ran As -> Spring Boot App will start the server.
OUTPUT
Get all Users List
Hitting below URL will return all list of users with below inline output
http://localhost:8080/user
Get User by Id
Hitting below URL will return specific user having id passed as 1
http://localhost:8080/user/1
Create User by Id
For POST or to save data in database we are going to use postman which is chrome plugin. Provide all the inputs highlighted red in color. Click on send will save the data in the database and return response highlighted in green.
It will call below method from UserController.java class:
@PostMapping(path = “/user”)
public ResponseEntity<User> saveUser(@RequestBody User user) {
Update User detail By Id
For PUT or to update data in database, we are going to use postman which is chrome plugin. Provide all the inputs highlighted red in color. Click on send will update the data in the database and return response highlighted in green.
Put will call below method from UserController.java class, pick user id as 2 from URL and data from request body as mentioned in below function definition.
Delete User by Id
To delete user record from database, we are going to use postman which is chrome plugin. Provide all the inputs highlighted red in color. Click on send will delete user of specific Id form the database and return response highlighted in green.
Delete will call below method from UserController.java class, pick user id as 2 from URL:
Imran Khan, Adobe Community Advisor, AEM certified developer and Java Geek, is an experienced AEM developer with over 11 years of expertise in designing and implementing robust web applications. He leverages Adobe Experience Manager, Analytics, and Target to create dynamic digital experiences. Imran possesses extensive expertise in J2EE, Sightly, Struts 2.0, Spring, Hibernate, JPA, React, HTML, jQuery, and JavaScript.