Java 弹簧靴&x2B;(JPA)-类别层次结构-递归遍历
我有目录实体-Java 弹簧靴&x2B;(JPA)-类别层次结构-递归遍历,java,spring,jpa,recursion,Java,Spring,Jpa,Recursion,我有目录实体- public class Category { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column private String categoryName; @ManyToOne @JoinColumn(name = "parent_id") private Category parent; @OneToMany(mappedBy = "parent", casca
public class Category {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column
private String categoryName;
@ManyToOne
@JoinColumn(name = "parent_id")
private Category parent;
@OneToMany(mappedBy = "parent", cascade = CascadeType.REMOVE, orphanRemoval = true)
private List<Category> children = new ArrayList<Category>();
@ManyToMany(mappedBy = "categories")
private Set<Product>
控制器中的一些方法(在我的例子中,问题是原则)
如何修改递归,以便获得类别和子类别的正确顺序?我不明白为什么我会得到一些重复的数据,你会得到重复的数据,因为你用
List categoryList=categoryRepository.findAll()从数据库中获取所有类别代码>
此列表包含住宅、1类、2类
因为对于此列表的每个项目,您在主循环中调用recursiveTree
方法,您将为HOME打印树(这将打印其所有子项),您还将为Category 1调用recursiveTree
,再次打印该类别及其子项,以此类推
如果希望每个类别只打印一次,请只查找根类别(主类别和没有父类别的任何其他类别),然后调用recursiveTree
。例如:
List<Category> rootCategoryList = categoryRepository.findByParentIsNull();
for (Category cat : rootCategoryList) {
recursiveTree(cat);
}
List rootCategoryList=categoryRepository.findByParentIsNull();
对于(类别类别:rootCategoryList){
递归树(cat);
}
由于使用List categoryList=categoryRepository.findAll()从数据库中获取所有类别,因此会得到重复的数据代码>
此列表包含住宅、1类、2类
因为对于此列表的每个项目,您在主循环中调用recursiveTree
方法,您将为HOME打印树(这将打印其所有子项),您还将为Category 1调用recursiveTree
,再次打印该类别及其子项,以此类推
如果希望每个类别只打印一次,请只查找根类别(主类别和没有父类别的任何其他类别),然后调用recursiveTree
。例如:
List<Category> rootCategoryList = categoryRepository.findByParentIsNull();
for (Category cat : rootCategoryList) {
recursiveTree(cat);
}
List rootCategoryList=categoryRepository.findByParentIsNull();
对于(类别类别:rootCategoryList){
递归树(cat);
}
要在thymeleaf中显示类别树,此代码非常有效
<div th:fragment="submenu">
<ul>
<li th:each="cat : ${categories}">
<span th:text="${cat.categoryName}"></span>
<div th:with="categories = ${cat.children}" th:include="admin/fragments/info :: submenu"></div>
</li>
</ul>
</div>
-
要在thymeleaf中显示类别树,此代码非常有效
<div th:fragment="submenu">
<ul>
<li th:each="cat : ${categories}">
<span th:text="${cat.categoryName}"></span>
<div th:with="categories = ${cat.children}" th:include="admin/fragments/info :: submenu"></div>
</li>
</ul>
</div>
-
因为getChildren()
不起作用你是什么意思?你能帮我吗?我很想,但我不知道怎么做。因为getChildren()
不起作用。你是什么意思?你能帮我吗?我很想,但我不知道怎么做。你真是个天才!!我仍然无法理解它是如何工作的:)再解释一下?如何仅获取空值的父目录,同时获取子目录(父目录没有空值)。您是否有一些如何在thymeleaf视图中实现这一点的教程:)当在recursiveTree
方法中调用cat.getChildren()
时,将从数据库中检索子对象,以便返回它们。请注意,如果要在thymeleaf中执行类似的操作,应首先迭代类别(即:调用类似于recursiveTree
但不打印的方法),因为默认情况下,在视图渲染期间数据库连接不再可用。要在thymeleaf中渲染,可以使用片段-例如,请参见you is genius!!我仍然无法理解它是如何工作的:)再解释一下?如何仅获取空值的父目录,同时获取子目录(父目录没有空值)。您是否有一些如何在thymeleaf视图中实现这一点的教程:)当在recursiveTree
方法中调用cat.getChildren()
时,将从数据库中检索子对象,以便返回它们。请注意,如果要在thymeleaf中执行类似的操作,应首先迭代类别(即:调用类似于recursiveTree
但不打印的方法),因为默认情况下,在视图渲染期间数据库连接不再可用。要在thymeleaf中渲染,可以使用片段-例如,请参见
<div th:fragment="submenu">
<ul>
<li th:each="cat : ${categories}">
<span th:text="${cat.categoryName}"></span>
<div th:with="categories = ${cat.children}" th:include="admin/fragments/info :: submenu"></div>
</li>
</ul>
</div>