Переход на SpringBoot 3 и добавление примера со Slice
This commit is contained in:
6
pom.xml
6
pom.xml
@@ -5,17 +5,21 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-parent</artifactId>
|
<artifactId>spring-boot-starter-parent</artifactId>
|
||||||
<version>2.7.5</version>
|
<version>3.1.0</version>
|
||||||
<relativePath/> <!-- lookup parent from repository -->
|
<relativePath/> <!-- lookup parent from repository -->
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<groupId>dev.struchkov.example</groupId>
|
<groupId>dev.struchkov.example</groupId>
|
||||||
<artifactId>spring-pagination</artifactId>
|
<artifactId>spring-pagination</artifactId>
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
|
||||||
<name>spring-pagination</name>
|
<name>spring-pagination</name>
|
||||||
<description>spring-pagination</description>
|
<description>spring-pagination</description>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<java.version>17</java.version>
|
<java.version>17</java.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
|||||||
@@ -2,6 +2,10 @@
|
|||||||
GET http://localhost:8080/api/post?offset=0&limit=3
|
GET http://localhost:8080/api/post?offset=0&limit=3
|
||||||
Content-Type: application/json
|
Content-Type: application/json
|
||||||
|
|
||||||
|
### Получить первую страницу из 3 элементов
|
||||||
|
GET http://localhost:8080/api/post/exampleSlice?offset=0&limit=3
|
||||||
|
Content-Type: application/json
|
||||||
|
|
||||||
### Получить вторую страницу из 3 элементов с сортировкой
|
### Получить вторую страницу из 3 элементов с сортировкой
|
||||||
GET http://localhost:8080/api/post/exampleSort?offset=1&limit=3&sort=createOn
|
GET http://localhost:8080/api/post/exampleSort?offset=1&limit=3&sort=createOn
|
||||||
Content-Type: application/json
|
Content-Type: application/json
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
package dev.struchkov.example;
|
package dev.struchkov.example;
|
||||||
|
|
||||||
|
import jakarta.persistence.Entity;
|
||||||
|
import jakarta.persistence.Id;
|
||||||
|
import jakarta.persistence.Table;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
|
|
||||||
import javax.persistence.Entity;
|
|
||||||
import javax.persistence.Id;
|
|
||||||
import javax.persistence.Table;
|
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
|
|||||||
@@ -1,16 +1,16 @@
|
|||||||
package dev.struchkov.example;
|
package dev.struchkov.example;
|
||||||
|
|
||||||
|
import jakarta.validation.constraints.Min;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.data.domain.Page;
|
import org.springframework.data.domain.Page;
|
||||||
import org.springframework.data.domain.PageRequest;
|
import org.springframework.data.domain.PageRequest;
|
||||||
|
import org.springframework.data.domain.Slice;
|
||||||
import org.springframework.data.domain.Sort;
|
import org.springframework.data.domain.Sort;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
import javax.validation.constraints.Min;
|
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("api/post")
|
@RequestMapping("api/post")
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
@@ -26,6 +26,14 @@ public class PostController {
|
|||||||
return repository.findAll(PageRequest.of(offset, limit));
|
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")
|
@GetMapping("exampleSort")
|
||||||
public Page<Post> getAllAndSort(
|
public Page<Post> getAllAndSort(
|
||||||
@RequestParam("offset") Integer offset,
|
@RequestParam("offset") Integer offset,
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package dev.struchkov.example;
|
|||||||
|
|
||||||
import org.springframework.data.domain.Page;
|
import org.springframework.data.domain.Page;
|
||||||
import org.springframework.data.domain.Pageable;
|
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.JpaRepository;
|
||||||
import org.springframework.data.jpa.repository.Query;
|
import org.springframework.data.jpa.repository.Query;
|
||||||
import org.springframework.data.repository.query.Param;
|
import org.springframework.data.repository.query.Param;
|
||||||
@@ -10,6 +11,9 @@ import java.util.UUID;
|
|||||||
|
|
||||||
public interface PostRepository extends JpaRepository<Post, 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);
|
Page<Post> findAllByTitleLikeIgnoreCase(String titleLike, Pageable pageable);
|
||||||
|
|
||||||
@Query("SELECT p FROM Post p WHERE p.title like %:title%")
|
@Query("SELECT p FROM Post p WHERE p.title like %:title%")
|
||||||
|
|||||||
Reference in New Issue
Block a user