spring-data-jpa 将mybatis转换为springdata jpa实现

t3psigkw  于 2022-11-10  发布在  Spring
关注(0)|答案(1)|浏览(193)

1,Map器. xml

select
        DISTINCT(A.id) articleId, A.created_at
        from
        articles A
        left join article_tags AT on A.id = AT.article_id
        left join tags T on T.id = AT.tag_id
        left join article_favorites AF on AF.article_id = A.id
        left join users AU on AU.id = A.user_id
        left join users AFU on AFU.id = AF.user_id
    </sql>

<select id="findArticlesWithCursor" resultType="java.lang.String">
    <include refid="selectArticleIds" />
    <where>
        <if test="tag != null">
            T.name = #{tag}
        </if>
        <if test="author != null">
            AND AU.username = #{author}
        </if>
        <if test="favoritedBy != null">
            AND AFU.username = #{favoritedBy}
        </if>
        <if test='page.cursor != null and page.direction.name() == "NEXT"'>
            AND A.created_at &lt; #{page.cursor}
        </if>
        <if test='page.cursor != null and page.direction.name() == "PREV"'>
            AND A.created_at > #{page.cursor}
        </if>
    </where>
    <if test='page.direction.name() == "NEXT"'>
        order by A.created_at desc
    </if>
    <if test='page.direction.name() == "PREV"'>
        order by A.created_at asc
    </if>
    limit #{page.queryLimit}
</select>

2.Methods

List<String> findArticlesWithCursor(
      @Param("tag") String tag,
      @Param("author") String author,
      @Param("favoritedBy") String favoritedBy,
      @Param("page") CursorPageParameter page);

3.CursorPageParamete

@Data
@NoArgsConstructor
public class CursorPageParameter<T> {
  private static final int MAX_LIMIT = 1000;
  private int limit = 20;
  private T cursor;
  private Direction direction;

  public CursorPageParameter(T cursor, int limit, Direction direction) {
    setLimit(limit);
    setCursor(cursor);
    setDirection(direction);
  }

  public boolean isNext() {
    return direction == Direction.NEXT;
  }

  public int getQueryLimit() {
    return limit + 1;
  }

  private void setCursor(T cursor) {
    this.cursor = cursor;
  }

  private void setLimit(int limit) {
    if (limit > MAX_LIMIT) {
      this.limit = MAX_LIMIT;
    } else if (limit > 0) {
      this.limit = limit;
    }
  }
}

4.Direction

public enum Direction {
    PREV,
    NEXT
  }

5.CursorPager

@Getter
public class CursorPager<T extends Node> {
  private List<T> data;
  private boolean next;
  private boolean previous;

  public CursorPager(List<T> data, Direction direction, boolean hasExtra) {
    this.data = data;

    if (direction == Direction.NEXT) {
      this.previous = false;
      this.next = hasExtra;
    } else {
      this.next = false;
      this.previous = hasExtra;
    }
  }

  public boolean hasNext() {
    return next;
  }

  public boolean hasPrevious() {
    return previous;
  }

  public PageCursor getStartCursor() {
    return data.isEmpty() ? null : data.get(0).getCursor();
  }

  public PageCursor getEndCursor() {
    return data.isEmpty() ? null : data.get(data.size() - 1).getCursor();
  }

  public enum Direction {
    PREV,
    NEXT
  }
}

6.Node

public interface Node {
  PageCursor getCursor();
}
omtl5h9j

omtl5h9j1#

@Query(
          """
                  select  A.id 
                  from Article A 
                  left join ArticleTagRelation AT on A.id=AT.articleId 
                  left join Tag T on T.id=AT.tagId 
                  left join ArticleFavorite AF on AF.articleId=A.id 
                  left join User AU on AU.id=A.userId 
                  left join User AFU on AFU.id=AF.userId 
                  where
                  (?1 is null or T.name=?1) 
                  and 
                  (?2 is null or AU.username=?2) 
                  and
                  (?3 is null or AFU.username=?3) 
                  and (
                    (?4 is not null and ?5='NEXT'
                     and A.createdAt <?4)
                    or
                    (?4 is not null and ?5='PREV'
                     and A.createdAt >?4)
                    or
                     (?4 is null)
                  )
                  group by A.id 
                  order by
                  case when ?5='NEXT' then A.createdAt end desc,
                  case when ?5='PREV' then A.createdAt end asc
          """

  )
  List<String> findArticlesWithCursor(String tag,
                                          String author,
                                          String favoritedBy,
                                          Timestamp cursor,
                                          String DirectionName,
                                          Pageable pageable);

相关问题