refactor query mappers

This commit is contained in:
aisensiy 2017-08-21 17:25:00 +08:00
parent 4550e5a3c5
commit 8e7ec76774
5 changed files with 50 additions and 48 deletions

View File

@ -2,11 +2,10 @@ package io.spring.application;
import io.spring.application.data.ArticleData; import io.spring.application.data.ArticleData;
import io.spring.application.data.ArticleDataList; import io.spring.application.data.ArticleDataList;
import io.spring.infrastructure.mybatis.readservice.UserRelationshipQueryService;
import io.spring.core.user.User; import io.spring.core.user.User;
import io.spring.infrastructure.mybatis.readservice.ArticleReadService; import io.spring.infrastructure.mybatis.readservice.ArticleReadService;
import lombok.Data; import io.spring.infrastructure.mybatis.readservice.UserRelationshipQueryService;
import lombok.NoArgsConstructor; import lombok.Value;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -130,9 +129,8 @@ public class ArticleQueryService {
} }
} }
@Data @Value
@NoArgsConstructor
class ArticleFavoriteCount { class ArticleFavoriteCount {
private String id; private String id;
private int count; private Integer count;
} }

View File

@ -7,7 +7,7 @@
<select id="articleFavoriteCount" resultType="java.lang.Integer"> <select id="articleFavoriteCount" resultType="java.lang.Integer">
select count(1) from article_favorites where article_id = #{articleId} select count(1) from article_favorites where article_id = #{articleId}
</select> </select>
<select id="articlesFavoriteCount" resultMap="favoriteCount"> <select id="articlesFavoriteCount" resultMap="transfer.data.favoriteCount">
select A.id, count(AF.user_id) as favoriteCount from articles A select A.id, count(AF.user_id) as favoriteCount from articles A
left join article_favorites AF on A.id = AF.article_id left join article_favorites AF on A.id = AF.article_id
where id in where id in
@ -27,10 +27,4 @@
</foreach> </foreach>
and AF.user_id = #{currentUser.id} and AF.user_id = #{currentUser.id}
</select> </select>
<resultMap id="favoriteCount" type="io.spring.application.ArticleFavoriteCount">
<id column="id" property="id"/>
<result column="favoriteCount" property="count"/>
</resultMap>
</mapper> </mapper>

View File

@ -25,11 +25,11 @@
left join users U on U.id = A.user_id left join users U on U.id = A.user_id
</sql> </sql>
<select id="findById" resultMap="articleData"> <select id="findById" resultMap="transfer.data.articleData">
<include refid="selectArticleData"/> <include refid="selectArticleData"/>
where A.id = #{id} where A.id = #{id}
</select> </select>
<select id="findBySlug" resultMap="articleData"> <select id="findBySlug" resultMap="transfer.data.articleData">
<include refid="selectArticleData"/> <include refid="selectArticleData"/>
where A.slug = #{slug} where A.slug = #{slug}
</select> </select>
@ -79,7 +79,7 @@
</if> </if>
</where> </where>
</select> </select>
<select id="findArticles" resultMap="articleData"> <select id="findArticles" resultMap="transfer.data.articleData">
<include refid="selectArticleData"/> <include refid="selectArticleData"/>
where A.id in where A.id in
<foreach index="index" collection="articleIds" item="id" open="(" separator="," close=")"> <foreach index="index" collection="articleIds" item="id" open="(" separator="," close=")">
@ -87,7 +87,7 @@
</foreach> </foreach>
order by A.created_at desc order by A.created_at desc
</select> </select>
<select id="findArticlesOfAuthors" resultMap="articleData"> <select id="findArticlesOfAuthors" resultMap="transfer.data.articleData">
<include refid="selectArticleData"/> <include refid="selectArticleData"/>
where A.user_id in where A.user_id in
<foreach index="index" collection="authors" item="id" open="(" separator="," close=")"> <foreach index="index" collection="authors" item="id" open="(" separator="," close=")">
@ -105,25 +105,4 @@
<resultMap id="articleId" type="string"> <resultMap id="articleId" type="string">
<id javaType="string" column="articleId"/> <id javaType="string" column="articleId"/>
</resultMap> </resultMap>
<resultMap id="articleData" type="io.spring.application.data.ArticleData">
<id column="articleId" property="id"/>
<result column="articleSlug" property="slug"/>
<result column="articleTitle" property="title"/>
<result column="articleDescription" property="description"/>
<result column="articleBody" property="body"/>
<result column="articleCreatedAt" property="createdAt"/>
<result column="articleUpdatedAt" property="updatedAt"/>
<association property="profileData" resultMap="profileData"/>
<collection property="tagList" javaType="list" ofType="string">
<result column="tagName"/>
</collection>
</resultMap>
<resultMap id="profileData" type="io.spring.application.data.ProfileData">
<id column="userId" property="id"/>
<result column="userUsername" property="username"/>
<result column="userBio" property="bio"/>
<result column="userImage" property="image"/>
</resultMap>
</mapper> </mapper>

View File

@ -12,20 +12,12 @@
on C.user_id = U.id on C.user_id = U.id
</sql> </sql>
<select id="findById" resultMap="commentData"> <select id="findById" resultMap="transfer.data.commentData">
<include refid="selectCommentData"/> <include refid="selectCommentData"/>
where C.id = #{id} where C.id = #{id}
</select> </select>
<select id="findByArticleId" resultMap="commentData"> <select id="findByArticleId" resultMap="transfer.data.commentData">
<include refid="selectCommentData"/> <include refid="selectCommentData"/>
where C.article_id = #{articleId} where C.article_id = #{articleId}
</select> </select>
<resultMap id="commentData" type="io.spring.application.data.CommentData">
<id column="commentId" property="id"/>
<result column="commentBody" property="body"/>
<result column="commentCreatedAt" property="createdAt"/>
<result column="commentCreatedAt" property="updatedAt"/>
<association property="profileData" resultMap="io.spring.infrastructure.mybatis.readservice.ArticleReadService.profileData"/>
</resultMap>
</mapper> </mapper>

View File

@ -0,0 +1,39 @@
<?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="transfer.data">
<resultMap id="favoriteCount" type="io.spring.application.ArticleFavoriteCount">
<constructor>
<idArg column="id" javaType="string"/>
<arg column="favoriteCount" javaType="integer"/>
</constructor>
</resultMap>
<resultMap id="profileData" type="io.spring.application.data.ProfileData">
<id column="userId" property="id"/>
<result column="userUsername" property="username"/>
<result column="userBio" property="bio"/>
<result column="userImage" property="image"/>
</resultMap>
<resultMap id="articleData" type="io.spring.application.data.ArticleData">
<id column="articleId" property="id"/>
<result column="articleSlug" property="slug"/>
<result column="articleTitle" property="title"/>
<result column="articleDescription" property="description"/>
<result column="articleBody" property="body"/>
<result column="articleCreatedAt" property="createdAt"/>
<result column="articleUpdatedAt" property="updatedAt"/>
<association property="profileData" resultMap="transfer.data.profileData"/>
<collection property="tagList" javaType="list" ofType="string">
<result column="tagName"/>
</collection>
</resultMap>
<resultMap id="commentData" type="io.spring.application.data.CommentData">
<id column="commentId" property="id"/>
<result column="commentBody" property="body"/>
<result column="commentCreatedAt" property="createdAt"/>
<result column="commentCreatedAt" property="updatedAt"/>
<association property="profileData" resultMap="profileData"/>
</resultMap>
</mapper>