Spring Boot, MySQL, JPA, Hibernate Restful CRUD API Tutorial
Spring Boot, MySQL, JPA, Hibernate Restful CRUD API Tutorial
In this tutorial, we’ll build a Restful CRUD API for a simple user management
application. We’ll first build the APIs to create, retrieve, update and delete a
user, and then test them using postman.
Spring Boot has taken the Spring framework to the next level. It has drastically
reduced the configuration and setup time required for spring projects.
boot-tutorial.html .
You can set up a project with almost zero configuration and start building the
things that actually matter to your application.
If you are new to Spring boot and want to get started with it quickly, then this
blog post is for you.
2. Maven dependencies
Let's define all required maven dependencies in pom.xml file:
<project
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
<relativePath/>
</parent>
<groupId>net.javaguides</groupId>
<artifactId>springboot-crud-restful-webservices</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-crud-restful-webservices</name>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
spring.datasource.url = jdbc:mysql://localhost:3306/usersDB?useSSL=false
spring.datasource.username = root
spring.datasource.password = root
## Hibernate Properties
# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect =
org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.hibernate.ddl-auto = update
id - primary key
firstName - user first name
lastName - user last name
emailId - user email ID
package net.javaguides.springboot.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "users")
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "first_name")
private String firstName;
@Column(name = "last_name")
@Column(name = "email")
public User() {
super();
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
return id;
this.id = id;
return firstName;
return lastName;
this.lastName = lastName;
return email;
this.email = email;
@Table annotation is used to provide the details of the table that this entity
strategy. In the above case, we have declared the primary key to be an Auto
Increment field.
mapped to the annotated field. You can define several properties like name,
length, nullable, updateable, etc.
5. Define UserRepository
Let's create a UserRepository to access User's data from the database.
Well, Spring Data JPA has comes with a JpaRepository interface which defines
methods for all the CRUD operations on the entity, and a default
implementation of JpaRepository called SimpleJpaRepository.
package net.javaguides.springboot.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import net.javaguides.springboot.entity.User;
@Repository
package net.javaguides.springboot.exception;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
@ResponseStatus(value = HttpStatus.NOT_FOUND)
super(message);
package net.javaguides.springboot.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import net.javaguides.springboot.entity.User;
import net.javaguides.springboot.exception.ResourceNotFoundException;
import net.javaguides.springboot.repository.UserRepository;
@RestController
@RequestMapping("/api/users")
@Autowired
@GetMapping
return this.userRepository.findAll();
// get user by id
@GetMapping("/{id}")
return this.userRepository.findById(userId)
}
// create user
@PostMapping
return this.userRepository.save(user);
// update user
@PutMapping("/{id}")
existingUser.setFirstName(user.getFirstName());
existingUser.setLastName(user.getLastName());
existingUser.setEmail(user.getEmail());
return this.userRepository.save(existingUser);
// delete user by id
@DeleteMapping("/{id}")
this.userRepository.delete(existingUser);
return ResponseEntity.ok().build();
}
8. Running the Application
We have successfully developed all the CRUD Rest APIs for the User model.
Now it's time to deploy our application in a servlet container(embedded
tomcat).
1. From the root directory of the application and type the following command
to run it -
$ mvn spring-boot:run
9. Demo
The demo of this tutorial covered in below YouTube video tutorial:
10. Conclusion
Congratulations guys! We successfully built a Restful CRUD API using Spring
Boot, Mysql, JPA, and Hibernate.
You can find the source code for this tutorial on my GitHub repository. Feel
free to clone the repository and build upon it.
Thank you for reading. Please ask any questions in the comment section
below.