下面就来使用mybatisplus实现个人博客系统中的评论展示模块,具体思路见注释
评论实体类
表结构
实体类
@Data
@EqualsAndHashCode(callSuper = false)//当@EqualsAndHashCode(callSuper = false)时不会比较其继承的父类的属性可能会导致错误判断;
@Accessors(chain = true)//@Accessors(chain = true) 使用chain属性,setter方法返回当前对象
public class BlogComment implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 主键id
*/
@TableId(value = "comment_id", type = IdType.AUTO)
private Long commentId;
/**
* 关联的blog主键
*/
@TableField("blog_id")
@NotNull(message = "非法请求")//被注释的元素不能为null
@Min(value = 0, message = "非法请求")//被注释的元素必须是一个数字,其值必须大于等于指定的最小值
private Long blogId;
/**
* 评论者名称
*/
@TableField("commentator")
@NotBlank(message = "请输入称呼")//只能作用在接收的String类型上,注意是只能,不能为null,而且调用trim()后,长度必须大于0
@Length(min = 1, max = 6, message = "名称过长或过短")//限制长度
private String commentator;
/**
* 评论人的邮箱
*/
@TableField("email")
@Email(message = "邮箱地址不合法")//合法性校验
private String email;
/**
* 网址
*/
@TableField("website_url")
private String websiteUrl;
/**
* 评论内容
*/
@TableField("comment_body")
@NotBlank(message = "请输入评论内容")
@Length(min = 1, max = 200, message = "评论内容过长或过短")
private String commentBody;
/**
* 评论提交时间
*/
@TableField("comment_create_time")
@JsonFormat(pattern = "yyyy/MM/dd", timezone = "GMT+8")//将Date转换成String,一般后台传值给前台时
private Date commentCreateTime;
/**
* 评论时的ip地址
*/
@TableField("commentator_ip")
private String commentatorIp;
/**
* 回复内容
*/
@TableField("reply_body")
private String replyBody;
/**
* 回复时间
*/
@TableField("reply_create_time")
@JsonFormat(pattern = "yyyy/MM/dd", timezone = "GMT+8")
private Date replyCreateTime;
/**
* 是否审核通过 0-未审核 1-审核通过
*/
@TableField("comment_status")
private Integer commentStatus;
/**
* 是否删除 0-未删除 1-已删除
*/
@TableField("is_deleted")
private Integer isDeleted;
}
其中的枚举类
public enum CommentStatusEnum {
/**
* 允许评论
*/
ALLOW(1, "允许评论"),
/**
* 不允许评论
*/
NO_ALLOW(0, "不允许评论");
private final int status;
private final String note;
CommentStatusEnum(int status, String note) {
this.status = status;
this.note = note;
}
public String getNote() {
return note;
}
public int getStatus() {
return status;
}
}
public enum DeleteStatusEnum {
/**
* 已删除
*/
DELETED(1, "已删除"),
/**
* 未删除
*/
NO_DELETED(0, "未删除");
private final int status;
private final String note;
DeleteStatusEnum(int status, String note) {
this.status = status;
this.note = note;
}
public String getNote() {
return note;
}
public int getStatus() {
return status;
}
}
dao层
public interface BlogCommentMapper extends BaseMapper<BlogComment> {
}
service层
public interface BlogCommentService extends IService<BlogComment> {
}
@Service
public class BlogCommentServiceImpl extends ServiceImpl<BlogCommentMapper, BlogComment> implements BlogCommentService {
}
Controller
@Autowired
private BlogCommentService blogCommentService;//注入
@GetMapping("/blog/listComment")
@ResponseBody
public AjaxResultPage<BlogComment> listComment(AjaxPutPage<BlogComment> ajaxPutPage, Integer blogId) {//传入参数:1.分页参数,2.文章的id
Page<BlogComment> page = ajaxPutPage.putPageToPage();
blogCommentService.page(page, new QueryWrapper<BlogComment>()//条件构造器,注意使用了lamba表达式
.lambda()
.eq(BlogComment::getBlogId, blogId)//首先根据blogid查询所有评论
.eq(BlogComment::getCommentStatus, CommentStatusEnum.ALLOW.getStatus())//筛选允许评论的
.eq(BlogComment::getIsDeleted, DeleteStatusEnum.NO_DELETED.getStatus())//筛选未删除的
.orderByDesc(BlogComment::getCommentCreateTime));//按照时间排序
AjaxResultPage<BlogComment> ajaxResultPage = new AjaxResultPage<>();
ajaxResultPage.setCount(page.getTotal());
ajaxResultPage.setData(page.getRecords());
return ajaxResultPage;//返回即可在前端使用
}