力铭功成
简体中文 | English | Español
Avatar

力铭功成

我是力铭,一个热爱科普与跨文化交流的生活记录者。这里有我的日常,也有我对世界的观察,欢迎来到我的博客!

牛客社区3.22添加评论

·

添加评论

dao

CommentMapper

int insertComment(Comment comment);

    <insert id="insertComment" parameterType="Comment">
        insert into comment (<include refid="insertFields"></include>)
        values(# {userId},# {entityType},# {entityId},# {targetId},# {content},# {status},# {createTime})
    </insert>

DiscussPostMapper

冗余commentcount 添加评论要更新

int updateCommentCount(int id,int commentCount);

    <update id="updateCommentCount">
        update discuss_post set comment_count =# {commentCount} where id=# {id}
    </update>

服务层

CommentService
 //事务 对两个表的操作
    @Transactional(isolation = Isolation.READ_COMMITTED,propagation = Propagation.REQUIRED)
    public int addComment(Comment comment){
        if(comment==null){
            throw new IllegalArgumentException("参数不能为空!");
        }

        //添加评论
        comment.setContent(HtmlUtils.htmlEscape(comment.getContent()));
        comment.setContent(sensitiveFilter.filter(comment.getContent()));
        int rows = commentMapper.insertComment(comment);

        //更新帖子评论数量
        if(comment.getStatus()==ENTITY_TYPE_POST){
            //是帖子时entityid是post.id  是评论时它就是comment.id
            int count = commentMapper.selectCountByEntity(comment.getEntityType(), comment.getEntityId());
            discussPostMapper.updateCommentCount(comment.getEntityId(),count);
        }

        return rows;

    }

控制层

CommentController


@Controller
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;

    @Autowired
    private HostHolder hostHolder;

    //评论完跳到原页 所以需要discussPostId
    @PostMapping(path="/add/{discussPostId}")
    public String addComment(@PathVariable("discussPostId") int discussPostId,Comment comment){
        //可能没登录 以后再处理
        comment.setUserId(hostHolder.getUser().getId());
        comment.setStatus(0);//正常
        comment.setCreateTime(new Date());
        
        return "redirect:/discuss/detail/"+discussPostId;
    }
}

模板

给帖子的评论
1.method th:action
2.name字段与实体对应
3.隐藏提交
<form class="replyform" method="post" th:action="@{|/comment/add/${post.id}|}">
	<p class="mt-3">
		<a name="replyform"></a>
		<textarea placeholder="在这里畅所欲言你的看法吧!" name="content"></textarea>
		<input type="hidden" name="entityType" value="1">
		<input type="hidden" name="entityId" th:value="${post.id}">
	</p>
给评论的回复有两种  指不指向一个人
不指向人
和刚才一样 
注意button应该是submit
<form method="post" th:action="@{|/comment/add/${post.id}|}">
	<div>
		<input type="text" class="input-size" name="content" placeholder="请输入你的观点"/>
		<input type="hidden" name="entityType" value="2">
		<input type="hidden" name="entityId" th:value="${cvo.comment.id}">
	</div>
	<div class="text-right mt-2">
		<button type="submit" class="btn btn-primary btn-sm" onclick="# ">&nbsp;&nbsp;回&nbsp;&nbsp;复&nbsp;&nbsp;</button>
	</div>
</form>


<div>
<input type="text" class="input-size" name="content" th:placeholder="|回复${rvo.user.username}|"/>
<input type="hidden" name="entityType" value="2">
<input type="hidden" name="entityId" th:value="${cvo.comment.id}">
<input type="hidden" name="targetId" th:value="${rvo.user.id}"><!-- 新增回复的target是之前回复的发布者  -->
</div>

Bug

1.controller忘记insert了

2.服务层写错了 应该是getentity不是getstatus 粗心 而且应该用discusspostservice 不是mapper

3.int updateCommentCount(@Param(“id”)int id,@Param(“commentCount”)int commentCount); id之前也不一致

4.给别人的回复button 忘记改了

<button type="submit" class="btn btn-primary btn-sm" onclick="# ">&nbsp;&nbsp;回&nbsp;&nbsp;复&nbsp;&nbsp;</button>

© 2026 力铭功成

Built with Astro