Переход на SpringBoot 3 и добавление примера со Slice

This commit is contained in:
Struchkov Mark 2023-05-29 19:42:29 +03:00
parent 809ca38a30
commit b62eda5612
Signed by: upagge
GPG Key ID: D3018BE7BA428CA6
5 changed files with 26 additions and 6 deletions

View File

@ -5,17 +5,21 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.5</version>
<version>3.1.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>dev.struchkov.example</groupId>
<artifactId>spring-pagination</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-pagination</name>
<description>spring-pagination</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>

View File

@ -2,6 +2,10 @@
GET http://localhost:8080/api/post?offset=0&limit=3
Content-Type: application/json
### Получить первую страницу из 3 элементов
GET http://localhost:8080/api/post/exampleSlice?offset=0&limit=3
Content-Type: application/json
### Получить вторую страницу из 3 элементов с сортировкой
GET http://localhost:8080/api/post/exampleSort?offset=1&limit=3&sort=createOn
Content-Type: application/json

View File

@ -1,11 +1,11 @@
package dev.struchkov.example;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import lombok.Getter;
import lombok.Setter;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import java.time.LocalDateTime;
import java.util.UUID;

View File

@ -1,16 +1,16 @@
package dev.struchkov.example;
import jakarta.validation.constraints.Min;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Slice;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.validation.constraints.Min;
@RestController
@RequestMapping("api/post")
@RequiredArgsConstructor
@ -26,6 +26,14 @@ public class PostController {
return repository.findAll(PageRequest.of(offset, limit));
}
@GetMapping("exampleSlice")
public Slice<Post> getAllSlice(
@RequestParam(value = "offset", defaultValue = "0") @Min(0) Integer offset,
@RequestParam(value = "limit", defaultValue = "20") Integer limit
) {
return repository.findAllSlice(PageRequest.of(offset, limit));
}
@GetMapping("exampleSort")
public Page<Post> getAllAndSort(
@RequestParam("offset") Integer offset,

View File

@ -2,6 +2,7 @@ package dev.struchkov.example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
@ -10,6 +11,9 @@ import java.util.UUID;
public interface PostRepository extends JpaRepository<Post, UUID> {
@Query("SELECT p FROM Post p")
Slice<Post> findAllSlice(Pageable pageable);
Page<Post> findAllByTitleLikeIgnoreCase(String titleLike, Pageable pageable);
@Query("SELECT p FROM Post p WHERE p.title like %:title%")