Spring Boot Tutorial 120 lessons · 2:38:34+ of video
Spring Boot Home What Spring Boot Is, Where Spring Boot Is Used and How a Web App Works
What Spring Boot Is Where Spring Boot Is Used How a Web App Works What You Need, Spring Initializr and The Project Structure
What You Need Spring Initializr The Project Structure The Main Class, Running the App and Your First Endpoint
The Main Class Running the App Your First Endpoint HTTP Methods, Status Codes and JSON
HTTP Methods Status Codes JSON Testing with curl, @RestController and @GetMapping
Testing with curl @RestController @GetMapping Path Variables, Query Parameters and Practice: A Greeting API
Path Variables Query Parameters Practice: A Greeting API What REST Is, Returning Objects as JSON and Records for Data
What REST Is Returning Objects as JSON Records for Data @PostMapping and @RequestBody, @PutMapping and @DeleteMapping
@PostMapping and @RequestBody @PutMapping @DeleteMapping ResponseEntity, Status Codes in Practice and Practice: A Book API in Memory
ResponseEntity Status Codes in Practice Practice: A Book API in Memory Inversion of Control, Beans and Dependency Injection
Inversion of Control Beans Dependency Injection @Service, Constructor Injection and @Component and Friends
@Service Constructor Injection @Component and Friends @Configuration and @Bean, Bean Scopes and Practice: Layers
@Configuration and @Bean Bean Scopes Practice: Layers application.properties, @Value and @ConfigurationProperties
application.properties @Value @ConfigurationProperties YAML, Profiles and Logging
YAML Profiles Logging CommandLineRunner, Environment Variables and Practice: A Configurable Shop
CommandLineRunner Environment Variables Practice: A Configurable Shop Why a Database, The H2 Database and What JPA Is
Why a Database The H2 Database What JPA Is Entities, Ids and Generated Values and Repositories
Entities Ids and Generated Values Repositories CRUD with JpaRepository, Seeding Data and Practice: Books in a Database
CRUD with JpaRepository Seeding Data Practice: Books in a Database Derived Query Methods, More Query Keywords and Sorting
Derived Query Methods More Query Keywords Sorting Paging, @Query with JPQL and Native Queries
Paging @Query with JPQL Native Queries Counting and Exists, Transactions and Practice: Search Books
Counting and Exists Transactions Practice: Search Books Relationships, @ManyToOne and @OneToMany
Relationships @ManyToOne @OneToMany Lazy Loading, Cascade and Many to Many
Lazy Loading Cascade Many to Many DTOs for Relationships, Avoiding Endless JSON and Practice: Authors and Books
DTOs for Relationships Avoiding Endless JSON Practice: Authors and Books Why Validate, Bean Validation and @Valid
Why Validate Bean Validation @Valid Validation Errors, Custom Exceptions and @ResponseStatus
Validation Errors Custom Exceptions @ResponseStatus @RestControllerAdvice, ProblemDetail and Practice: Clean Errors
@RestControllerAdvice ProblemDetail Practice: Clean Errors Controller, Service, Repository, Mapping Entities to DTOs and Update Requests
Controller, Service, Repository Mapping Entities to DTOs Update Requests PATCH, Filtering with Query Parameters and Pagination in the API
PATCH Filtering with Query Parameters Pagination in the API API Versioning, CORS and Practice: A Complete Books API
API Versioning CORS Practice: A Complete Books API Spring Security Basics, Locked by Default and SecurityFilterChain
Spring Security Basics Locked by Default SecurityFilterChain Users and Passwords, Password Encoding and Roles
Users and Passwords Password Encoding Roles Protecting Endpoints by Role, Method Security and Practice: A Secured API
Protecting Endpoints by Role Method Security Practice: A Secured API Why Test, JUnit Basics and Testing a Service
Why Test JUnit Basics Testing a Service Mockito, @WebMvcTest and MockMvc
Mockito @WebMvcTest MockMvc @DataJpaTest, @SpringBootTest and Practice: Test the API
@DataJpaTest @SpringBootTest Practice: Test the API Actuator, Health and Info and Packaging a Jar
Actuator Health and Info Packaging a Jar Project: The Plan, Entities, Repositories and Service
Project: The Plan Project: Entities Project: Repositories and Service Project: The Controller, Validation and Errors and Security
Project: The Controller Project: Validation and Errors Project: Security Project: Tests, Where to Go Next and Course Recap
Project: Tests Where to Go Next Course Recap Spring Boot · Lesson 58 of 120
Paging One page at a time.
⏱ 2 min read ▶ Video at 1:18:18 🎓 Free
▶ starts at 1:18:18 A shop might have a million books. Sending them all at once would be slow, and nobody can read them anyway. So we send one page at a time.
A million books Too slow to send One page at a time Page Request dot of makes a Pageable, a page number, and a page size. Page numbers start at zero. It can include a Sort too. The result is a Page. It holds this page's books, plus the total count, and the number of pages.
key value PageRequest.of(0, 3) page 0, size 3 Numbers start at 0 Page<Book> content + totals Also sorting inside
Any query method can take a Pageable as its last parameter. Then it returns a Page, instead of a list.
BookRepository.java java Copy
public interface BookRepository extends JpaRepository < Book , Long > {
Page< Book > findByPriceLessThan ( double max , Pageable pageable );
} Prefer to watch? The full Spring Boot course is free All lessons in one video with animations, real code and chapters.
Watch free Find all takes a page request, for page zero, with three books, sorted by title. Get content gives the books on that page. Get total elements, and get total pages, give the totals. Page two, the third page, is the last one. And our own query is paged too. Run it. The first page has three books. There are eight books, in three pages. The last page has only two books. And the cheap books fill two pages.
QueryDemo.java java Copy
@ Component
public class QueryDemo implements CommandLineRunner {
private final BookRepository books;
public QueryDemo (BookRepository books ) { this .books = books; }
@ Override
public void run (String... args ) {
Page < Book > page = books. findAll (PageRequest. of ( 0 , 3 , Sort. by ( "title" )));
System.out. println ( "Page 1: " + page. getContent ());
System.out. println ( "Total books: " + page. getTotalElements ());
System.out. println ( "Total pages: " + page. getTotalPages ());
Page < Book > last = books. findAll (PageRequest. of ( 2 , 3 , Sort. by ( "title" )));
System.out. println ( "Page 3: " + last. getContent () + ", last? " + last. isLast ());
var cheap = books. findByPriceLessThan ( 9 , PageRequest. of ( 0 , 2 ));
System.out. println (cheap. getTotalPages () + " cheap pages: " + cheap. getContent ());
}
} Output
Page 1: [Book 4: Children of Dune (8.49), Book 3: Circe (10.99), Book 1: Dune (9.99)]
Total books: 8
Total pages: 3
Page 3: [Book 6: The Fellowship of the Ring (11.99), Book 2: The Hobbit (8.99)], last?
true
2 cheap pages: [Book 2: The Hobbit (8.99), Book 4: Children of Dune (8.49)] Also uses Book.java @ Entity
public class Book {
@ Id
@ GeneratedValue ( strategy = GenerationType.IDENTITY)
private Long id;
private String title;
private String author;
private double price;
protected Book () {}
public Book (String title , String author , double price ) {
this .title = title; this .author = author; this .price = price;
}
public Long getId () { return id; }
public String getTitle () { return title; }
public String getAuthor () { return author; }
public double getPrice () { return price; }
public void setPrice ( double price ) { this .price = price; }
@ Override
public String toString () { return "Book " + id + ": " + title + " (" + price + ")" ; }
} Also uses BookRepository.java public interface BookRepository extends JpaRepository < Book , Long > {
Page< Book > findByPriceLessThan ( double max , Pageable pageable );
} Also uses data.sql INSERT INTO book (title, author, price) VALUES
( 'Dune' , 'Frank Herbert' , 9 . 99 ),
( 'The Hobbit' , 'J.R.R. Tolkien' , 8 . 99 ),
( 'Circe' , 'Madeline Miller' , 10 . 99 ),
( 'Children of Dune' , 'Frank Herbert' , 8 . 49 ),
( 'Song of Achilles' , 'Madeline Miller' , 9 . 49 ),
( 'The Fellowship of the Ring' , 'J.R.R. Tolkien' , 11 . 99 ),
( 'Emma' , 'Jane Austen' , 6 . 99 ),
( 'Persuasion' , 'Jane Austen' , 7 . 49 ); Also uses application.properties spring.jpa.defer-datasource-initialization =true Pass a Page Request, and get back a Page, with the content and the totals. Page numbers start at zero. Next, writing our own queries, with at Query.
Key takeaways PageRequest.of(page, size) Page<Book> getTotalPages()