评论与之前的操作差不多
先写实体类
package com.wzh.springbootproject.entity;
import java.util.List;
public class Comment {
/** ID */
private Integer id;
/** 内容 */
private String content;
/** 评论人 */
private Integer userId;
/** 父级ID */
private Integer pid;
/** 根节点ID */
private Integer rootId;
/** 评论时间 */
private String time;
/** 博客ID */
private Integer fid;
private String module;
private String userName;
private String avatar;
private String replyUser; // 回复给谁 就是谁的名称
private List<Comment> children;
public String getReplyUser() {
return replyUser;
}
public void setReplyUser(String replyUser) {
this.replyUser = replyUser;
}
public List<Comment> getChildren() {
return children;
}
public void setChildren(List<Comment> children) {
this.children = children;
}
public String getAvatar() {
return avatar;
}
public void setAvatar(String avatar) {
this.avatar = avatar;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public Integer getPid() {
return pid;
}
public void setPid(Integer pid) {
this.pid = pid;
}
public Integer getRootId() {
return rootId;
}
public void setRootId(Integer rootId) {
this.rootId = rootId;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public Integer getFid() {
return fid;
}
public void setFid(Integer fid) {
this.fid = fid;
}
public String getModule() {
return module;
}
public void setModule(String module) {
this.module = module;
}
}
CommentController
package com.wzh.springbootproject.controller;
import com.wzh.springbootproject.common.Result;
import com.wzh.springbootproject.entity.Comment;
import com.wzh.springbootproject.service.CommentService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
/**
* 前端操作接口
**/
@RestController
@RequestMapping("/comment")
public class CommentController {
@Resource
private CommentService commentService;
/**
* 新增
*/
@PostMapping("/add")
public Result add(@RequestBody Comment comment) {
commentService.add(comment);
return Result.success();
}
/**
* 删除
*/
@DeleteMapping("/delete/{id}")
public Result deleteById(@PathVariable Integer id) {
commentService.deleteById(id);
return Result.success();
}
/**
* 批量删除
*/
@DeleteMapping("/delete/batch")
public Result deleteBatch(@RequestBody List<Integer> ids) {
commentService.deleteBatch(ids);
return Result.success();
}
/**
* 修改
*/
@PutMapping("/update")
public Result updateById(@RequestBody Comment comment) {
commentService.updateById(comment);
return Result.success();
}
/**
* 根据ID查询
*/
@GetMapping("/selectById/{id}")
public Result selectById(@PathVariable Integer id) {
Comment comment = commentService.selectById(id);
return Result.success(comment);
}
/**
* 查询所有
*/
@GetMapping("/selectAll")
public Result selectAll(Comment comment) {
List<Comment> list = commentService.selectAll(comment);
return Result.success(list);
}
@GetMapping("/selectForUser")
public Result selectForUser(Comment comment) {
List<Comment> list = commentService.selectForUser(comment);
return Result.success(list);
}
/**
* 分页查询
*/
@GetMapping("/selectPage")
public Result selectPage(Comment comment,
@RequestParam(defaultValue = "1") Integer pageNum,
@RequestParam(defaultValue = "10") Integer pageSize) {
PageInfo<Comment> page = commentService.selectPage(comment, pageNum, pageSize);
return Result.success(page);
}
@GetMapping("/selectCount")
public Result selectCount(@RequestParam Integer fid, @RequestParam String module) {
Integer count = commentService.selectCount(fid, module);
return Result.success(count);
}
}
CommentMapper
package com.wzh.springbootproject.mapper;
import com.wzh.springbootproject.entity.Comment;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import java.util.List;
/**
* 操作comment相关数据接口
*/
public interface CommentMapper {
/**
* 新增
*/
int insert(Comment comment);
/**
* 删除
*/
int deleteById(Integer id);
/**
* 修改
*/
int updateById(Comment comment);
/**
* 根据ID查询
*/
Comment selectById(Integer id);
/**
* 查询所有
*/
List<Comment> selectAll(Comment comment);
/**
* 查询前台展示的评论信息
*/
List<Comment> selectForUser(Comment comment);
@Select("select count(*) from comment where fid = #{fid} and module = #{module}")
Integer selectCount(@Param("fid") Integer fid, @Param("module") String module);
}
实现
<?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">
<mapper namespace="com.wzh.springbootproject.mapper.CommentMapper">
<sql id="Base_Column_List">
id,content,user_id,pid,root_id,time,blog_id,fid,module
</sql>
<select id="selectAll" resultType="com.wzh.springbootproject.entity.Comment">
select
comment.*, user.name as userName, user.avatar as avatar, u2.name as replyUser
from comment
left join user
on comment.user_id = user.id
left join comment c2
on comment.pid = c2.id
left join user u2
on c2.user_id = u2.id
<where>
<if test="userName != null"> and user.name like concat('%', #{userName}, '%')</if>
<if test="fid != null"> and comment.fid = #{fid}</if>
<if test="module != null"> and comment.module = #{module}</if>
<if test="rootId != null"> and comment.root_id = #{rootId}</if>
</where>
order by id desc
</select>
<select id="selectForUser" resultType="com.wzh.springbootproject.entity.Comment">
select
comment.*, user.name as userName, user.avatar as avatar
from comment
left join user
on comment.user_id = user.id
<where>
<if test="fid != null"> and comment.fid = #{fid}</if>
<if test="module != null"> and comment.module = #{module}</if>
and pid is null
</where>
order by comment.id desc
</select>
<select id="selectById" resultType="com.wzh.springbootproject.entity.Comment">
select
<include refid="Base_Column_List" />
from comment
where id = #{id}
</select>
<delete id="deleteById">
delete from comment
where id = #{id}
</delete>
<insert id="insert" parameterType="com.wzh.springbootproject.entity.Comment" useGeneratedKeys="true" keyProperty="id">
insert into comment
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">id,</if>
<if test="content != null">content,</if>
<if test="userId != null">user_id,</if>
<if test="pid != null">pid,</if>
<if test="rootId != null">root_id,</if>
<if test="time != null">time,</if>
<if test="fid != null">fid,</if>
<if test="module != null">module,</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">#{id},</if>
<if test="content != null">#{content},</if>
<if test="userId != null">#{userId},</if>
<if test="pid != null">#{pid},</if>
<if test="rootId != null">#{rootId},</if>
<if test="time != null">#{time},</if>
<if test="fid != null">#{fid},</if>
<if test="module != null">#{module},</if>
</trim>
</insert>
<update id="updateById" parameterType="com.wzh.springbootproject.entity.Comment">
update comment
<set>
<if test="content != null">
content = #{content},
</if>
<if test="userId != null">
user_id = #{userId},
</if>
<if test="pid != null">
pid = #{pid},
</if>
<if test="rootId != null">
root_id = #{rootId},
</if>
<if test="time != null">
time = #{time},
</if>
<if test="fid != null">
fid = #{fid},
</if>
<if test="module != null">
module = #{module},
</if>
</set>
where id = #{id}
</update>
</mapper>
CommentService
package com.wzh.springbootproject.service;
import cn.hutool.core.date.DateUtil;
import com.wzh.springbootproject.common.RoleEnum;
import com.wzh.springbootproject.entity.Account;
import com.wzh.springbootproject.entity.Comment;
import com.wzh.springbootproject.mapper.CommentMapper;
import com.wzh.springbootproject.utils.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;
/**
* 业务处理
**/
@Service
public class CommentService {
@Resource
private CommentMapper commentMapper;
/**
* 新增
*/
public void add(Comment comment) {
Account currentUser = TokenUtils.getCurrentUser();
if (RoleEnum.USER.name().equals(currentUser.getRole())) {
comment.setUserId(currentUser.getId());
}
comment.setTime(DateUtil.now());
commentMapper.insert(comment); // 先插入数据 拿到主键ID 再设置数据
if (comment.getRootId() == null) {
comment.setRootId(comment.getId());
commentMapper.updateById(comment); // 注意 更新一下 root_id
}
}
/**
* 删除
*/
public void deleteById(Integer id) {
commentMapper.deleteById(id);
}
/**
* 批量删除
*/
public void deleteBatch(List<Integer> ids) {
for (Integer id : ids) {
commentMapper.deleteById(id);
}
}
/**
* 修改
*/
public void updateById(Comment comment) {
commentMapper.updateById(comment);
}
/**
* 根据ID查询
*/
public Comment selectById(Integer id) {
return commentMapper.selectById(id);
}
/**
* 查询所有
*/
public List<Comment> selectAll(Comment comment) {
return commentMapper.selectAll(comment);
}
public List<Comment> selectForUser(Comment comment) {
List<Comment> commentList = commentMapper.selectForUser(comment); // 查询一级的评论
for (Comment c : commentList) { // 查询回复列表
Comment param = new Comment();
param.setRootId(c.getId());
List<Comment> children = this.selectAll(param);
children = children.stream().filter(child -> !child.getId().equals(c.getId())).collect(Collectors.toList()); // 排除当前查询结果里最外层节点
c.setChildren(children);
}
return commentList;
}
/**
* 分页查询
*/
public PageInfo<Comment> selectPage(Comment comment, Integer pageNum, Integer pageSize) {
PageHelper.startPage(pageNum, pageSize);
List<Comment> list = commentMapper.selectAll(comment);
return PageInfo.of(list);
}
public Integer selectCount(Integer fid, String module) {
return commentMapper.selectCount(fid, module);
}
}