{"id":482621,"date":"2026-06-06T10:43:34","date_gmt":"2026-06-06T10:43:34","guid":{"rendered":"https:\/\/savepearlharbor.com\/?p=482621"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=482621","title":{"rendered":"\u041f\u043e\u0447\u0435\u043c\u0443 \u044f \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043b \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c Spring Pageable \u0432 \u043a\u043e\u043d\u0442\u0440\u0430\u043a\u0442\u044b \u0441\u043b\u043e\u044f \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f"},"content":{"rendered":"<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041a\u043e\u043d\u0442\u0440\u0430\u043a\u0442 use case \u0434\u043e\u043b\u0436\u0435\u043d \u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c \u043f\u043e\u0442\u0440\u0435\u0431\u043d\u043e\u0441\u0442\u044c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f, \u0430 \u043d\u0435 API \u0432\u044b\u0431\u0440\u0430\u043d\u043d\u043e\u0433\u043e persistence \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a\u0430. <\/p>\n<p>Spring Data \u043f\u0430\u0433\u0438\u043d\u0430\u0446\u0438\u044f \u0445\u043e\u0440\u043e\u0448\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0432 CRUD \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f\u0445. \u0414\u043b\u044f \u043c\u043d\u043e\u0433\u0438\u0445 \u043f\u0440\u043e\u0435\u043a\u0442\u043e\u0432 Pageable \u0432 application service \u044d\u0442\u043e \u0440\u0430\u0437\u0443\u043c\u043d\u044b\u0439 \u043a\u043e\u043c\u043f\u0440\u043e\u043c\u0438\u0441\u0441. \u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u0435\u0442, \u043a\u043e\u0433\u0434\u0430 Pageable \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u0447\u0430\u0441\u0442\u044c\u044e \u043f\u0443\u0431\u043b\u0438\u0447\u043d\u043e\u0433\u043e \u043a\u043e\u043d\u0442\u0440\u0430\u043a\u0442\u0430 use case:<\/p>\n<pre><code>interface OwnerUseCase {    Page&lt;OwnerView&gt; execute(Pageable pageable);}<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:87px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043a\u0430\u0436\u0434\u044b\u0439 \u043a\u043b\u0438\u0435\u043d\u0442: \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440, \u0448\u0435\u0434\u0443\u043b\u0435\u0440, message listener,\u00a0\u2014 \u0432\u044b\u043d\u0443\u0436\u0434\u0435\u043d \u0437\u043d\u0430\u0442\u044c \u043e\u00a0Spring Data API. \u041a\u043e\u043d\u0442\u0440\u0430\u043a\u0442 \u0441\u043b\u043e\u044f \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0442 \u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c \u0434\u0435\u0442\u0430\u043b\u0438 persistence \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a\u0430, \u0430\u00a0\u043d\u0435\u00a0\u043f\u043e\u0442\u0440\u0435\u0431\u043d\u043e\u0441\u0442\u044c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f.<\/p>\n<p><strong>\u041d\u0435\u0437\u0430\u0432\u0438\u0441\u0438\u043c\u0430\u044f \u043e\u0442 \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a\u0430 \u043c\u043e\u0434\u0435\u043b\u044c \u043f\u0430\u0433\u0438\u043d\u0430\u0446\u0438\u0438<\/strong><\/p>\n<p>PageQuery, PageResult \u0438 SortRequest \u0436\u0438\u0432\u0443\u0442 \u0432 \u0441\u043b\u043e\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f. \u041d\u0438\u043a\u0430\u043a\u0438\u0445 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0435\u0439 \u043d\u0430 Spring Data. \u041e\u0431\u044b\u0447\u043d\u044b\u0435 Java records:<\/p>\n<pre><code>public record PageQuery(int page, int size, List&lt;SortRequest&gt; sort) {    public static PageQuery of(int page, int size) {        return new PageQuery(page, size, List.of());    }    public static PageQuery of(int page, int size, List&lt;SortRequest&gt; sort) {        return new PageQuery(page, size, sort);    }}public record PageResult&lt;T&gt;(List&lt;T&gt; content, int page, int size, long total) {}<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>SortRequest \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u0434\u043e\u043c\u0435\u043d\u043d\u044b\u0435 \u0438\u043c\u0435\u043d\u0430 \u043f\u043e\u043b\u0435\u0439. \u041d\u0438\u043a\u0430\u043a\u043e\u0433\u043e SQL, \u043d\u0438\u043a\u0430\u043a\u0438\u0445 \u0434\u0435\u0442\u0430\u043b\u0435\u0439 \u0438\u043d\u0444\u0440\u0430\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b:<\/p>\n<pre><code>public record SortRequest(String field, Direction direction) {    public enum Direction { ASC, DESC }    private static final Set&lt;String&gt; ALLOWED_FIELDS = Set.of(\"id\", \"name\");    public SortRequest {        if (!ALLOWED_FIELDS.contains(field))            throw new IllegalArgumentException(\"Invalid sort field: \" + field);    }    public static SortRequest asc(String field)  {            return new SortRequest(field, Direction.ASC); }    public static SortRequest desc(String field) {            return new SortRequest(field, Direction.DESC);     }}<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>\u0412\u044b\u0437\u044b\u0432\u0430\u044e\u0449\u0438\u0439 \u043a\u043e\u0434 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u0434\u043e\u043c\u0435\u043d\u043d\u044b\u0439 \u044f\u0437\u044b\u043a. \u041d\u0438\u043a\u0430\u043a\u0438\u0435 SQL aliases \u043d\u0435 \u043f\u0440\u043e\u043d\u0438\u043a\u0430\u044e\u0442 \u0432 \u0441\u043b\u043e\u0439 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f:<\/p>\n<pre><code>\/\/ \u043e\u0434\u043d\u043e \u043f\u043e\u043b\u0435PageQuery.of(0, 10, List.of(SortRequest.asc(\"name\")));\/\/ \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u043b\u0435\u0439PageQuery.of(0, 10, List.of(    SortRequest.asc(\"name\"),    SortRequest.desc(\"id\")));<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>\u041a\u043e\u043d\u0442\u0440\u0430\u043a\u0442 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u044f \u0447\u0442\u0435\u043d\u0438\u044f \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 read model \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e (CQRS \u043f\u043e\u0434\u0445\u043e\u0434), \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439 \u043d\u0430\u043c\u0435\u0440\u0435\u043d\u043d\u043e \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043f\u0440\u043e\u0435\u043a\u0446\u0438\u044e, \u0430 \u043d\u0435 \u0430\u0433\u0440\u0435\u0433\u0430\u0442:<\/p>\n<pre><code>public interface OwnerReadRepository {    PageResult&lt;OwnerView&gt; findAllFlat(PageQuery request);}<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p><strong>\u0418\u043d\u0444\u0440\u0430\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u043d\u044b\u0439 \u0430\u0434\u0430\u043f\u0442\u0435\u0440<\/strong><\/p>\n<p>\u041d\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u0435 \u0438\u043d\u0444\u0440\u0430\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b PageQuery \u043a\u043e\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0432 SQL \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b. SQL aliases \u0436\u0438\u0432\u0443\u0442 \u0437\u0434\u0435\u0441\u044c \u0438 \u043d\u0438\u0433\u0434\u0435 \u0431\u043e\u043b\u044c\u0448\u0435. \u0421\u043b\u043e\u0439 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u043d\u0438\u043a\u043e\u0433\u0434\u0430 \u043d\u0435 \u0432\u0438\u0434\u0438\u0442 \u043d\u0438 Pageable, \u043d\u0438 SQL aliases, \u043d\u0438 \u0441\u044b\u0440\u043e\u0439 ResultSet:<\/p>\n<pre><code>private static final Map&lt;String, String&gt; FIELD_MAP = Map.of(    \"id\",   \"o.id\",    \"name\", \"o.name\");private static final String SELECT_PAGE = \"\"\"    SELECT o.id   AS owner_id,           o.name AS owner_name    FROM owners o    ORDER BY %s    LIMIT :limit OFFSET :offset    \"\"\";@Repositorypublic class JdbcOwnerReadRepository implements OwnerReadRepository {    private String buildOrderBy(PageQuery request) {        if (request.sort().isEmpty()) return \"o.id ASC\";        return request.sort().stream()                .map(s -&gt; FIELD_MAP.get(s.field()) + \" \" + s.direction().name())                .collect(Collectors.joining(\", \"));    }    @Override    public PageResult&lt;OwnerView&gt; findAllFlat(PageQuery request) {        int offset = request.page() * request.size();        \/\/ orderBy \u0441\u043e\u0431\u0438\u0440\u0430\u0435\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u0438\u0437 \u043f\u043e\u043b\u0435\u0439 FIELD_MAP + enum direction \u2014 SQL \u0438\u043d\u044a\u0435\u043a\u0446\u0438\u044f \u043d\u0435\u0432\u043e\u0437\u043c\u043e\u0436\u043d\u0430        String orderBy = buildOrderBy(request);        List&lt;OwnerView&gt; content = jdbc.sql(SELECT_PAGE.formatted(orderBy))                .param(\"limit\",  request.size())                .param(\"offset\", offset)                .query(OwnerProjection.class)                .stream()                .map(ViewMapper::toView)                .toList();        long total = jdbc.sql(COUNT_ALL).query(Long.class).single();        return new PageResult&lt;&gt;(content, request.page(), request.size(), total);    }}<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>OwnerProjection \u0438 ViewMapper package-private. \u041e\u043d\u0438 \u043d\u0438\u043a\u043e\u0433\u0434\u0430 \u043d\u0435 \u0432\u044b\u0445\u043e\u0434\u044f\u0442 \u0437\u0430 \u043f\u0440\u0435\u0434\u0435\u043b\u044b \u0438\u043d\u0444\u0440\u0430\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u043d\u043e\u0433\u043e \u043f\u0430\u043a\u0435\u0442\u0430. \u0412 \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u0435 \u0441\u043b\u0443\u0447\u0430\u0435\u0432 \u0441\u043c\u0435\u043d\u0430 persistence \u0442\u0435\u0445\u043d\u043e\u043b\u043e\u0433\u0438\u0438 \u0437\u0430\u0442\u0440\u0430\u0433\u0438\u0432\u0430\u0435\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u0430\u0434\u0430\u043f\u0442\u0435\u0440. \u0421\u043b\u043e\u0439 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u043e\u0441\u0442\u0430\u0451\u0442\u0441\u044f \u043d\u0435\u0438\u0437\u043c\u0435\u043d\u043d\u044b\u043c.<\/p>\n<p><strong>\u0422\u0435\u0441\u0442\u0438\u0440\u0443\u0435\u043c\u043e\u0441\u0442\u044c<\/strong><\/p>\n<p>Application service \u0442\u0435\u0441\u0442\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0431\u0435\u0437 Spring \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430:<\/p>\n<pre><code>@Testvoid returns_paginated_owners() {    var request = PageQuery.of(0, 10);    var expected = new PageResult&lt;&gt;(List.of(ownerView), 0, 10, 1L);    when(repository.findAllFlat(request)).thenReturn(expected);    var result = service.getOwners(request);    assertThat(result.content().get(0).name()).isEqualTo(\"jack1\");    verify(repository).findAllFlat(request);}<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>\u041d\u0435\u0442 Spring \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430, \u043d\u0435\u0442 Pageable, \u043d\u0435\u0442 Spring Data \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0435\u0439 \u0432 \u0442\u0435\u0441\u0442\u0435.<\/p>\n<p><strong>\u041a\u043e\u043c\u043f\u0440\u043e\u043c\u0438\u0441\u0441<\/strong><\/p>\n<p>\u042d\u0442\u043e\u0442 \u043f\u043e\u0434\u0445\u043e\u0434 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442 \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0446\u0438\u0438. \u0412 \u043f\u0440\u043e\u0441\u0442\u043e\u043c CRUD \u044d\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0438\u0437\u043b\u0438\u0448\u043d\u0438\u043c. \u0418 \u044d\u0442\u043e \u0432\u0430\u043b\u0438\u0434\u043d\u044b\u0439 \u0432\u044b\u0431\u043e\u0440.<\/p>\n<p>\u0412 \u0441\u0438\u0441\u0442\u0435\u043c\u0435 \u0433\u0434\u0435 \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u043d\u044b\u0435 \u0433\u0440\u0430\u043d\u0438\u0446\u044b \u0432\u0430\u0436\u043d\u044b \u2014 \u044f\u0432\u043d\u0430\u044f \u043c\u043e\u0434\u0435\u043b\u044c \u043f\u0430\u0433\u0438\u043d\u0430\u0446\u0438\u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u0442 \u043d\u0435\u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u044c \u0441\u043b\u043e\u044f \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u043e\u0442 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e\u0433\u043e \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a\u0430. \u0421\u0442\u043e\u0438\u0442 \u043e\u0442\u043c\u0435\u0442\u0438\u0442\u044c: \u0432 \u0440\u0430\u0437\u043d\u044b\u0445 bounded context\u2019\u0430\u0445 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0441\u0432\u043e\u044f \u0441\u0435\u043c\u0430\u043d\u0442\u0438\u043a\u0430 \u043f\u0430\u0433\u0438\u043d\u0430\u0446\u0438\u0438: cursor-based, keyset. PageQuery \u043d\u0435 \u0441\u0442\u043e\u0438\u0442 \u0434\u0435\u043b\u0430\u0442\u044c \u043e\u0431\u0449\u0438\u043c \u043d\u0430 \u0432\u0441\u044e \u0441\u0438\u0441\u0442\u0435\u043c\u0443 \u0432 \u0442\u0430\u043a\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435.<\/p>\n<p>\u0418 \u0434\u0430, PageQuery \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u043f\u043e\u0445\u043e\u0436\u0435 \u043d\u0430 Pageable. \u042d\u0442\u043e \u043d\u0430\u043c\u0435\u0440\u0435\u043d\u043d\u043e. \u0426\u0435\u043b\u044c \u043d\u0435 \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u043e\u0431\u0440\u0435\u0441\u0442\u0438 \u043d\u043e\u0432\u0443\u044e \u043c\u043e\u0434\u0435\u043b\u044c \u043f\u0430\u0433\u0438\u043d\u0430\u0446\u0438\u0438. \u0426\u0435\u043b\u044c \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e\u0431\u044b \u043a\u043e\u043d\u0442\u0440\u0430\u043a\u0442 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u043f\u0440\u0438\u043d\u0430\u0434\u043b\u0435\u0436\u0430\u043b \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044e, \u0430 \u043d\u0435 Spring Data.<\/p>\n<h4>\u0420\u0430\u0431\u043e\u0447\u0438\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u044b \u043d\u0430 \u0413\u0438\u0442\u0445\u0430\u0431\u0435<\/h4>\n<p><a href=\"https:\/\/github.com\/java-backend-architecture\/persistence-flat-pagination-jdbc\" rel=\"noopener noreferrer nofollow\">\u041f\u043b\u043e\u0441\u043a\u0430\u044f \u043f\u0430\u0433\u0438\u043d\u0430\u0446\u0438\u044f &#8212; PageQuery, PageResult, \u0447\u0438\u0441\u0442\u044b\u0439 SQL<\/a><\/p>\n<p><a href=\"https:\/\/github.com\/java-backend-architecture\/persistence-flat-pagination-sorting-jdbc\" rel=\"noopener noreferrer nofollow\">\u041f\u0430\u0433\u0438\u043d\u0430\u0446\u0438\u044f \u0441 \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u043b\u0435\u0432\u043e\u0439 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u043e\u0439 \u0438 whitelist \u0437\u0430\u0449\u0438\u0442\u043e\u0439<\/a><\/p>\n<p><a href=\"https:\/\/github.com\/java-backend-architecture\/persistence-graph-pagination-jdbc\" rel=\"noopener noreferrer nofollow\">\u041f\u0430\u0433\u0438\u043d\u0430\u0446\u0438\u044f \u043f\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u043d\u043e\u043c\u0443 \u0433\u0440\u0430\u0444\u0443 (owner \u2192 pets \u2192 visits)<\/a><\/p>\n<\/div>\n<p>\u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/1044352\/\">https:\/\/habr.com\/ru\/articles\/1044352\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u041a\u043e\u043d\u0442\u0440\u0430\u043a\u0442 use case \u0434\u043e\u043b\u0436\u0435\u043d \u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c \u043f\u043e\u0442\u0440\u0435\u0431\u043d\u043e\u0441\u0442\u044c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f, \u0430 \u043d\u0435 API \u0432\u044b\u0431\u0440\u0430\u043d\u043d\u043e\u0433\u043e persistence \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a\u0430. Spring Data \u043f\u0430\u0433\u0438\u043d\u0430\u0446\u0438\u044f \u0445\u043e\u0440\u043e\u0448\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0432 CRUD \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f\u0445. \u0414\u043b\u044f \u043c\u043d\u043e\u0433\u0438\u0445 \u043f\u0440\u043e\u0435\u043a\u0442\u043e\u0432 Pageable \u0432 application service \u044d\u0442\u043e \u0440\u0430\u0437\u0443\u043c\u043d\u044b\u0439 \u043a\u043e\u043c\u043f\u0440\u043e\u043c\u0438\u0441\u0441. \u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u0435\u0442, \u043a\u043e\u0433\u0434\u0430 Pageable \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u0447\u0430\u0441\u0442\u044c\u044e \u043f\u0443\u0431\u043b\u0438\u0447\u043d\u043e\u0433\u043e \u043a\u043e\u043d\u0442\u0440\u0430\u043a\u0442\u0430 use case:interface OwnerUseCase {    Page&lt;OwnerView&gt; execute(Pageable pageable);}\u0422\u0435\u043f\u0435\u0440\u044c \u043a\u0430\u0436\u0434\u044b\u0439 \u043a\u043b\u0438\u0435\u043d\u0442: \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440, \u0448\u0435\u0434\u0443\u043b\u0435\u0440, message listener,\u00a0\u2014 \u0432\u044b\u043d\u0443\u0436\u0434\u0435\u043d \u0437\u043d\u0430\u0442\u044c \u043e\u00a0Spring Data API. \u041a\u043e\u043d\u0442\u0440\u0430\u043a\u0442 \u0441\u043b\u043e\u044f \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0442 \u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c \u0434\u0435\u0442\u0430\u043b\u0438 persistence \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a\u0430, \u0430\u00a0\u043d\u0435\u00a0\u043f\u043e\u0442\u0440\u0435\u0431\u043d\u043e\u0441\u0442\u044c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f.\u041d\u0435\u0437\u0430\u0432\u0438\u0441\u0438\u043c\u0430\u044f \u043e\u0442 \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a\u0430 \u043c\u043e\u0434\u0435\u043b\u044c \u043f\u0430\u0433\u0438\u043d\u0430\u0446\u0438\u0438PageQuery, PageResult \u0438 SortRequest \u0436\u0438\u0432\u0443\u0442 \u0432 \u0441\u043b\u043e\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f. \u041d\u0438\u043a\u0430\u043a\u0438\u0445 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0435\u0439 \u043d\u0430 Spring Data. \u041e\u0431\u044b\u0447\u043d\u044b\u0435 Java records:public record PageQuery(int page, int size, List&lt;SortRequest&gt; sort) {    public static PageQuery of(int page, int size) {        return new PageQuery(page, size, List.of());    }    public static PageQuery of(int page, int size, List&lt;SortRequest&gt; sort) {        return new PageQuery(page, size, sort);    }}public record PageResult&lt;T&gt;(List&lt;T&gt; content, int page, int size, long total) {}SortRequest \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u0434\u043e\u043c\u0435\u043d\u043d\u044b\u0435 \u0438\u043c\u0435\u043d\u0430 \u043f\u043e\u043b\u0435\u0439. \u041d\u0438\u043a\u0430\u043a\u043e\u0433\u043e SQL, \u043d\u0438\u043a\u0430\u043a\u0438\u0445 \u0434\u0435\u0442\u0430\u043b\u0435\u0439 \u0438\u043d\u0444\u0440\u0430\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b:public record SortRequest(String field, Direction direction) {    public enum Direction { ASC, DESC }    private static final Set&lt;String&gt; ALLOWED_FIELDS = Set.of(&#171;id&#187;, &#171;name&#187;);    public SortRequest {        if (!ALLOWED_FIELDS.contains(field))            throw new IllegalArgumentException(&#171;Invalid sort field: &#187; + field);    }    public static SortRequest asc(String field)  {            return new SortRequest(field, Direction.ASC); }    public static SortRequest desc(String field) {            return new SortRequest(field, Direction.DESC);     }}\u0412\u044b\u0437\u044b\u0432\u0430\u044e\u0449\u0438\u0439 \u043a\u043e\u0434 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u0434\u043e\u043c\u0435\u043d\u043d\u044b\u0439 \u044f\u0437\u044b\u043a. \u041d\u0438\u043a\u0430\u043a\u0438\u0435 SQL aliases \u043d\u0435 \u043f\u0440\u043e\u043d\u0438\u043a\u0430\u044e\u0442 \u0432 \u0441\u043b\u043e\u0439 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f:\/\/ \u043e\u0434\u043d\u043e \u043f\u043e\u043b\u0435PageQuery.of(0, 10, List.of(SortRequest.asc(&#171;name&#187;)));\/\/ \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u043b\u0435\u0439PageQuery.of(0, 10, List.of(    SortRequest.asc(&#171;name&#187;),    SortRequest.desc(&#171;id&#187;)));\u041a\u043e\u043d\u0442\u0440\u0430\u043a\u0442 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u044f \u0447\u0442\u0435\u043d\u0438\u044f \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 read model \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e (CQRS \u043f\u043e\u0434\u0445\u043e\u0434), \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439 \u043d\u0430\u043c\u0435\u0440\u0435\u043d\u043d\u043e \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043f\u0440\u043e\u0435\u043a\u0446\u0438\u044e, \u0430 \u043d\u0435 \u0430\u0433\u0440\u0435\u0433\u0430\u0442:public interface OwnerReadRepository {    PageResult&lt;OwnerView&gt; findAllFlat(PageQuery request);}\u0418\u043d\u0444\u0440\u0430\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u043d\u044b\u0439 \u0430\u0434\u0430\u043f\u0442\u0435\u0440\u041d\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u0435 \u0438\u043d\u0444\u0440\u0430\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b PageQuery \u043a\u043e\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0432 SQL \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b. SQL aliases \u0436\u0438\u0432\u0443\u0442 \u0437\u0434\u0435\u0441\u044c \u0438 \u043d\u0438\u0433\u0434\u0435 \u0431\u043e\u043b\u044c\u0448\u0435. \u0421\u043b\u043e\u0439 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u043d\u0438\u043a\u043e\u0433\u0434\u0430 \u043d\u0435 \u0432\u0438\u0434\u0438\u0442 \u043d\u0438 Pageable, \u043d\u0438 SQL aliases, \u043d\u0438 \u0441\u044b\u0440\u043e\u0439 ResultSet:private static final Map&lt;String, String&gt; FIELD_MAP = Map.of(    &#171;id&#187;,   &#171;o.id&#187;,    &#171;name&#187;, &#171;o.name&#187;);private static final String SELECT_PAGE = &#171;&#187;&#187;    SELECT o.id   AS owner_id,           o.name AS owner_name    FROM owners o    ORDER BY %s    LIMIT :limit OFFSET :offset    &#171;&#187;&#187;;@Repositorypublic class JdbcOwnerReadRepository implements OwnerReadRepository {    private String buildOrderBy(PageQuery request) {        if (request.sort().isEmpty()) return &#171;o.id ASC&#187;;        return request.sort().stream()                .map(s -&gt; FIELD_MAP.get(s.field()) + &#187; &#187; + s.direction().name())                .collect(Collectors.joining(&#171;, &#171;));    }    @Override    public PageResult&lt;OwnerView&gt; findAllFlat(PageQuery request) {        int offset = request.page() * request.size();        \/\/ orderBy \u0441\u043e\u0431\u0438\u0440\u0430\u0435\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u0438\u0437 \u043f\u043e\u043b\u0435\u0439 FIELD_MAP + enum direction \u2014 SQL \u0438\u043d\u044a\u0435\u043a\u0446\u0438\u044f \u043d\u0435\u0432\u043e\u0437\u043c\u043e\u0436\u043d\u0430        String orderBy = buildOrderBy(request);        List&lt;OwnerView&gt; content = jdbc.sql(SELECT_PAGE.formatted(orderBy))                .param(&#171;limit&#187;,  request.size())                .param(&#171;offset&#187;, offset)                .query(OwnerProjection.class)                .stream()                .map(ViewMapper::toView)                .toList();        long total = jdbc.sql(COUNT_ALL).query(Long.class).single();        return new PageResult&lt;&gt;(content, request.page(), request.size(), total);    }}OwnerProjection \u0438 ViewMapper package-private. \u041e\u043d\u0438 \u043d\u0438\u043a\u043e\u0433\u0434\u0430 \u043d\u0435 \u0432\u044b\u0445\u043e\u0434\u044f\u0442 \u0437\u0430 \u043f\u0440\u0435\u0434\u0435\u043b\u044b \u0438\u043d\u0444\u0440\u0430\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u043d\u043e\u0433\u043e \u043f\u0430\u043a\u0435\u0442\u0430. \u0412 \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u0435 \u0441\u043b\u0443\u0447\u0430\u0435\u0432 \u0441\u043c\u0435\u043d\u0430 persistence \u0442\u0435\u0445\u043d\u043e\u043b\u043e\u0433\u0438\u0438 \u0437\u0430\u0442\u0440\u0430\u0433\u0438\u0432\u0430\u0435\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u0430\u0434\u0430\u043f\u0442\u0435\u0440. \u0421\u043b\u043e\u0439 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u043e\u0441\u0442\u0430\u0451\u0442\u0441\u044f \u043d\u0435\u0438\u0437\u043c\u0435\u043d\u043d\u044b\u043c.\u0422\u0435\u0441\u0442\u0438\u0440\u0443\u0435\u043c\u043e\u0441\u0442\u044cApplication service \u0442\u0435\u0441\u0442\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0431\u0435\u0437 Spring \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430:@Testvoid returns_paginated_owners() {    var request = PageQuery.of(0, 10);    var expected = new PageResult&lt;&gt;(List.of(ownerView), 0, 10, 1L);    when(repository.findAllFlat(request)).thenReturn(expected);    var result = service.getOwners(request);    assertThat(result.content().get(0).name()).isEqualTo(&#171;jack1&#187;);    verify(repository).findAllFlat(request);}\u041d\u0435\u0442 Spring \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430, \u043d\u0435\u0442 Pageable, \u043d\u0435\u0442 Spring Data \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0435\u0439 \u0432 \u0442\u0435\u0441\u0442\u0435.\u041a\u043e\u043c\u043f\u0440\u043e\u043c\u0438\u0441\u0441\u042d\u0442\u043e\u0442 \u043f\u043e\u0434\u0445\u043e\u0434 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442 \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0446\u0438\u0438. \u0412 \u043f\u0440\u043e\u0441\u0442\u043e\u043c CRUD \u044d\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0438\u0437\u043b\u0438\u0448\u043d\u0438\u043c. \u0418 \u044d\u0442\u043e \u0432\u0430\u043b\u0438\u0434\u043d\u044b\u0439 \u0432\u044b\u0431\u043e\u0440.\u0412 \u0441\u0438\u0441\u0442\u0435\u043c\u0435 \u0433\u0434\u0435 \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u043d\u044b\u0435 \u0433\u0440\u0430\u043d\u0438\u0446\u044b \u0432\u0430\u0436\u043d\u044b \u2014 \u044f\u0432\u043d\u0430\u044f \u043c\u043e\u0434\u0435\u043b\u044c \u043f\u0430\u0433\u0438\u043d\u0430\u0446\u0438\u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u0442 \u043d\u0435\u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u044c \u0441\u043b\u043e\u044f \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u043e\u0442 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e\u0433\u043e \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a\u0430. \u0421\u0442\u043e\u0438\u0442 \u043e\u0442\u043c\u0435\u0442\u0438\u0442\u044c: \u0432 \u0440\u0430\u0437\u043d\u044b\u0445 bounded context\u2019\u0430\u0445 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0441\u0432\u043e\u044f \u0441\u0435\u043c\u0430\u043d\u0442\u0438\u043a\u0430 \u043f\u0430\u0433\u0438\u043d\u0430\u0446\u0438\u0438: cursor-based, keyset. PageQuery \u043d\u0435 \u0441\u0442\u043e\u0438\u0442 \u0434\u0435\u043b\u0430\u0442\u044c \u043e\u0431\u0449\u0438\u043c \u043d\u0430 \u0432\u0441\u044e \u0441\u0438\u0441\u0442\u0435\u043c\u0443 \u0432 \u0442\u0430\u043a\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435.\u0418 \u0434\u0430, PageQuery \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u043f\u043e\u0445\u043e\u0436\u0435 \u043d\u0430 Pageable. \u042d\u0442\u043e \u043d\u0430\u043c\u0435\u0440\u0435\u043d\u043d\u043e. \u0426\u0435\u043b\u044c \u043d\u0435 \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u043e\u0431\u0440\u0435\u0441\u0442\u0438 \u043d\u043e\u0432\u0443\u044e \u043c\u043e\u0434\u0435\u043b\u044c \u043f\u0430\u0433\u0438\u043d\u0430\u0446\u0438\u0438. \u0426\u0435\u043b\u044c \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e\u0431\u044b \u043a\u043e\u043d\u0442\u0440\u0430\u043a\u0442 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u043f\u0440\u0438\u043d\u0430\u0434\u043b\u0435\u0436\u0430\u043b \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044e, \u0430 \u043d\u0435 Spring Data.\u0420\u0430\u0431\u043e\u0447\u0438\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u044b \u043d\u0430 \u0413\u0438\u0442\u0445\u0430\u0431\u0435\u041f\u043b\u043e\u0441\u043a\u0430\u044f \u043f\u0430\u0433\u0438\u043d\u0430\u0446\u0438\u044f &#8212; PageQuery, PageResult, \u0447\u0438\u0441\u0442\u044b\u0439 SQL\u041f\u0430\u0433\u0438\u043d\u0430\u0446\u0438\u044f \u0441 \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u043b\u0435\u0432\u043e\u0439 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u043e\u0439 \u0438 whitelist \u0437\u0430\u0449\u0438\u0442\u043e\u0439\u041f\u0430\u0433\u0438\u043d\u0430\u0446\u0438\u044f \u043f\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u043d\u043e\u043c\u0443 \u0433\u0440\u0430\u0444\u0443 (owner \u2192 pets \u2192 visits)\u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 https:\/\/habr.com\/ru\/articles\/1044352\/<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-482621","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/482621","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=482621"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/482621\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=482621"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=482621"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=482621"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}