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 < #{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();
}
1条答案
按热度按时间omtl5h9j1#