Crud operations using Spring Boot, JPA, RestAPI, Postman and MYSQL

Create, Read, Update and Delete using spring boot, Hibernate, JPA, Postman, and MYSQL. In this tutorial, we will create Restapis to perform crud operations.

This example uses eclipse and Lombok plugins and the project is created by the Spring Initializer tool. Its a maven project and below are the required maven dependencies that need to add to pom.xml.

Application.properties

spring.datasource.url=jdbc:mysql://localhost:3306/crud?createDatabaseIfNotExist=true
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.properties.hibernate.dialect= org.hibernate.dialect.MySQL8Dialect
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=update

server.servlet.context-path=/api
server.port=9092

Maven 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>org.springframework.boot</groupId>
   <artifactId>spring-boot-devtools</artifactId>
   <scope>runtime</scope>
   <optional>true</optional>
  </dependency>
  <dependency>
   <groupId>com.mysql</groupId>
   <artifactId>mysql-connector-j</artifactId>
   <scope>runtime</scope>
  </dependency>
  <dependency>
   <groupId>org.projectlombok</groupId>
   <artifactId>lombok</artifactId>
   <optional>true</optional>
  </dependency>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-test</artifactId>
   <scope>test</scope>
  </dependency>

CurdProjectApplication.java

package com.curdProject.CurdProject;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class CurdProjectApplication {

 public static void main(String[] args) {
  SpringApplication.run(CurdProjectApplication.class, args);
 }

}

UserController.java

package com.curdProject.CurdProject.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.curdProject.CurdProject.entity.UserEntity;
import com.curdProject.CurdProject.repository.UserRepository;

@RestController
public class UserController {

 
 @Autowired
 private UserRepository repository;
 
 @PostMapping("/saveUser")
 public UserEntity saveUser(@RequestBody UserEntity user) {
  return repository.save(user);
 }
 
 @GetMapping("/users")
 public List<UserEntity> Users() {
  List<UserEntity> list =  repository.findAll();
  
  return list;
 }
 
 @PutMapping("/updateUser")
 public UserEntity UpdateUser(@RequestBody UserEntity user) {
  UserEntity entity =  repository.saveAndFlush(user);
  
  return entity;
 }
 
 @DeleteMapping("/deleteUser")
 public String deleteUser(@RequestParam long id) {
   repository.deleteById(id);
         return "Deleted";
 }
}

UserEntity

package com.curdProject.CurdProject.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;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@Table(name="User")
@Entity
public class UserEntity {
 
 
 @Id
 @Column(name="ID")
 @GeneratedValue(strategy = GenerationType.IDENTITY)
 private long id;
 
 @Column(name="userName",length = 225)
 private String userName;

}

UserRepository

package com.curdProject.CurdProject.repository;

import org.springframework.data.jpa.repository.JpaRepository;

import com.curdProject.CurdProject.entity.UserEntity;

public interface UserRepository extends JpaRepository<UserEntity, Long> {

}