直接上代码 一个实体类里有集合 并且集合里的对象是自己本身的时候 需要用注解
@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class, property = "实体集合名称")
这是实体类
public class Tree {
private int id;
private String text;
private static String state = "closed";
private Map<String, String> attributes;
private Collection<Tree> children;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public static String getState() {
return state;
}
public static void setState(String state) {
Tree.state = state;
}
public Map<String, String> getAttributes() {
return attributes;
}
public void setAttributes(Map<String, String> attributes) {
this.attributes = attributes;
}
//转化为JSON的时候 javabean中有自己本身的集合 不加上这句代码 会导致转换json的数据结结构有问题
@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class, property = "children")
public Collection<Tree> getChildren() {
if(children==null){
return children = new ArrayList<Tree>();
}else{
return children;
}
}
public void setChildren(Collection<Tree> children) {
this.children = children;
}
}
//service 类 做数据整理 这里只做了二级菜单 三级菜单 的数据整理会在下次总结时展现出来
public Collection<Tree> structuralData(Map<String, Object> params){
List<ResourceCate> data = resourceDao.selectList(params);
Collection<Tree> result = new ArrayList<Tree>();
for (ResourceCate rc : data) {
if(rc.getpId()==0){
Tree tree = new Tree(); //从新建立了一个实体 与数据库中的表并不匹配 只与easyui中tree空间json格式匹配
tree.setId(rc.getId());
tree.setText(rc.getText());
result.add(tree);
}else{
for (Tree tree : result) {
if(rc.getpId()==tree.getId()){
Tree tree1 = new Tree();
tree1.setId(rc.getId());
tree1.setText(rc.getText());
Map<String, String> code = new HashMap<String, String>();
code.put("castCode", rc.getCateCode());
tree1.setAttributes(code);
Collection<Tree> r = tree.getChildren();
r.add(tree1);
tree.setChildren(r);
}
}
}
}
return result;
}
浙公网安备 33010602011771号