调整数据库字段,优化部分接口 #1
@@ -3,6 +3,7 @@ package cn.celess.blog.entity;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author : xiaohai
|
||||
@@ -41,14 +42,19 @@ public class Article {
|
||||
|
||||
private Date updateDate = null;
|
||||
|
||||
@Deprecated
|
||||
private Long categoryId;
|
||||
|
||||
@Deprecated
|
||||
private String tagsId;
|
||||
|
||||
@Deprecated
|
||||
private Long authorId;
|
||||
|
||||
@Deprecated
|
||||
private Long preArticleId;
|
||||
|
||||
@Deprecated
|
||||
private Long nextArticleId;
|
||||
|
||||
private Long readingNumber;
|
||||
@@ -58,4 +64,13 @@ public class Article {
|
||||
*/
|
||||
private Boolean open;
|
||||
|
||||
private Category category;
|
||||
|
||||
private List<Tag> tags;
|
||||
|
||||
private Integer likeCount;
|
||||
|
||||
private Integer dislikeCount;
|
||||
|
||||
private User user;
|
||||
}
|
||||
|
||||
@@ -21,10 +21,13 @@ public interface ArticleMapper {
|
||||
|
||||
int update(Article a);
|
||||
|
||||
@Deprecated
|
||||
int updateNextArticleId(long targetArticleID, long nextArticleID);
|
||||
|
||||
@Deprecated
|
||||
int updatePreArticleId(long targetArticleID, long preArticleID);
|
||||
|
||||
@Deprecated
|
||||
long getLastestArticleId();
|
||||
|
||||
Article getLastestArticle();
|
||||
@@ -33,7 +36,7 @@ public interface ArticleMapper {
|
||||
|
||||
boolean existsByTitle(String title);
|
||||
|
||||
boolean existsById(long id);
|
||||
boolean isDeletedById(long id);
|
||||
|
||||
List<Article> findAllByAuthorId(long authorID);
|
||||
|
||||
@@ -51,8 +54,11 @@ public interface ArticleMapper {
|
||||
|
||||
List<Article> getSimpleInfoByTag(List<String> idList);
|
||||
|
||||
@Deprecated
|
||||
int setReadingNumber(long number, long id);
|
||||
|
||||
int updateReadingNumber(long id);
|
||||
|
||||
long count();
|
||||
|
||||
}
|
||||
|
||||
@@ -2,8 +2,8 @@
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
|
||||
<mapper namespace="cn.celess.blog.mapper.CategoryMapper">
|
||||
<resultMap id="categoryResultMap" type="cn.celess.blog.entity.Category">
|
||||
<id column="c_id" property="id"/>
|
||||
<result column="c_name" property="name"/>
|
||||
<id column="t_id" property="id"/>
|
||||
<result column="t_name" property="name"/>
|
||||
</resultMap>
|
||||
|
||||
<insert id="insert">
|
||||
|
||||
@@ -6,78 +6,91 @@
|
||||
<result column="a_title" property="title"/>
|
||||
<result column="a_summary" property="summary"/>
|
||||
<result column="a_md_content" property="mdContent"/>
|
||||
<result column="a_tags_id" property="tagsId"/>
|
||||
<result column="a_category_id" property="categoryId"/>
|
||||
<result column="a_url" property="url"/>
|
||||
<result column="a_author_id" property="authorId"/>
|
||||
<result column="a_is_open" property="open"/>
|
||||
<result column="a_is_original" property="type"/>
|
||||
<result column="next_a_id" property="nextArticleId"/>
|
||||
<result column="pre_a_id" property="preArticleId"/>
|
||||
<!-- <result column="next_a_id" property="nextArticleId"/>-->
|
||||
<!-- <result column="pre_a_id" property="preArticleId"/>-->
|
||||
<result column="a_reading_number" property="readingNumber"/>
|
||||
<result column="a_publish_date" property="publishDate"/>
|
||||
<result column="a_update_date" property="updateDate"/>
|
||||
<association property="category" column="a_category_id" javaType="cn.celess.blog.entity.Category"
|
||||
resultMap="cn.celess.blog.mapper.CategoryMapper.categoryResultMap">
|
||||
</association>
|
||||
<collection property="tags" ofType="cn.celess.blog.entity.Article"
|
||||
resultMap="cn.celess.blog.mapper.TagMapper.tagResultMap">
|
||||
</collection>
|
||||
</resultMap>
|
||||
|
||||
<insert id="insert" parameterType="cn.celess.blog.entity.Article">
|
||||
insert into article (a_author_id, a_category_id, a_tags_id, a_md_content, a_publish_date,
|
||||
|
||||
<resultMap id="articleViewResultMap" type="cn.celess.blog.entity.Article">
|
||||
<id column="articleId" property="id"/>
|
||||
<result column="title" property="title"/>
|
||||
<result column="summary" property="summary"/>
|
||||
<result column="mdContent" property="mdContent"/>
|
||||
<result column="url" property="url"/>
|
||||
<result column="isOpen" property="open"/>
|
||||
<result column="isOriginal" property="type"/>
|
||||
<result column="likeCount" property="likeCount"/>
|
||||
<result column="dislikeCount" property="dislikeCount"/>
|
||||
<result column="readingCount" property="readingNumber"/>
|
||||
<result column="publishDate" property="publishDate"/>
|
||||
<result column="updateDate" property="updateDate"/>
|
||||
<association property="category" column="categoryId" javaType="cn.celess.blog.entity.Category">
|
||||
<id column="categoryId" property="id"/>
|
||||
<result column="categoryName" property="name"/>
|
||||
</association>
|
||||
<association property="user" column="authorId" javaType="cn.celess.blog.entity.User">
|
||||
<id column="authorId" property="id"/>
|
||||
<result column="userEmail" property="email"/>
|
||||
<result column="userAvatar" property="avatarImgUrl"/>
|
||||
<result column="userDisplayName" property="displayName"/>
|
||||
</association>
|
||||
<collection property="tags" ofType="cn.celess.blog.entity.Tag">
|
||||
<id column="tagId" property="id"/>
|
||||
<result column="tagName" property="name"/>
|
||||
</collection>
|
||||
|
||||
</resultMap>
|
||||
|
||||
<insert id="insert" useGeneratedKeys="true" keyProperty="id" parameterType="cn.celess.blog.entity.Article">
|
||||
insert into article (a_author_id, a_category_id, a_md_content, a_publish_date,
|
||||
a_summary, a_title, a_url)
|
||||
values (#{authorId}, #{categoryId}, #{tagsId}, #{mdContent}, #{publishDate},
|
||||
values (#{user.id}, #{category.id}, #{mdContent}, #{publishDate},
|
||||
#{summary}, #{title}, #{url})
|
||||
<selectKey resultType="java.lang.Long" keyProperty="id">
|
||||
SELECT LAST_INSERT_ID() AS id
|
||||
</selectKey>
|
||||
</insert>
|
||||
<delete id="delete">
|
||||
delete
|
||||
from article
|
||||
<update id="delete">
|
||||
update article
|
||||
set is_delete = true
|
||||
where a_id = #{id}
|
||||
</delete>
|
||||
</update>
|
||||
|
||||
<update id="update">
|
||||
update article
|
||||
set
|
||||
set a_update_date=now(),
|
||||
<if test="title!=null">a_title=#{title},</if>
|
||||
<if test="mdContent!=null">a_md_content=#{mdContent},</if>
|
||||
<if test="summary!=null">a_summary=#{summary},</if>
|
||||
<if test="type!=null">a_is_original=#{type},</if>
|
||||
<if test="url!=null">a_url=#{url},</if>
|
||||
<if test="updateDate!=null">a_update_date=#{updateDate},</if>
|
||||
<if test="categoryId!=null">a_category_id=#{categoryId},</if>
|
||||
<if test="tagsId!=null">a_tags_id=#{tagsId},</if>
|
||||
<if test="nextArticleId!=null">next_a_id=#{nextArticleId},</if>
|
||||
<if test="preArticleId!=null">pre_a_id=#{preArticleId},</if>
|
||||
<if test="category!=null">a_category_id=#{category.id},</if>
|
||||
<if test="open!=null">a_is_open=#{open}</if>
|
||||
where a_id = #{id}
|
||||
</update>
|
||||
|
||||
<update id="updateNextArticleId">
|
||||
<update id="updateReadingNumber">
|
||||
update article
|
||||
set next_a_id=#{nextArticleID}
|
||||
where a_id = #{targetArticleID}
|
||||
</update>
|
||||
|
||||
<update id="updatePreArticleId">
|
||||
update article
|
||||
set pre_a_id=#{preArticleID}
|
||||
where a_id = #{targetArticleID}
|
||||
</update>
|
||||
<update id="setReadingNumber">
|
||||
update article
|
||||
set a_reading_number=#{number}
|
||||
set a_reading_number=a_reading_number + 1
|
||||
where a_id = #{id}
|
||||
</update>
|
||||
|
||||
<select id="getLastestArticleId" resultType="long">
|
||||
select a_id
|
||||
from article
|
||||
order by a_id desc
|
||||
limit 1
|
||||
</select>
|
||||
|
||||
<select id="getLastestArticle" resultMap="articleResultMap" resultType="cn.celess.blog.entity.Article">
|
||||
select *
|
||||
from article
|
||||
from article,
|
||||
tag_category
|
||||
where tag_category.is_category = true
|
||||
and article.a_category_id = tag_category.t_id
|
||||
order by a_id desc
|
||||
limit 1
|
||||
</select>
|
||||
@@ -85,16 +98,22 @@
|
||||
|
||||
<select id="findArticleById" resultMap="articleResultMap">
|
||||
select *
|
||||
from article
|
||||
from article,
|
||||
tag_category
|
||||
where a_id = #{id}
|
||||
and tag_category.is_category = true
|
||||
and article.a_category_id = tag_category.t_id
|
||||
</select>
|
||||
|
||||
<select id="existsByTitle" resultType="boolean">
|
||||
SELECT EXISTS(SELECT * FROM article WHERE a_title = #{title})
|
||||
</select>
|
||||
|
||||
<select id="existsById" resultType="boolean">
|
||||
SELECT EXISTS(SELECT * FROM article WHERE a_id = #{id})
|
||||
<select id="isDeletedById" resultType="boolean">
|
||||
select is_delete
|
||||
from article
|
||||
WHERE a_id = #{id}
|
||||
# SELECT EXISTS(SELECT * FROM article
|
||||
</select>
|
||||
|
||||
<select id="findAllByAuthorId" resultMap="articleResultMap">
|
||||
@@ -125,10 +144,11 @@
|
||||
order by a_id desc
|
||||
</select>
|
||||
|
||||
<select id="findAll" resultMap="articleResultMap">
|
||||
<select id="findAll" resultMap="articleViewResultMap">
|
||||
select *
|
||||
from article
|
||||
order by a_id desc
|
||||
from articleView
|
||||
where isDelete = false
|
||||
order by articleId desc
|
||||
</select>
|
||||
|
||||
<select id="getSimpleInfo" resultMap="articleResultMap">
|
||||
|
||||
134
src/test/java/cn/celess/blog/mapper/ArticleMapperTest.java
Normal file
134
src/test/java/cn/celess/blog/mapper/ArticleMapperTest.java
Normal file
@@ -0,0 +1,134 @@
|
||||
package cn.celess.blog.mapper;
|
||||
|
||||
import cn.celess.blog.BaseTest;
|
||||
import cn.celess.blog.entity.Article;
|
||||
import cn.celess.blog.entity.Category;
|
||||
import cn.celess.blog.entity.Tag;
|
||||
import cn.celess.blog.entity.User;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class ArticleMapperTest extends BaseTest {
|
||||
@Autowired
|
||||
ArticleMapper articleMapper;
|
||||
|
||||
@Test
|
||||
public void insert() {
|
||||
|
||||
Article article = generateArticle();
|
||||
|
||||
articleMapper.insert(article);
|
||||
assertNotNull(article.getId());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void delete() {
|
||||
Article article = generateArticle();
|
||||
|
||||
articleMapper.insert(article);
|
||||
assertFalse(articleMapper.isDeletedById(article.getId()));
|
||||
assertEquals(1, articleMapper.delete(article.getId()));
|
||||
assertTrue(articleMapper.isDeletedById(article.getId()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void update() {
|
||||
Article article = generateArticle();
|
||||
String randomText = UUID.randomUUID().toString();
|
||||
|
||||
// 此字段不会通过insert被写入数据库而是使用插入数据的默认值 数据库中该字段默认为true
|
||||
article.setOpen(true);
|
||||
|
||||
articleMapper.insert(article);
|
||||
|
||||
article.setTitle("test update " + randomText);
|
||||
article.setMdContent("test update ");
|
||||
article.setSummary("test update ");
|
||||
article.setType(false);
|
||||
article.setUrl("https://www.celess.cn");
|
||||
article.getCategory().setId(2L);
|
||||
article.setOpen(!article.getOpen());
|
||||
articleMapper.update(article);
|
||||
|
||||
Article articleById = articleMapper.findArticleById(article.getId());
|
||||
assertEquals(article.getTitle(), articleById.getTitle());
|
||||
assertEquals(article.getMdContent(), articleById.getMdContent());
|
||||
assertEquals(article.getSummary(), articleById.getSummary());
|
||||
assertEquals(article.getType(), articleById.getType());
|
||||
assertEquals(article.getCategory().getId(), articleById.getCategory().getId());
|
||||
assertEquals(article.getOpen(), articleById.getOpen());
|
||||
assertNotNull(articleById.getUpdateDate());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void selectAll() {
|
||||
List<Article> list = articleMapper.findAll();
|
||||
assertNotEquals(0, list.size());
|
||||
list.forEach(article -> {
|
||||
assertNotEquals(0, article.getTags().size());
|
||||
assertNotNull(article.getId());
|
||||
assertNotNull(article.getTitle());
|
||||
assertNotNull(article.getSummary());
|
||||
assertNotNull(article.getMdContent());
|
||||
assertNotNull(article.getType());
|
||||
if (!article.getType()) {
|
||||
assertNotNull(article.getUrl());
|
||||
}
|
||||
assertNotNull(article.getReadingNumber());
|
||||
assertNotNull(article.getLikeCount());
|
||||
assertNotNull(article.getDislikeCount());
|
||||
assertNotNull(article.getPublishDate());
|
||||
assertNotNull(article.getOpen());
|
||||
assertNotNull(article.getCategory());
|
||||
assertNotNull(article.getUser());
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void updateReadCount() {
|
||||
Article article = generateArticle();
|
||||
articleMapper.insert(article);
|
||||
Article articleById = articleMapper.findArticleById(article.getId());
|
||||
assertEquals(Long.valueOf(0), articleById.getReadingNumber());
|
||||
articleMapper.updateReadingNumber(articleById.getId());
|
||||
articleById = articleMapper.findArticleById(article.getId());
|
||||
assertEquals(Long.valueOf(1), articleById.getReadingNumber());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void getLastestArticle() {
|
||||
Article article = generateArticle();
|
||||
articleMapper.insert(article);
|
||||
Article lastestArticle = articleMapper.getLastestArticle();
|
||||
assertNotNull(lastestArticle);
|
||||
assertEquals(article.getId(), lastestArticle.getId());
|
||||
// assertNotNull(lastestArticle.getCategory());
|
||||
// assertNotEquals(0, lastestArticle.getTags().size());
|
||||
}
|
||||
|
||||
|
||||
private Article generateArticle() {
|
||||
String randomText = UUID.randomUUID().toString();
|
||||
|
||||
Article article = new Article();
|
||||
Category category = new Category();
|
||||
category.setId(1L);
|
||||
article.setCategory(category);
|
||||
article.setTitle(" unity test for article " + randomText);
|
||||
article.setMdContent("# unity test for article");
|
||||
article.setSummary("unity test for article");
|
||||
User user = new User();
|
||||
user.setId(1L);
|
||||
article.setUser(user);
|
||||
article.setType(true);
|
||||
return article;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user