Implementing CRUD Operations (Create, Read, Update, Delete) in a Spring Boot application involves creating RESTful endpoints for each operation. In this article we will expose separate rest end point for each operation.

As we have learnt in previous article to create sample spring rest end point, and also we have created mysql table using hibernate, We will use same book table for CRUD Operation.

  1. CREATE (C)  :  Create RESTFul api to accept value and store to the table, Will create HTTP Post method to accept value from controller and pass to business class and finally to call data base layer to store the value.

Step 1: Create package with controller with your base package, then create java file BookController.java and then put below code in controller, here we have created addbook() method .

We need book info to be passed so I have created on sample BookVO java class which holds the info which we are going to store to the table.

package com.techbug.techbug.vo;

public class BookVO {
private Long id;
private String name;
private String code;
private String description;

public Long getId() {return id;}
public void setId(Long id) {
   this.id = id;
}
public String getName() {return name;}
public void setName(String name) {
  this.name = name;
}
public String getCode() {return code;}
public void setCode(String code) {
  this.code = code;
}
public String getDescription() {return description;}
public void setDescription(String description) {
  this.description = description;
}
}

 

package com.techbug.techbug.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.techbug.techbug.service.BookService;
import com.techbug.techbug.vo.BookVO;

@RestController
public class BookController {
@Autowired
private BookService bookService;

@PostMapping(value = "/addbook")
public String addBook(@RequestBody BookVO addBookRequest) {
  return bookService.addBook(addBookRequest);
 }
}

 

Step 2: Create Service Package and inside that, create Service file called BookService.java and define addBook() method. addBook() method is getting called from BookController.java there we have added @Autowired BookService bookService which will allow to access all method which is declared BookService class.

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.techbug.techbug.entity.Book;
import com.techbug.techbug.repository.BookRepository;
import com.techbug.techbug.vo.BookVO;

@Service
public class BookService {
@Autowired
private BookRepository bookRepository;

public String addBook(BookVO addBookRequest) {
    Book book = new Book();
    book.setCode(addBookRequest.getCode());
    book.setDescription(addBookRequest.getDescription());
    book.setName(addBookRequest.getName());
    bookRepository.save(book);
    return "Book Added Successfully !!";
 }
}

Step 3: Create an Interface and extend JPARepository inside interface and map table entity where data has to be stored or populated, each repository represents individual table entity.

package com.techbug.techbug.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import com.techbug.techbug.entity.Book;

public interface BookRepository extends JpaRepository<Book, Long>{
}

Step 4: Finally your project will looks like

CRUD Operations

 

Step 5:  Now run as java application and using api validation tool, such as POSTMAN to post the message to new api.

Curd Operation Postman Request

Step 6:  we see success response from api, we should be able to see records inserted after running select query for the table.

Data base

Continue here

Happy Learning !! we will learn about rest operation in next post.

7 Replies to “Spring Boot CRUD Operations”

Leave a Reply

Your email address will not be published. Required fields are marked *