一、表结构
CREATE TABLE `my_tree` (
`test_id` VARCHAR(32) NOT NULL,
`gjfl_name` VARCHAR(100) NULL DEFAULT NULL,
`parent_id` VARCHAR(32) NULL DEFAULT NULL,
`gjfl_level` INT(11) NULL DEFAULT NULL,
PRIMARY KEY (`id`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB;
二、构建数据
INSERT INTO `my_tree` VALUES ('1', '1', '0', '1');
INSERT INTO `my_tree` VALUES ('2', '2', '1', '2');
INSERT INTO `my_tree` VALUES ('3', '3', '0', '1');
INSERT INTO `my_tree` VALUES ('4', '4', '3', '2');
INSERT INTO `my_tree` VALUES ('5', '5', '4', '3');
INSERT INTO `my_tree` VALUES ('6', '6', '2', '3');
INSERT INTO `my_tree` VALUES ('7', '7', '3', '2');
三、实体
@Table(name = "my_tree")
public class MyTree implements Serializable {
@Id
@Column(name = "test_id")
private String testId;
@Column(name = "gjfl_name")
private String gjflName;
@Column(name = "parent_id")
private String parentId;
@Column(name = "gjfl_level")
private Integer gjflLevel;
private List childrenList;//注意这里
//忽略setter、getter方法,使用lombok可以生成setter、getter
}
四、mapper配置
注意collection里的配置:javaType、 column、select
五、DAO层
public interface MyTreeMapper {
List findLevel(String param);
}
service及controller层与平常没有差别
结果
"list":[
{"testId":"1","gjflName":"1","parentId":"0","gjflLevel":1,
"childrenList":[
{"testId":"2","gjflName":"2","parentId":"1","gjflLevel":2,
"childrenList":[
{"testId":"6","gjflName":"6","parentId":"2","gjflLevel":3,"childrenList":[]}
]
}
]
},
{"testId":"3","gjflName":"3","parentId":"0","gjflLevel":1,
"childrenList":[
{"testId":"4","gjflName":"4","parentId":"3","gjflLevel":2,
"childrenList":[
{"testId":"5","gjflName":"5","parentId":"4","gjflLevel":3,"childrenList":[]}
]
},
{"testId":"7","gjflName":"7","parentId":"3","gjflLevel":2,"childrenList":[]}
]
}
]