Spring Boot Restful CRUD API Tutorial With MySQL, JPA
By AmarSivas | | Updated : 2020-12-24 | Viewed : 146 times

In this quick tutorial, we talk about the rest of API creation for curd operations with MySQL. So be ready for this quick tutorial. Let's create the application and try to understand some of the important components of this application.
Table of Contents:
Creating a Spring Boot application
To create a spring boot application, you can use Spring Initializer
Or you can use the prepared GitHub link for the same. The repo link in GitHub is Spring-Boot-Curd-Example-App
Required Maven dependencies
Here we are using Mysql and Spring Data JPA. So we need to add the maven dependencies for the same. Please find the below given for the same.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
Creating Rest Controller Class
Now we will focus to implement different types of the component to create the curd app for curd operations.
Please see below-given cod snippet example for ProductsController.java
@RestController
@RequestMapping("/curd-app/api")
public class ProductsController {
//..
@GetMapping("/products")
public ResponseEntity<?> getAllProducts() {
return productServiceImpl.getAllProducts();
}
@PostMapping(value = "/products")
public ResponseEntity<?> saveProduct(@RequestBody Product product) {
return productServiceImpl.saveProduct(product);
}
@PutMapping(value = "/products")
public ResponseEntity<?> updateProduct(@RequestBody Product product) {
return productServiceImpl.updateProduct(product);
}
@DeleteMapping(value = "/products")
public ResponseEntity<?> deleteProduct(@RequestBody Product product) {
return productServiceImpl.deleteProduct(product);
}
@PatchMapping(value = "/products")
public ResponseEntity<?>patchProduct(@RequestBody Product product) {
return productServiceImpl.patchProduct(product);
}
}
Here we used given annotations. those are
Note: put and patch HTTP verbs can be used for updating the resources only. But whenever a partial update is required unlike the whole object then we can use the patch.
Creating Service Class
To write the business logic we need one service class. Here is the below-given code snippet for the same.
@Service
public class ProductServiceImpl {
@Autowired
ProductRepository productRepository;
}
Here
Creating Entity Class
Here Entity class will be representing the table in Mysql DB. So we can apply different types of curd operations on this DB using
@Entity
@Table(name = "product",
uniqueConstraints = {
@UniqueConstraint(columnNames = "product_id")
})
@Getter
@Setter
public class Product {
}
For more information about the annotations please find the below-given link Javax-Persistence-API
Creating the ProductRepository.java
Below given snippet for example repository class. Here we used
@Repository
public interface ProductRepository extends JpaRepository<Product, Long>{
}
Here
Creating SpringBootCurdApplication
The main class where the server will be started easily as embedded tomcat server.
@SpringBootApplication
public class SpringBootCurdApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootCurdApplication.class, args);
}
}
Here
Execution and Reponses:
So now the application is ready for execution. For the entire code repository in
Please find the below-given screenshots for the same.




