在写商品分类多级列表时,
问题出在Controller层的第12,13,14行
12行我打印对象是存在的,但是13行赋值时一直为空
public Map<String,Object> findAll() {
Map<String,Object> map = new HashMap<>();
try {
Map<String,Object> map1 = new HashMap<>();
List<Category> categorys =categoryService.findAll();
//存放所有一级目录
List<CategoryTwo> categoryTwos = new ArrayList();
//找出父级目录
for(Category category : categorys){
if(category.getCat_pid() == 0){
System.out.println(category);
CategoryTwo categoryTwo = new CategoryTwo(category);
categoryTwos.add(categoryTwo);//存入一级目录集合中
System.out.println(categoryTwos);
}
}
map1.put("msg","获取成功");
map1.put("status",200);
map.put("meta",map1);
}catch (Exception e){
e.printStackTrace();
}
return map;
}
运行结果
public class Category {
private Integer cat_id;
private String cat_name;
private Integer cat_pid;
private Integer cat_level;
private String cat_icon;
public List children;
}
二级实体对象
public class CategoryTwo extends Category{
private List<CategoryTwo> categorieTwos;
public CategoryTwo(Category category){
super(category.getCat_id(),category.getCat_name(),category.getCat_pid(),
category.getCat_level(),category.getCat_icon(),category.getChildren());
}
}
数据库
你貌似并没有把 categoryTwos 赋值进去CategoryTwo ?
二级实体对象里面的categorieTwos 没有看到有赋值的地方。
复制你的代码,看看有没有不一样的地方
import java.util.List;
public class Category {
private Integer cat_id;
private String cat_name;
private Integer cat_pid;
private Integer cat_level;
private String cat_icon;
public List children;
public Category(Integer cat_id, String cat_name, Integer cat_pid, Integer cat_level, String cat_icon,
List children) {
this.cat_id = cat_id;
this.cat_name = cat_name;
this.cat_pid = cat_pid;
this.cat_level = cat_level;
this.cat_icon = cat_icon;
this.children = children;
}
public Integer getCat_id() {
return cat_id;
}
public void setCat_id(Integer cat_id) {
this.cat_id = cat_id;
}
public String getCat_name() {
return cat_name;
}
public void setCat_name(String cat_name) {
this.cat_name = cat_name;
}
public Integer getCat_pid() {
return cat_pid;
}
public void setCat_pid(Integer cat_pid) {
this.cat_pid = cat_pid;
}
public Integer getCat_level() {
return cat_level;
}
public void setCat_level(Integer cat_level) {
this.cat_level = cat_level;
}
public String getCat_icon() {
return cat_icon;
}
public void setCat_icon(String cat_icon) {
this.cat_icon = cat_icon;
}
public List getChildren() {
return children;
}
public void setChildren(List children) {
this.children = children;
}
@Override
public String toString() {
return "Category [cat_id=" + cat_id + ", cat_name=" + cat_name + ", cat_pid=" + cat_pid + ", cat_level="
+ cat_level + ", cat_icon=" + cat_icon + ", children=" + children + "]";
}
}
import java.util.List;
public class CategoryTwo extends Category {
private List<CategoryTwo> categorieTwos;
public CategoryTwo(Category category) {
super(category.getCat_id(), category.getCat_name(), category.getCat_pid(), category.getCat_level(),
category.getCat_icon(), category.getChildren());
}
}