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

108 lines
3.8 KiB
XML
Raw Normal View History

2017-08-15 09:47:18 +07:00
<?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" >
2017-08-18 16:08:27 +07:00
<mapper namespace="io.spring.infrastructure.mybatis.readservice.ArticleReadService">
2017-08-15 15:36:07 +07:00
<sql id="profileColumns">
U.id userId,
U.username userUsername,
U.bio userBio,
U.image userImage
</sql>
2017-08-15 10:21:55 +07:00
<sql id="selectArticleData">
2017-08-15 09:47:18 +07:00
select
2017-08-15 10:21:55 +07:00
A.id articleId,
A.slug articleSlug,
A.title articleTitle,
A.description articleDescription,
A.body articleBody,
A.created_at articleCreatedAt,
A.updated_at articleUpdatedAt,
2017-08-16 14:58:51 +07:00
T.name tagName,
2017-08-15 15:36:07 +07:00
<include refid="profileColumns"/>
2017-08-15 09:47:18 +07:00
from
2017-08-15 10:21:55 +07:00
articles A
left join article_tags AT on A.id = AT.article_id
2017-08-16 14:58:51 +07:00
left join tags T on T.id = AT.tag_id
2017-08-15 10:21:55 +07:00
left join users U on U.id = A.user_id
</sql>
2017-08-21 16:25:00 +07:00
<select id="findById" resultMap="transfer.data.articleData">
2017-08-15 10:21:55 +07:00
<include refid="selectArticleData"/>
2017-08-15 09:47:18 +07:00
where A.id = #{id}
</select>
2017-08-21 16:25:00 +07:00
<select id="findBySlug" resultMap="transfer.data.articleData">
2017-08-15 10:21:55 +07:00
<include refid="selectArticleData"/>
where A.slug = #{slug}
</select>
2017-08-17 13:27:29 +07:00
<select id="queryArticles" resultMap="articleId">
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
2017-08-17 13:27:29 +07:00
<where>
<if test="tag != null">
T.name = #{tag}
</if>
<if test="author != null">
AND AU.username = #{author}
2017-08-17 13:27:29 +07:00
</if>
<if test="favoritedBy != null">
AND AFU.username = #{favoritedBy}
2017-08-17 13:27:29 +07:00
</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
2017-08-17 13:27:29 +07:00
<where>
<if test="tag != null">
T.name = #{tag}
</if>
<if test="author != null">
AND AU.username = #{author}
2017-08-17 13:27:29 +07:00
</if>
<if test="favoritedBy != null">
AND AFU.username = #{favoritedBy}
2017-08-17 13:27:29 +07:00
</if>
</where>
</select>
2017-08-21 16:25:00 +07:00
<select id="findArticles" resultMap="transfer.data.articleData">
2017-08-17 13:27:29 +07:00
<include refid="selectArticleData"/>
where A.id in
<foreach index="index" collection="articleIds" item="id" open="(" separator="," close=")">
#{id}
</foreach>
2017-08-18 16:08:27 +07:00
order by A.created_at desc
2017-08-17 13:27:29 +07:00
</select>
2017-08-21 16:25:00 +07:00
<select id="findArticlesOfAuthors" resultMap="transfer.data.articleData">
2017-08-17 16:17:37 +07:00
<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>
2017-08-17 13:27:29 +07:00
<resultMap id="articleId" type="string">
<id javaType="string" column="articleId"/>
</resultMap>
2017-08-15 09:47:18 +07:00
</mapper>