调整数据库字段,优化部分接口 #1

Merged
xiaohai2271 merged 33 commits from dev into master 2020-05-27 16:45:03 +08:00
3 changed files with 210 additions and 72 deletions
Showing only changes of commit 9185ff8f58 - Show all commits

View File

@@ -8,8 +8,8 @@ import java.util.List;
/**
* @Author: 小海
* @Date 2019/06/30 16:19
* @Description
* @Date: 2019/06/30 16:19
* @Description:
*/
@Mapper
@Repository
@@ -18,11 +18,9 @@ public interface CommentMapper {
int updateContent(String content, long id);
int updateResponder(String responder, long id);
int delete(long id);
int deleteByArticleId(long articleId);
int deleteByPagePath(String pagePath);
boolean existsById(long id);
@@ -30,19 +28,13 @@ public interface CommentMapper {
Comment getLastestComment();
List<Comment> findAllByAuthorIDAndType(long id, boolean isComment);
List<Comment> findAllByFromUser(long id);
List<Comment> findAllByPId(long pid);
List<Comment> findAllByPid(long pid);
List<Comment> findAllByArticleID(long articleId);
List<Comment> findAllByPagePath(String pagePath);
List<Comment> findAllByArticleIDAndPId(long articleID, long pid);
List<Comment> findAllByPagePathAndPid(String pagePath, long pid);
List<Comment> findCommentsByTypeAndPId(boolean isComment, long pid);
List<Comment> findAllByPId(int pid);
List<Comment> findAllByType(boolean isComment);
long countByType(boolean isComment);
long countByPagePath(String pagePath);
}

View File

@@ -3,21 +3,44 @@
<mapper namespace="cn.celess.blog.mapper.CommentMapper">
<resultMap id="commentResultMap" type="cn.celess.blog.entity.Comment">
<id column="co_id" property="id"/>
<result column="co_article_id" property="articleID"/>
<result column="is_comment" property="type"/>
<result column="author_id" property="authorID"/>
<result column="co_page_path" property="pagePath"/>
<result column="co_status" property="status"/>
<result column="co_content" property="content"/>
<result column="co_date" property="date"/>
<result column="co_pid" property="pid"/>
<result column="co_response_id" property="responseId"/>
<result column="co_from_author_id" property="fromUser.id"/>
<result column="co_to_author_id" property="toUser.id"/>
<result column="is_delete" property="delete"/>
</resultMap>
<insert id="insert">
insert into comment (co_article_id, is_comment, author_id, co_content, co_date, co_pid)
VALUES (#{articleID}, #{type}, #{authorID}, #{content}, #{date}, #{pid})
<selectKey resultType="java.lang.Long" keyProperty="id">
SELECT LAST_INSERT_ID() AS id
</selectKey>
<resultMap id="commentViewResultMap" type="cn.celess.blog.entity.Comment">
<id column="commentId" property="id"/>
<result column="pagePath" property="pagePath"/>
<result column="status" property="status"/>
<result column="content" property="content"/>
<result column="date" property="date"/>
<result column="pid" property="pid"/>
<result column="fromAuthorId" property="fromUser.id"/>
<result column="toAuthorId" property="toUser.id"/>
<result column="isDelete" property="delete"/>
<association property="fromUser" column="fromAuthorId" javaType="cn.celess.blog.entity.User">
<id column="fromAuthorId" property="id"/>
<result column="fromAuthorEmail" property="email"/>
<result column="fromAuthorDisplayName" property="displayName"/>
<result column="fromAuthorAvatar" property="avatarImgUrl"/>
</association>
<association property="toUser" column="toAuthorId" javaType="cn.celess.blog.entity.User">
<id column="toAuthorId" property="id"/>
<result column="toAuthorEmail" property="email"/>
<result column="toAuthorDisplayName" property="displayName"/>
<result column="toAuthorAvatar" property="avatarImgUrl"/>
</association>
</resultMap>
<insert id="insert" useGeneratedKeys="true" keyProperty="id">
insert into comment (co_page_path, co_content, co_date, co_pid, co_from_author_id, co_to_author_id, is_delete)
VALUES (#{pagePath}, #{content}, now(), #{pid}, #{fromUser.id}, #{toUser.id}, false)
</insert>
<update id="updateContent">
@@ -26,71 +49,62 @@
where co_id = #{id}
</update>
<update id="updateResponder">
<update id="delete">
update comment
set co_response_id =#{responder}
set is_delete = true
where co_id = #{id}
</update>
<delete id="delete">
delete
from comment
where co_id = #{id}
</delete>
<delete id="deleteByArticleId">
delete
from comment
where co_article_id = #{articleId}
</delete>
<update id="deleteByPagePath">
update comment
set is_delete = true
where co_page_path = #{path}
</update>
<select id="existsById" resultType="java.lang.Boolean">
SELECT EXISTS(SELECT * FROM comment WHERE co_id = #{id})
</select>
<select id="findCommentById" resultMap="commentResultMap">
<select id="findCommentById" resultMap="commentViewResultMap">
select *
from comment
where co_id = #{id}
from commentView
where commentId = #{id}
</select>
<select id="findAllByAuthorIDAndType" resultMap="commentResultMap">
<select id="findAllByFromUser" resultMap="commentViewResultMap">
select *
from comment
where author_id = #{id}
and is_comment = #{isComment}
from commentView
where fromAuthorId = #{id}
</select>
<select id="findAllByPId" resultMap="commentResultMap">
<select id="findAllByPid" resultMap="commentViewResultMap">
select *
from comment
where co_pid = #{pid}
from commentView
where pid = #{pid}
</select>
<select id="findAllByArticleID" resultMap="commentResultMap">
<select id="findAllByPagePath" resultMap="commentViewResultMap">
select *
from comment
where co_article_id = #{articleId}
from commentView
where pagePath = #{pagePath}
</select>
<select id="findAllByArticleIDAndPId" resultMap="commentResultMap">
<select id="findAllByPagePathAndPid" resultMap="commentViewResultMap">
select *
from comment
where co_article_id = #{articleID}
and co_pid = #{pid}
from commentView
where pagePath = #{pagePath}
and pid = #{pid}
</select>
<select id="findCommentsByTypeAndPId" resultMap="commentResultMap">
select *
from comment
where is_comment = #{isComment}
and co_pid = #{pid}
</select>
<select id="findAllByType" resultMap="commentResultMap">
select *
from comment
where is_comment = #{isComment}
</select>
<select id="countByType" resultType="java.lang.Long">
<select id="countByPagePath" resultType="java.lang.Long">
select count(*)
from comment
where is_comment = #{isComment}
from commentView
where pagePath = #{pagePath}
</select>
<select id="getLastestComment" resultMap="commentResultMap">
<select id="getLastestComment" resultMap="commentViewResultMap">
select *
from comment
order by co_id desc
from commentView
order by commentId desc
limit 1
</select>

View File

@@ -0,0 +1,132 @@
package cn.celess.blog.mapper;
import cn.celess.blog.BaseTest;
import cn.celess.blog.entity.Comment;
import cn.celess.blog.entity.User;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
import static org.junit.Assert.*;
public class CommentMapperTest extends BaseTest {
@Autowired
UserMapper userMapper;
@Autowired
CommentMapper commentMapper;
@Test
public void insert() {
Comment comment = generateComment();
assertNotNull(comment.getId());
}
@Test
public void updateContent() {
Comment comment = generateComment();
comment.setContent(randomStr(10));
assertEquals(1, commentMapper.updateContent(comment.getContent(), comment.getId()));
}
@Test
public void delete() {
Comment comment = generateComment();
assertEquals(1, commentMapper.delete(comment.getId()));
Comment commentById = commentMapper.findCommentById(comment.getId());
assertTrue(commentById.isDelete());
}
@Test
public void deleteByPagePath() {
Comment comment = generateComment();
assertTrue(commentMapper.deleteByPagePath(comment.getPagePath()) >= 1);
Comment commentById = commentMapper.findCommentById(comment.getId());
assertTrue(commentById.isDelete());
}
@Test
public void existsById() {
Comment comment = generateComment();
assertTrue(commentMapper.existsById(comment.getId()));
}
@Test
public void findCommentById() {
Comment comment = generateComment();
assertNotNull(commentMapper.findCommentById(comment.getId()));
}
@Test
public void getLastestComment() {
Comment comment = generateComment();
Comment lastestComment = commentMapper.getLastestComment();
assertEquals(comment.getId(), lastestComment.getId());
}
@Test
public void findAllByFromUser() {
Comment comment = generateComment();
List<Comment> allByFromUser = commentMapper.findAllByFromUser(comment.getFromUser().getId());
assertNotEquals(0, allByFromUser);
allByFromUser.forEach(comment1 -> assertEquals(comment.getFromUser().getId(), comment1.getFromUser().getId()));
}
@Test
public void findAllByPid() {
Comment comment = generateComment();
List<Comment> allByPid = commentMapper.findAllByPid(comment.getPid());
assertTrue(allByPid.size() >= 1);
}
@Test
public void findAllByPagePath() {
Comment comment = generateComment();
List<Comment> allByPagePath = commentMapper.findAllByPagePath(comment.getPagePath());
assertTrue(allByPagePath.size() >= 1);
allByPagePath.forEach(comment1 -> assertEquals(comment.getPagePath(), comment1.getPagePath()));
}
@Test
public void findAllByPagePathAndPid() {
Comment comment = generateComment();
List<Comment> allByPagePathAndPid = commentMapper.findAllByPagePathAndPid(comment.getPagePath(), comment.getPid());
assertTrue(allByPagePathAndPid.size() >= 1);
allByPagePathAndPid.forEach(comment1 -> {
assertEquals(comment.getPagePath(), comment1.getPagePath());
assertEquals(comment.getPid(), comment1.getPid());
});
}
@Test
public void testFindAllByPid() {
Comment comment = generateComment();
List<Comment> findAllByPid = commentMapper.findAllByPid(comment.getPid());
assertTrue(findAllByPid.size() >= 1);
findAllByPid.forEach(comment1 -> assertEquals(comment.getPid(), comment1.getPid()));
}
@Test
public void countByType() {
Comment comment = generateComment();
long l = commentMapper.countByPagePath(comment.getPagePath());
assertTrue(l >= 1);
}
private Comment generateComment() {
User from = userMapper.findById(1);
assertNotNull(from);
User to = userMapper.findById(2);
assertNotNull(to);
Comment comment = new Comment();
comment.setContent(randomStr(8));
comment.setFromUser(from);
comment.setToUser(to);
comment.setPagePath("/tags");
comment.setPid(-1L);
commentMapper.insert(comment);
return comment;
}
}