You are on page 1of 5

Spring Boot Data JPA - findAll methods

----------------------------------------------------------------------------

1. findAll():List<T>
This method is defined in JpaRepository(I).
It is used to fetch all rows from DB table as they are provided.
No.of rows => No.of Object => Stored in List

----code----------
List<Product> list=repo.findAll();
list.forEach(System.out::println);

------------------------------------------------------------

2. findAll(Sort):List<T>
This method is defined in JpaRepository(I).
It is used to fetch all rows from DB table in Sorting order.

Sort object creation: [using static method by() ]


Sort s = Sort.by("variableName");

--code------------------------------------
Sort s=Sort.by("prodCost");
List<Product> list=repo.findAll(s);
list.forEach(System.out::println);
-------------------------------------------------

=> one enum is provided 'Direction'


with 2 values: ASC, DESC
=> Default Direction order is ASC.

Sort Object with Direction creation:


Sort s = Sort.by(Direction d, String variable);

--code------------------------
Sort s=Sort.by(Direction.DESC,"prodCost");
List<Product> list=repo.findAll(s);
list.forEach(System.out::println);
-----------------------------------------

3. findAllById(Iterable<ID> ids):List<T>
This method is defined in JpaRepository(I).
It is used to fetch rows based on ID(PrimaryKey)
It behaves like in operator.
=> it is used to fetch random rows

----code-------------
//SQL: select * from prodtab where pid in (10,13,15);
List<Integer> ids= Arrays.asList(10,13,15);
List<Product> list= repo.findAllById(ids);
list.forEach(System.out::println);
----------------------------------

4. findAll(Example<T> ob):List<T>
Example Object is used to hold Model class object
which is comapred with all DB rows (only non-null values)
if matching then those are selected.

Example is used to construct where clause by taking non-null


values from given model class object input.

---code---------------
//Product p=new Product(null, "DRF", 55.69);
Product p=new Product(null, null, 55.69);
Example<Product> ex=Example.of(p);

//select * from prodtab where pcost=55.69


List<Product> list= repo.findAll(ex);
list.forEach(System.out::println);
---------------------

5. findAll(Pageable p):Page<T>

This method is used to fetch data based on Pagination concept.


That means devide large table data into multiple pages.
Based on Page number and size data is provided.

pageSize = n. Then table rows are provided n rows as one page.


ex: size=3 then 3 rows are one page.

Every page gets one number.(Page number starts from zero).

** Pageable object ( pageNum, pageSize)


Pageable p=PageRequest.of(pageNum, pageSize);

--code--------------
Pageable p=PageRequest.of(1, 3);
Page<Product> list= repo.findAll(p);
list.forEach(System.out::println);
--------------------
*******************************************************************************
Spring Boot Data JPA - Bulk INSERT
*******************************************************************************
We can use repo.save() method that takes one object as input and stores in
DB table. if we want to insert multiple objects at a time then use method

saveAll(Iterable<T> objs):List<T>

=> For Iterable pass any collection type. Here, saveAll takes multiple
objects as Collection data and insert them into DB table.

---code-------------------------
repo.saveAll(
Arrays.asList(
new Product(18,"HH", 88.80),
new Product(19,"YY", 98.80),
new Product(20,"PP", 58.80)
)
);
----code---------------------
Product p1=new Product(21, "TYR", 96.35);
Product p2=new Product(22, "HFR", 86.35);
Product p3=new Product(23, "LOR", 76.35);

List<Product> list=Arrays.asList(p1,p2,p3);
repo.saveAll(list);
-----------------------------------
_______________________________________________________________________________
1. Model class
package in.nit.model;

import javax.persistence.Entity;
import javax.persistence.Id;

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

@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
public class Product {
@Id
private Integer prodId;
private String prodCode;
private Double prodCost;

2. Repository interface
package in.nit.repo;

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

import in.nit.model.Product;

public interface ProductRepository extends JpaRepository<Product, Integer>{

3. Runner class
package in.nit.runner;

import java.util.Arrays;
import java.util.List;

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

import in.nit.model.Product;
import in.nit.repo.ProductRepository;

@Component
public class ProductRunner implements CommandLineRunner {
@Autowired
private ProductRepository repo;

@Override
public void run(String... args) throws Exception {

Product p1=new Product(21, "TYR", 96.35);


Product p2=new Product(22, "HFR", 86.35);
Product p3=new Product(23, "LOR", 76.35);

List<Product> list=Arrays.asList(p1,p2,p3);
repo.saveAll(list);

System.out.println("--done--");

/*
List<Product> list=repo.findAll();
list.forEach(System.out::println);
*/
/*
Sort s=Sort.by("prodCost");
List<Product> list=repo.findAll(s);
list.forEach(System.out::println);
*/

/*
Sort s=Sort.by(Direction.DESC,"prodCost");
List<Product> list=repo.findAll(s);
list.forEach(System.out::println);
*/

//select rows based on Id (like in operator)


/*List<Integer> ids= Arrays.asList(10,13,15);
List<Product> list= repo.findAllById(ids);
list.forEach(System.out::println);
*/

//Product p=new Product(null, "DRF", 55.69);


/*
Product p=new Product(null, null, 55.69);
Example<Product> ex=Example.of(p);

//select * from prodtab where pcost=55.69


List<Product> list= repo.findAll(ex);
list.forEach(System.out::println);
*/

/*
Pageable p=PageRequest.of(1, 3);
Page<Product> list= repo.findAll(p);
list.forEach(System.out::println);
*/
}

4. application.properties
## External DB Connection(Datasource) details ##
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/boot
spring.datasource.username=root
spring.datasource.password=root

## Hibernate Specific properties ##


spring.jpa.properties.dialect=org.hibernate.dialect.MySQL55Dialect
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.hibernate.ddl-auto=update
=============================================================================

You might also like