1.评论Bean
private Long id;
private String content;
private Long blog_id;
private Long user_id;
private Long user_id_parent;
private Date createTime;
private List < Comment > commentList;
2.commentMapper设计的sql语句
查询所有的顶级评论 通过顶级评论的ID查询parentId
@Select ( "select * from comment where user_id_parent=0 and blog_id=#{blog_id}" )
public List < Comment > findTolComment ( Long blog_id) ;
@Select ( "select * from comment where blog_id=#{blog_id} and user_id_parent=#{user_id_parent}" )
public List < Comment > findCommentByParentUser_id ( @Param ( "blog_id" ) Long blog_id, @Param ( "user_id_parent" ) Long user_id_par
3.CommentServiceImpl
先查询出所有的顶级评论 通过一次循环逐个遍历顶级评论的子评论 通过comment.setCommentList()把子评论放入bean的变量中 然后就通过递归查询所有的子评论
@Override
public List < Comment > findCommentByParentUser_id ( Long blog_id) {
List < Comment > TolComment = commentMapper. findTolComment ( blog_id) ;
for ( Comment comment: TolComment ) {
List < Comment > childComment = commentMapper. findCommentByParentUser_id ( blog_id, comment. getId ( ) ) ;
comment. setCommentList ( childComment) ;
if ( childComment != null && childComment. size ( ) > 0 ) {
getComment ( childComment, blog_id) ;
}
}
return TolComment ;
}
public void getComment ( List < Comment > list, Long blog_id) {
for ( Comment comment: list) {
List < Comment > childComment = commentMapper. findCommentByParentUser_id ( blog_id, comment. getId ( ) ) ;
if ( childComment != null && childComment. size ( ) > 0 ) {
comment. setCommentList ( childComment) ;
getComment ( childComment, blog_id) ;
}
}
}
实例二:通过Stream实现
public class TreeTest {
/**
* 递归查询子节点
* @param root 根节点
* @param all 所有节点
* @return 根节点信息
*/
private static List<Menu> getChildrens(Menu root, List<Menu> all) {
List<Menu> children = all.stream().filter(m -> {
return Objects.equals(m.getParentId(), root.getId());
}).map(
(m) -> {
m.setChildList(getChildrens(m, all));
return m;
}
).collect(Collectors.toList());
return children;
}
public static void main(String[] args) {
//模拟从数据库查询出来
List<Menu> menus = Arrays.asList(
new Menu(1,"根节点",0),
new Menu(2,"子节点1",1),
new Menu(3,"子节点1.1",2),
new Menu(4,"子节点1.2",2),
new Menu(5,"根节点1.3",2),
new Menu(13,"根节点1.3.1",5),
new Menu(6,"根节点2",1),
new Menu(7,"根节点2.1",6),
new Menu(8,"根节点2.2",6),
new Menu(9,"根节点2.2.1",7),
new Menu(10,"根节点2.2.2",7),
new Menu(11,"根节点3",1),
new Menu(12,"根节点3.1",11)
);
//获取父节点
List<Menu> collect = menus.stream().filter(m -> m.getParentId() == 0).map(
(m) -> {
m.setChildList(TreeTest.getChildrens(m, menus));
return m;
}
).collect(Collectors.toList());
System.out.println("-------转json输出结果-------");
System.out.println(collect);
}
}
class Menu {
/**
* id
*/
public Integer id;
/**
* 名称
*/
public String name;
/**
* 父id ,根节点为0
*/
public Integer parentId;
/**
* 子节点信息
*/
public List<Menu> childList;
public Integer getId() {
return id;
}
@Override
public String toString() {
return "Menu{" +
"id=" + id +
", name='" + name + '\'' +
", parentId=" + parentId +
", childList=" + childList +
'}';
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getParentId() {
return parentId;
}
public void setParentId(Integer parentId) {
this.parentId = parentId;
}
public List<Menu> getChildList() {
return childList;
}
public void setChildList(List<Menu> childList) {
this.childList = childList;
}
public Menu(Integer id, String name, Integer parentId) {
this.id = id;
this.name = name;
this.parentId = parentId;
}
public Menu(Integer id, String name, Integer parentId, List<Menu> childList) {
this.id = id;
this.name = name;
this.parentId = parentId;
this.childList = childList;
}
}