Documente Academic
Documente Profesional
Documente Cultură
----------------------------------------------------------------------------
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.
--code------------------------------------
Sort s=Sort.by("prodCost");
List<Product> list=repo.findAll(s);
list.forEach(System.out::println);
-------------------------------------------------
--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.
---code---------------
//Product p=new Product(null, "DRF", 55.69);
Product p=new Product(null, null, 55.69);
Example<Product> ex=Example.of(p);
5. findAll(Pageable p):Page<T>
--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;
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 {
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);
*/
/*
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