1.CommentMapper:
import com.nowcoder.community.entity.Comment;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface CommentMapper {
List<Comment> selectCommentByEntity(int entityType,int entityId,int offset,int limit);
int selectCountByEntity(int entityType,int entityId);
int insertComment(Comment comment);
}
2.CommentMapper.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.nowcoder.community.mapper.CommentMapper">
<select id="selectCommentByEntity" resultType="comment">
select * from comment where status=0 and entity_type=#{entityType} and entity_id=#{entityId} order by create_time asc limit #{offset},#{limit};
</select>
<select id="selectCountByEntity" resultType="int">
select count(id) from comment where status=0 and entity_type=#{entityType} and entity_id=#{entityId};
</select>
<insert id="insertComment" keyProperty="id" parameterType="Comment">
insert into comment(user_id, entity_type, entity_id, target_id, content, status, create_time) VALUES (#{userId},#{entityType},#{entityId},#{targetId},#{content},#{status},#{createTime});
</insert>
</mapper>
3.CommentService:
import com.nowcoder.community.entity.Comment;
import com.nowcoder.community.mapper.CommentMapper;
import com.nowcoder.community.util.CommunityConstant;
import com.nowcoder.community.util.SensitiveFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.util.HtmlUtils;
import java.util.List;
@Service
public class CommentServiceImpl implements CommentService, CommunityConstant {
@Autowired
private CommentMapper commentMapper;
@Autowired
private SensitiveFilter sensitiveFilter;
@Autowired
private DiscussPostServiceImpl discussPostService;
@Override
public List<Comment> selectCommentByEntity(int entityType, int entityId, int offset, int limit) {
return commentMapper.selectCommentByEntity(entityType, entityId, offset, limit);
}
@Override
public int selectCountByEntity(int entityType, int entityId) {
return commentMapper.selectCountByEntity(entityType, entityId);
}
@Override
public int insertComment(Comment comment) {
return commentMapper.insertComment(comment);
}
@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.getEntityType() == ENTITY_TYPE_POST) {
int count = commentMapper.selectCountByEntity(comment.getEntityType(), comment.getEntityId());
discussPostService.updateCommentCount(comment.getEntityId(), count);
}
return rows;
}
}
4.编写Controller层:
import com.nowcoder.community.entity.Comment;
import com.nowcoder.community.entity.User;
import com.nowcoder.community.service.CommentService;
import com.nowcoder.community.util.HostHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import java.util.Date;
@Controller
@RequestMapping("/comment")
public class CommentController {
@Autowired
private CommentService commentService;
@Autowired
private HostHolder hostHolder;
//添加评论
@RequestMapping(path = "/add/{discussPostId}", method = RequestMethod.POST)
public String addComment(@PathVariable("discussPostId") int discussPostId, Comment comment,String username){
//客户端表单中向服务器需要提交以下内容
/*
private int id;
private int userId;
private int entityType; //评论的类型,比如帖子的评论,评论用户评论的评论
private int entityId; //评论的帖子的id
private int targetId; //记录评论指向的人
private String content; //评论的内容
private int status; //表明状态,是否被拉黑
private Date createTime; //创建的时间*/
//其中 entityType entityId targetId content 需要表单中进行提交
/* comment.setId(user.getId());
String username = user.getUsername();
System.out.println(username);
model.addAttribute("username",username);*/
System.out.println(username);
comment.setUserId(hostHolder.getUser().getId());
comment.setStatus(0);
comment.setCreateTime(new Date());
commentService.addComment(comment);
//添加成功后,重定向至帖子详情页
return "redirect:/discuss/detail/" + discussPostId;
}
}