fix: 分页数据异常

This commit is contained in:
禾几海
2021-03-15 17:31:37 +08:00
parent 5632d47674
commit fafee4f918
5 changed files with 220 additions and 188 deletions

View File

@@ -1,6 +1,7 @@
package cn.celess.blog.mapper; package cn.celess.blog.mapper;
import cn.celess.blog.entity.ArticleTag; import cn.celess.blog.entity.ArticleTag;
import cn.celess.blog.entity.Tag;
import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
@@ -27,6 +28,8 @@ public interface ArticleTagMapper {
List<ArticleTag> findAllByArticleId(Long articleId); List<ArticleTag> findAllByArticleId(Long articleId);
List<Tag> findTagByArticleId(Long articleId);
int deleteMultiById(List<ArticleTag> articleTags); int deleteMultiById(List<ArticleTag> articleTags);
List<ArticleTag> findArticleByTag(Long tagId); List<ArticleTag> findArticleByTag(Long tagId);

View File

@@ -64,6 +64,14 @@
and article_tag.t_id = tag_category.t_id and article_tag.t_id = tag_category.t_id
</select> </select>
<select id="findTagByArticleId" resultMap="cn.celess.blog.mapper.TagMapper.tagResultMap">
select tag_category.*
from article_tag,
tag_category
where a_id = #{articleId}
and article_tag.t_id = tag_category.t_id
</select>
<select id="findOneById" resultMap="articleTagResultMap"> <select id="findOneById" resultMap="articleTagResultMap">
select * select *
from article_tag, from article_tag,
@@ -98,4 +106,4 @@
and article_tag.a_id = article.a_id and article_tag.a_id = article.a_id
</select> </select>
</mapper> </mapper>

View File

@@ -1,185 +1,187 @@
<?xml version="1.0" encoding="UTF-8" ?> <?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" > <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="cn.celess.blog.mapper.ArticleMapper"> <mapper namespace="cn.celess.blog.mapper.ArticleMapper">
<resultMap id="articleResultMap" type="cn.celess.blog.entity.Article"> <resultMap id="articleResultMap" type="cn.celess.blog.entity.Article">
<id column="a_id" property="id"/> <id column="a_id" property="id"/>
<result column="a_title" property="title"/> <result column="a_title" property="title"/>
<result column="a_summary" property="summary"/> <result column="a_summary" property="summary"/>
<result column="a_md_content" property="mdContent"/> <result column="a_md_content" property="mdContent"/>
<result column="a_url" property="url"/> <result column="a_url" property="url"/>
<result column="a_author_id" property="user.id"/> <result column="a_author_id" property="user.id"/>
<result column="a_is_open" property="open"/> <result column="a_is_open" property="open"/>
<result column="a_is_original" property="type"/> <result column="a_is_original" property="type"/>
<!-- <result column="next_a_id" property="nextArticleId"/>--> <!-- <result column="next_a_id" property="nextArticleId"/>-->
<!-- <result column="pre_a_id" property="preArticleId"/>--> <!-- <result column="pre_a_id" property="preArticleId"/>-->
<result column="a_reading_number" property="readingNumber"/> <result column="a_reading_number" property="readingNumber"/>
<result column="a_publish_date" property="publishDate"/> <result column="a_publish_date" property="publishDate"/>
<result column="a_update_date" property="updateDate"/> <result column="a_update_date" property="updateDate"/>
<result column="is_delete" property="deleted"/> <result column="is_delete" property="deleted"/>
<association property="category" column="a_category_id" javaType="cn.celess.blog.entity.TagCategory" <association property="category" column="a_category_id" javaType="cn.celess.blog.entity.TagCategory"
resultMap="cn.celess.blog.mapper.CategoryMapper.categoryResultMap"> resultMap="cn.celess.blog.mapper.CategoryMapper.categoryResultMap">
</association> </association>
<collection property="tags" ofType="cn.celess.blog.entity.TagCategory" <collection property="tags" ofType="cn.celess.blog.entity.Tag"
resultMap="cn.celess.blog.mapper.CategoryMapper.categoryResultMap"> select="cn.celess.blog.mapper.ArticleTagMapper.findTagByArticleId" column="a_id">
</collection> <id column="tagId" property="id"/>
</resultMap> <result column="tagName" property="name"/>
</collection>
</resultMap>
<resultMap id="articleViewResultMap" type="cn.celess.blog.entity.Article">
<id column="articleId" property="id"/>
<result column="title" property="title"/> <resultMap id="articleViewResultMap" type="cn.celess.blog.entity.Article">
<result column="summary" property="summary"/> <id column="articleId" property="id"/>
<result column="mdContent" property="mdContent"/> <result column="title" property="title"/>
<result column="url" property="url"/> <result column="summary" property="summary"/>
<result column="isOpen" property="open"/> <result column="mdContent" property="mdContent"/>
<result column="isOriginal" property="type"/> <result column="url" property="url"/>
<result column="likeCount" property="likeCount"/> <result column="isOpen" property="open"/>
<result column="dislikeCount" property="dislikeCount"/> <result column="isOriginal" property="type"/>
<result column="readingCount" property="readingNumber"/> <result column="likeCount" property="likeCount"/>
<result column="publishDate" property="publishDate"/> <result column="dislikeCount" property="dislikeCount"/>
<result column="updateDate" property="updateDate"/> <result column="readingCount" property="readingNumber"/>
<result column="isDelete" property="deleted"/> <result column="publishDate" property="publishDate"/>
<association property="category" column="categoryId" javaType="cn.celess.blog.entity.Category"> <result column="updateDate" property="updateDate"/>
<id column="categoryId" property="id"/> <result column="isDelete" property="deleted"/>
<result column="categoryName" property="name"/> <association property="category" column="categoryId" javaType="cn.celess.blog.entity.Category">
</association> <id column="categoryId" property="id"/>
<association property="user" column="authorId" javaType="cn.celess.blog.entity.User"> <result column="categoryName" property="name"/>
<id column="authorId" property="id"/> </association>
<result column="userEmail" property="email"/> <association property="user" column="authorId" javaType="cn.celess.blog.entity.User">
<result column="userAvatar" property="avatarImgUrl"/> <id column="authorId" property="id"/>
<result column="userDisplayName" property="displayName"/> <result column="userEmail" property="email"/>
</association> <result column="userAvatar" property="avatarImgUrl"/>
<collection property="tags" ofType="cn.celess.blog.entity.Tag"> <result column="userDisplayName" property="displayName"/>
<id column="tagId" property="id"/> </association>
<result column="tagName" property="name"/> <collection property="tags" ofType="cn.celess.blog.entity.Tag">
</collection> <id column="tagId" property="id"/>
<result column="tagName" property="name"/>
</resultMap> </collection>
<insert id="insert" useGeneratedKeys="true" keyProperty="id" parameterType="cn.celess.blog.entity.Article"> </resultMap>
insert into article (a_author_id, a_category_id, a_md_content, a_is_original,
a_summary, a_title, a_url) <insert id="insert" useGeneratedKeys="true" keyProperty="id" parameterType="cn.celess.blog.entity.Article">
values (#{user.id}, #{category.id}, #{mdContent}, #{type}, #{summary}, #{title}, #{url}) insert into article (a_author_id, a_category_id, a_md_content, a_is_original,
</insert> a_summary, a_title, a_url)
<update id="delete"> values (#{user.id}, #{category.id}, #{mdContent}, #{type}, #{summary}, #{title}, #{url})
update article </insert>
set is_delete = true <update id="delete">
where a_id = #{id} update article
</update> set is_delete = true
where a_id = #{id}
<update id="update"> </update>
update article
set a_update_date=now(), <update id="update">
<if test="title!=null">a_title=#{title},</if> update article
<if test="mdContent!=null">a_md_content=#{mdContent},</if> set a_update_date=now(),
<if test="summary!=null">a_summary=#{summary},</if> <if test="title!=null">a_title=#{title},</if>
<if test="type!=null">a_is_original=#{type},</if> <if test="mdContent!=null">a_md_content=#{mdContent},</if>
<if test="url!=null">a_url=#{url},</if> <if test="summary!=null">a_summary=#{summary},</if>
<if test="category!=null">a_category_id=#{category.id},</if> <if test="type!=null">a_is_original=#{type},</if>
<if test="open!=null">a_is_open=#{open}</if> <if test="url!=null">a_url=#{url},</if>
where a_id = #{id} <if test="category!=null">a_category_id=#{category.id},</if>
</update> <if test="open!=null">a_is_open=#{open}</if>
where a_id = #{id}
<update id="updateReadingNumber"> </update>
update article
set a_reading_number=a_reading_number + 1 <update id="updateReadingNumber">
where a_id = #{id} update article
</update> set a_reading_number=a_reading_number + 1
where a_id = #{id}
<select id="getLastestArticle" resultMap="articleViewResultMap" resultType="cn.celess.blog.entity.Article"> </update>
select *
from articleView <select id="getLastestArticle" resultMap="articleViewResultMap" resultType="cn.celess.blog.entity.Article">
order by articleId desc select *
limit 1 from articleView
</select> order by articleId desc
limit 1
</select>
<select id="findArticleById" resultMap="articleViewResultMap">
select *
from articleView <select id="findArticleById" resultMap="articleViewResultMap">
where articleId = #{id} select *
</select> from articleView
where articleId = #{id}
<select id="existsByTitle" resultType="boolean"> </select>
SELECT EXISTS(SELECT * FROM article WHERE a_title = #{title})
</select> <select id="existsByTitle" resultType="boolean">
SELECT EXISTS(SELECT * FROM article WHERE a_title = #{title})
<select id="isDeletedById" resultType="boolean"> </select>
select is_delete
from article <select id="isDeletedById" resultType="boolean">
WHERE a_id = #{id} select is_delete
</select> from article
WHERE a_id = #{id}
<select id="findAllByAuthorId" resultMap="articleViewResultMap"> </select>
select *
from articleView <select id="findAllByAuthorId" resultMap="articleViewResultMap">
where authorId = #{authorId} select *
and isDelete = false from articleView
order by articleId desc where authorId = #{authorId}
</select> and isDelete = false
order by articleId desc
<select id="findAllByOpen" resultMap="articleViewResultMap"> </select>
select *
from articleView <select id="findAllByOpen" resultMap="articleResultMap">
where isOpen = #{isOpen} select *
and isDelete = false from article
order by articleId desc where a_is_open = #{isOpen}
</select> and is_delete = false
order by a_id desc
</select>
<select id="getTitleById" resultType="string">
SELECT title
from articleView <select id="getTitleById" resultType="string">
where articleId = #{id} SELECT title
</select> from articleView
where articleId = #{id}
<select id="findAllByCategoryId" resultMap="articleViewResultMap"> </select>
select *
from articleView <select id="findAllByCategoryId" resultMap="articleViewResultMap">
where categoryId = #{id} select *
and isDelete = false from articleView
order by articleId desc where categoryId = #{id}
</select> and isDelete = false
order by articleId desc
<select id="findAllByCategoryIdAndOpen" resultMap="articleViewResultMap"> </select>
select *
from articleView <select id="findAllByCategoryIdAndOpen" resultMap="articleViewResultMap">
where categoryId = #{id} select *
and isDelete = false from articleView
and isOpen = true where categoryId = #{id}
order by articleId desc and isDelete = false
</select> and isOpen = true
order by articleId desc
</select>
<select id="findAll" resultMap="articleViewResultMap">
select *
from articleView <select id="findAll" resultMap="articleViewResultMap">
order by articleId desc select *
</select> from articleView
order by articleId desc
</select>
<select id="count" resultType="long">
select count(*)
from article <select id="count" resultType="long">
where is_delete = false; select count(*)
</select> from article
where is_delete = false;
<select id="getPreArticle" resultMap="articleViewResultMap"> </select>
select *
from articleView <select id="getPreArticle" resultMap="articleViewResultMap">
where articleId = (select max(articleId) select *
from articleView from articleView
where articleId &lt; #{id} where articleId = (select max(articleId)
) from articleView
</select> where articleId &lt; #{id}
<select id="getNextArticle" resultMap="articleViewResultMap"> )
select * </select>
from articleView <select id="getNextArticle" resultMap="articleViewResultMap">
where articleId = (select min(articleId) select *
from articleView from articleView
where articleId &gt; #{id} where articleId = (select min(articleId)
) from articleView
</select> where articleId &gt; #{id}
)
</select>
</mapper>
</mapper>

View File

@@ -134,4 +134,13 @@ public class ArticleTagMapperTest extends BaseTest {
return articleTag; return articleTag;
} }
}
@Test
public void findTagByArticleId() {
Article article = articleMapper.findAll().get(0);
assertNotNull(article);
List<Tag> tagByArticleId = articleTagMapper.findTagByArticleId(article.getId());
assertNotEquals(0, tagByArticleId.size());
}
}

View File

@@ -3,6 +3,7 @@ package cn.celess.blog.service;
import cn.celess.blog.BaseTest; import cn.celess.blog.BaseTest;
import cn.celess.blog.entity.model.ArticleModel; import cn.celess.blog.entity.model.ArticleModel;
import cn.celess.blog.entity.model.PageData; import cn.celess.blog.entity.model.PageData;
import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@@ -24,4 +25,13 @@ public class ArticleServiceTest extends BaseTest {
assertTrue(pageData.getList().stream().anyMatch(ArticleModel::isDeleted)); assertTrue(pageData.getList().stream().anyMatch(ArticleModel::isDeleted));
assertTrue(pageData.getList().stream().anyMatch(articleModel -> !articleModel.isDeleted())); assertTrue(pageData.getList().stream().anyMatch(articleModel -> !articleModel.isDeleted()));
} }
}
@Test
public void retrievePageForOpen() {
PageData<ArticleModel> articleModelPageData = articleService.retrievePageForOpen(10, 1);
assertEquals(10, articleModelPageData.getPageSize());
assertEquals(1, articleModelPageData.getPageNum());
assertEquals(10, articleModelPageData.getList().size());
articleModelPageData.getList().forEach(Assert::assertNotNull);
}
}