Data JPA-findAll Method with Example Class

This method is used to take one input Model class object that will be compared with every row data (only non-null variables) matching records are selected.

Here : Example is used to check and compare non-null variabes,findAll just collecting data from Example.

1. Model
package in.nareshit.raghu.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

public class Book {
	private Integer bookId;
	private String bookName;
	private String author;
	private Double bookCost;
	private String bookType;

2. Repository
package in.nareshit.raghu.repo;
import in.nareshit.raghu.model.Book;

public interface BookRepository extends JpaRepository<Book, Integer> {


4. Runner class
package in.nareshit.raghu.runner;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import in.nareshit.raghu.model.Book;
import in.nareshit.raghu.repo.BookRepository;

public class BookRepoRunner implements CommandLineRunner {

	private BookRepository repo;

	public void run(String... args) throws Exception {
		/* It will store all row at a time */
					new Book(101, "Core Java", "SAM", 300.0, "BackEnd"),
					new Book(102, "Adv Java", "SAM", 400.0, "BackEnd"),
					new Book(103, "Spring Boot", "RAM", 500.0, "BackEnd"),
					new Book(104, "Angular", "RAM", 300.0, "FrontEnd"),
					new Book(105, "HTML", "SAM", 400.0, "FrontEnd"),
					new Book(106, "SQL/PLSQL", "RAM", 300.0, "Database")
		//SQL: select * from book where bookType=? and author=?
		Book b = new Book();
		//convert this into example object
		Example example = Example.of(b);
		//fetch data
		List list = repo.findAll(example);