spring-boot-realworld-examp.../src/main/resources/mapper/ArticleReadService.xml

160 lines
5.6 KiB
XML

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="io.spring.infrastructure.mybatis.readservice.ArticleReadService">
<sql id="profileColumns">
U.id userId,
U.username userUsername,
U.bio userBio,
U.image userImage
</sql>
<sql id="selectArticleData">
select
A.id articleId,
A.slug articleSlug,
A.title articleTitle,
A.description articleDescription,
A.body articleBody,
A.created_at articleCreatedAt,
A.updated_at articleUpdatedAt,
T.name tagName,
<include refid="profileColumns"/>
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 users U on U.id = A.user_id
</sql>
<sql id="selectArticleIds">
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="findById" resultMap="transfer.data.articleData">
<include refid="selectArticleData"/>
where A.id = #{id}
</select>
<select id="findBySlug" resultMap="transfer.data.articleData">
<include refid="selectArticleData"/>
where A.slug = #{slug}
</select>
<select id="queryArticles" resultMap="articleId">
<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>
</where>
order by A.created_at desc
limit #{page.offset}, #{page.limit}
</select>
<select id="countArticle" resultType="java.lang.Integer">
select
count(DISTINCT A.id)
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
<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>
</where>
</select>
<select id="findArticles" resultMap="transfer.data.articleData">
<include refid="selectArticleData"/>
where A.id in
<foreach index="index" collection="articleIds" item="id" open="(" separator="," close=")">
#{id}
</foreach>
order by A.created_at desc
</select>
<select id="findArticlesOfAuthors" resultMap="transfer.data.articleData">
<include refid="selectArticleData"/>
where A.user_id in
<foreach index="index" collection="authors" item="id" open="(" separator="," close=")">
#{id}
</foreach>
limit #{page.offset}, #{page.limit}
</select>
<select id="countFeedSize" resultType="java.lang.Integer">
select count(1) from articles A where A.user_id in
<foreach collection="authors" item="id" open="(" close=")" separator=",">
#{id}
</foreach>
</select>
<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 != "" and page.direction.name() == "NEXT"'>
AND A.created_at &lt; #{page.cursor}
</if>
<if test='page.cursor != "" 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>
<select id="findArticlesOfAuthorsWithCursor" resultMap="transfer.data.articleData">
<include refid="selectArticleData"/>
<where>
A.user_id in
<foreach index="index" collection="authors" item="id" open="(" separator="," close=")">
#{id}
</foreach>
<if test='page.cursor != "" and page.direction.name() == "NEXT"'>
AND A.created_at &lt; #{page.cursor}
</if>
<if test='page.cursor != "" and page.direction.name() == "PREV"'>
AND A.created_at > #{page.cursor}
</if>
<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>
</where>
limit #{page.queryLimit}
</select>
<resultMap id="articleId" type="string">
<id javaType="string" column="articleId"/>
</resultMap>
</mapper>