Giriş
Şu satırı dahil ederiz.
Şu satırı dahil ederiz.
import org.springframework.data.domain.Pageable;
Pageable nesnesi repository nesneler ile birlikte kullanılır. Açıklaması şöyle. Pageable Arayüzü metoda parametre olarak verilir. Metod Page nesnesi döner.
Problem
- Typically OLTP users do not consume large amount of data so it is wastage of processing resources.
- Severe system performance issue can arise depending on the volume of data
Solution :
- Paginate results
- Narrow down search criteria
Örnek
Metod şöyledir
@GetMapping(value = "/fetchCustomerByLastNamePagination")public ResponseEntity<Page<Customer>> fetchCustomerByLastNamePagination(@RequestParam String lastName, int pageId, int pageSize) throws RuntimeException {return new ResponseEntity<>(customerService.findCustomerByLastNamePagination(lastName, pageId, pageSize), HttpStatus.OK);}-- Requesthttp://localhost:8081/springreadyapp/fetchCustomerByLastNamePagination?lastName=Karim&pageId=0&pageSize=2-- Generated queryHibernate:selectcustomer0_.id as id1_0_,customer0_.first_name as first_na2_0_,customer0_.gender as gender3_0_,customer0_.last_name as last_nam4_0_,customer0_.mobile_number as mobile_n5_0_,customer0_.present_address as present_6_0_,customer0_.permanent_address as permanen7_0_fromcustomer customer0_wherecustomer0_.last_name=? limit ?Hibernate:selectcount(customer0_.id) as col_0_0_fromcustomer customer0_wherecustomer0_.last_name=?
Çıktı şöyledir
-- Response{"content": [{"id": 1,"firstName": "Sajedul","lastName": "Karim","mobileNumber": "01737186095","presentAddress": "Dhaka","permanentAddress": "Nilphamari","gender": "Male"},{"id": 2,"firstName": "Sajedul","lastName": "Karim","mobileNumber": "01737186095","presentAddress": "Dhaka","permanentAddress": "Nilphamari","gender": "Male"}],"pageable": {"sort": {"sorted": false,"unsorted": true,"empty": true},"offset": 0,"pageSize": 2,"pageNumber": 0,"paged": true,"unpaged": false},"totalPages": 21,"totalElements": 41,"last": false,"numberOfElements": 2,"first": true,"size": 2,"number": 0,"sort": {"sorted": false,"unsorted": true,"empty": true},"empty": false}
Örnek
Elimizde şöyle bir kod olsun
@Data@Document(collection = "tweets")public class Tweet {private ObjectId id;private String author;private String content;private Long createdAt;}public interface TweetRepository extends MongoRepository<Tweet, ObjectId> {}@GetMapping(value = "/paged")public Page<Tweet> getPaged(Pageable pageable) {return tweetRepository.findAll(pageable);}
Şöyle yaparız. Burada Spring akıllı ve URL parametrelerinden bir tane Pageable nesnesi yarattı.
http://myapi.com/tweets?size=10&page=1
Örnek
public Optional<Page<User>> getAllByPage(Integer pageSize, Integer page) {
Pageable pageable = PageRequest.of(page, pageSize);
return userRepository.findAll(pageable);
}
ÖrnekŞöyle yaparız.
Cacheable(key = "#pageable.pageNumber")
public Person getPersons(Pageable pageable)
Hiç yorum yok:
Yorum Gönder