我的数据库中有一种树型结构,如下所示。
mysql> select * from categories;
+----+---------------+-----------+
| id | category_name | parent_id |
+----+---------------+-----------+
| 1 | footwear | -1 |
| 2 | kids footwear | 1 |
| 3 | for girls | 2 |
| 4 | sandals | 3 |
| 5 | electronics | -1 |
| 6 | kids | -1 |
+----+---------------+-----------+
6 rows in set (0.00 sec)
我想阅读父类别的所有子类别,表格中的-1表示该类别为父类别或最高类别。树中可能有任何级别。
一旦用户点击父类别(-1),我需要显示其所有子类别。我不想再次在每个子类别上再次点击数据库。每次点击。 我有一些想法,可以通过将表条目转换为树和&而不是遍历树,但是从桌子本身检索它是一件很复杂的事情。
我从这个链接有一些想法以另一种方式做到(第二种方法) http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/ 但是在这个方法中,如果我想插入一个新的子类别,那么我必须修改整个表格。
还有其他建议吗?
答案 0 :(得分:1)
AFAIK只有几种可行的方法:
另一种方法可能是使用不同的数据库;)
答案 1 :(得分:0)
you have to use recursive function to do this kind of problem
public String findcategoryList() {
List<Category> results = dao.findAllCategory();
LinkedHashMap<String, List<String>> map = new LinkedHashMap();
LinkedHashMap<Integer, List<Integer>> mapKey = new LinkedHashMap();
for (Object object : results) {
Category ee = (Category) object;
Set<Category> stsd = ee.getSubCategories();
if (stsd != null && stsd.size() > 0 && ee.getCategory() == null) {
dispaly(stsd, ee, map, mapKey);
} else if (ee.getCategory() == null) {
List<String> list = new ArrayList<String>();
List<Integer> listKey = new ArrayList<Integer>();
map.put(ee.getCategoryname(), list);
mapKey.put(ee.getCategoryId(), listKey);
System.out.println(ee.getCategoryname() + "---- No Child");
System.out.println(ee.getCategoryId() + "---- No Child");
}
}
StringBuilder menu = new StringBuilder();
System.out.println("------------------Menu Start----------------");
Set<String> keys = new TreeSet<String>(map.keySet());
menu.append("<ul class='drop'>");
Set<String> sets = new HashSet<String>();
if(map.size() == mapKey.size()){
Iterator<Entry<String, List<String>>> iter1 = map.entrySet().iterator();
Iterator<Entry<Integer, List<Integer>>> iter2 = mapKey.entrySet().iterator();
while(iter1.hasNext() || iter2.hasNext()) {
Entry<String, List<String>> e1 = iter1.next();
Entry<Integer, List<Integer>> e2 = iter2.next();
String key = e1.getKey();
List<String> child = map.get(key);
Integer keyId = e2.getKey();
List<Integer> childKey = mapKey.get(keyId);
if (!sets.contains(key)) {
System.out.println("key : "+key);
if (child != null && child.size() > 0) {
menu.append("<li><a href='#'>").append(key).append("<i class='fa fa-caret-down'></i></a>");
menu.append("\n");
menu.append("<ul class='sub_menu submenu'>");
menu.append("\n");
}else{
menu.append("<li><a href='./viewVideos-"+keyId+"'>").append(key).append("<i class='fa fa-caret-down'></i></a>");
menu.append("\n");
}
sets.add(key);
menuCreate(menu, child, childKey, map, mapKey, sets);
if (child != null && child.size() > 0) {
menu.append("</ul>");
menu.append("\n");
}
menu.append("</li>");
}
}
}
menu.append("</ul>");
//System.out.println(menu.toString());
return menu.toString();
}
public static void menuCreate(StringBuilder menu, List<String> child, List<Integer> childKey, Map<String, List<String>> map,
Map<Integer, List<Integer>> mapKey,
Set<String> sets) {
if (child != null && child.size() > 0) {
// Collections.sort(child, ComparatorUtils.NATURAL_COMPARATOR);
for (int i=0;i<child.size();i++) {
List<String> subChild = map.get(child.get(i));
List<Integer> subChildKey = mapKey.get(childKey.get(i));
System.out.println("subChild : "+subChild+ " child : " + child + " Value : " + child.get(i));
menu.append("<li>");
boolean flag = false;
if (subChild == null || (child != null && child.size() <= 0)) {
menu.append("<a href='./viewVideos-"+childKey.get(i)+"'>");
flag = true;
}
if(flag){
menu.append(child.get(i)).append("<i class='fa fa-caret-right'></i>");
menu.append("</a>");
}else{
menu.append("<a href='#'>").append(child.get(i)).append("<i class='fa fa-caret-right'></i></a>");
}
menu.append("\n");
if (subChild != null && subChild.size() > 0) {
if (!sets.contains(child.get(i))) {
sets.add(child.get(i));
menu.append("<ul class='sub_menu submenu'>");
menu.append("\n");
menuCreate(menu, subChild, subChildKey, map, mapKey, sets);
menu.append("</ul>");
menu.append("\n");
}
}
menu.append("</li>");
}
}
}
public void dispaly(Set<Category> msp, Category ee, Map<String, List<String>> map, Map<Integer, List<Integer>> mapKey) {
for (Category category : msp) {
Set<Category> stsd = category.getSubCategories();
if (category.getCategory() != null) {
if (map.get(category.getCategory().getCategoryname()) == null) {
List<String> list = new ArrayList<String>();
List<Integer> listKey = new ArrayList<Integer>();
list.add(category.getCategoryname());
listKey.add(category.getCategoryId());
map.put(category.getCategory().getCategoryname(), list);
mapKey.put(category.getCategory().getCategoryId(), listKey);
} else {
List<String> list = map.get(category.getCategory().getCategoryname());
List<Integer> listKey = mapKey.get(category.getCategory().getCategoryId());
list.add(category.getCategoryname());
listKey.add(category.getCategoryId());
map.put(category.getCategory().getCategoryname(), list);
mapKey.put(category.getCategory().getCategoryId(), listKey);
}
System.out.println(category.getCategory().getCategoryname() + "----" + category.getCategoryname());
System.out.println(category.getCategory().getCategoryId() + "----" + category.getCategoryId());
}
if (stsd != null && stsd.size() > 0) {
dispaly(stsd, category, map, mapKey);
}
}
menu.toString() will give you tree bar in html format.