评论回复功能的数据库设计可以分开设计成两张表,评论表和回复表,也可以将其设计为一张表,我采用的是一张表
评论回复表的相关字段(我做的是商品goods下的评论回复)
字段解释:
gc_id:评论回复表id
gc_uid:评论用户id(外键)
gc_content:评论内容
gc_time:评论时间
gc_gid:评论的商品id(外键)
gc_status:评论状态(1未删除0已删除)
gc_parentid:父级评论的id,如果不是对评论的回复,那么该值为null
我在评论回复表中添加了几条数据
接下来是代码部分了
采用的是 springboot
+mybatis
1.GoodsComment
的pojo类
public class GoodsComment {
private Integer gcId;
private Integer gcGid;
private Integer gcUid;
private Date gcTime;
private String gcContent;
private Integer gcStatus;
private Integer gcParentid;
private Goods goods;
private Users users;
private List<GoodsComment> child;
// 略去了getter和setter方法
}
2.GoodsCommentMapper.java
public interface GoodsCommentMapper {
// 根据商品id查询该商品下的所有评论
List<GoodsComment> findByGoodId(@Param("gId") Integer gId);
}
3.GoodsCommentService.java
public interface GoodsCommentService {
// 根据商品id查询该商品下的所有评论
List<GoodsComment> findByGoodId(Integer gId);
}
4.GoodsCommentServiceImpl.java
@Service
public class GoodsCommentServiceImpl implements GoodsCommentService {
@Resource
private GoodsCommentMapper goodsCommentMapper;
@Override
public List<GoodsComment> findByGoodId(Integer gId) {
return goodsCommentMapper.findByGoodId(gId);
}
}
5.GoodsCommentMapper.xml
<select id="findByGoodId" resultMap="BaseResultMap">
SELECT a.*,b.*
FROM goods_comment AS a,users AS b
WHERE a.gc_gId=#{gId}
AND a.gc_status=b.u_status=1
AND a.gc_uid=b.u_id
select>
6.CoodsCommentController.java
@RestController
@RequestMapping("/goodsComment")
public class GoodsCommentController {
@Autowired
private GoodsCommentService commentService;
/**
* 根据商品id查询该商品下的所有评论
* @param gId
* @return
*/
@GetMapping("/findByGoodId/{gId}")
public List<GoodsComment> findByGoodId(@PathVariable("gId") Integer gId) {
List<GoodsComment> allComments = commentService.findByGoodId(gId);
if (allComments == null || allComments.size() == 0) {
return new ArrayList<>();
}
List<GoodsComment> comments = new ArrayList<>();
List<GoodsComment> parents = new ArrayList<>();
for (GoodsComment comment : allComments) {
if (comment.getGcParentid()==null) {
comments.add(comment);
parents.add(comment);
} else {
boolean foundParent=false;
for (GoodsComment parent : parents) {
if (comment.getGcParentid() == parent.getGcId()) {
if (parent.getChild() == null) {
parent.setChild(new ArrayList<>());
}
parent.getChild().add(comment);
parents.add(comment);
foundParent=true;
//如果对list迭代过程中同时修改list,会报java.util.ConcurrentModificationException
// 的异常,所以我们需要break,当然break也可以提高算法效率
break;
}
}
if (!foundParent) {
throw new RuntimeException("can not find the parent comment");
}
}
}
return comments;
}
}
7.获取到的数据部分
[
{
"gcId": 1,
"gcGid": 1,
"gcUid": 11,
"gcTime": "2020-03-13T14:29:52.000+0000",
"gcContent": "我是评论商品的",
"gcStatus": 1,
"gcParentid": null,
"goods": null,
"users": {
"uId": 11,
"uName": "小陈",
"uAvatar": "http://imgs/FjrnXL4DP6Ib8xmZmXQU5vy5GWvq"
},
"child": [
{
"gcId": 3,
"gcGid": 1,
"gcUid": 6,
"gcTime": "2020-03-15T16:31:49.000+0000",
"gcContent": "我是回复",
"gcStatus": 1,
"gcParentid": 1,
"goods": null,
"users": {
"uId": 6,
"uName": "小贰",
"uAvatar": "http://imgs/Fi-WU4zsZ-1OTVfPg5OXyIao13SP"
},
"child": [
{
"gcId": 4,
"gcGid": 1,
"gcUid": 10,
"gcTime": "2020-03-15T16:48:15.000+0000",
"gcContent": "我是回复的回复",
"gcStatus": 1,
"gcParentid": 3,
"goods": null,
"users": {
"uId": 10,
"uName": "小贰",
"uAvatar": "http://imgs/Fi-WU4zsZ-1OTVfPg5OXyIao13SP"
},
"child": [
{
"gcId": 5,
"gcGid": 1,
"gcUid": 9,
"gcTime": "2020-03-15T20:26:09.000+0000",
"gcContent": "我看一下新增评论",
"gcStatus": 1,
"gcParentid": 4,
"goods": null,
"users": {
"uId": 9,
"uName": "小贰",
"uAvatar": "http://imgs/Fi-WU4zsZ-1OTVfPg5OXyIao13SP"
},
"child": null
}
]
}
]
}
]
},
{
"gcId": 2,
"gcGid": 1,
"gcUid": 8,
"gcTime": "2020-03-14T14:30:12.000+0000",
"gcContent": "我也是评论这个商品的",
"gcStatus": 1,
"gcParentid": null,
"goods": null,
"users": {
"uId": 8,
"uName": "小贰",
"uAvatar": "http://imgs/Fi-WU4zsZ-1OTVfPg5OXyIao13SP"
},
"child": null
}
]