今天学习到了做一个三级联动,还要可以进行模糊查询,看了很多CSDN的文章,写的都不太清楚,对于俺这种小白极为不友好,看到这个大佬的文章,非常感谢,自己就照抄了一波 链接
SQLCREATE TABLE `equipment` ( `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'ID', `name` varchar(50) NOT NULL COMMENT '名称', `model` varchar(50) DEFAULT NULL COMMENT '型号', `unit_price` int(10) DEFAULT NULL COMMENT '产品单价', `parent_id` bigint(20) DEFAULT '0' COMMENT '父设备ID', `order_num` int(4) DEFAULT '0' COMMENT '显示排序', `type` char(1) DEFAULT '' COMMENT '设备类型(M类型 C系列 F产品)', `remark` varchar(500) DEFAULT '' COMMENT '备注', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=101 DEFAULT CHARSET=utf8 COMMENT='备注类型管理表'mapper.xml 就是最普通的查询所有
mapper.java
public ListserviceImpl.javaselectEquipmentList(Equipment equipment);
@Override
public PageInfo selectListPage(Equipment equipment,Integer PageNum,Integer PageSize){
PageHelper.startPage(PageNum,PageSize);
List equipmentList1 = equipmentMapper.selectEquipmentList(equipment);
List equipmentList = equipmentList1.stream().sorted((menu1,menu2)->{
return (menu1.getOrderNum() == null ? 0 :menu1.getOrderNum()) - (menu2.getOrderNum() ==null ? 0: menu2.getOrderNum());
}).collect(Collectors.toList());
List newAllList = new ArrayList<>();
List idList = new ArrayList<>();
for (Equipment model : equipmentList){
idList.add(model.getId());
}
for (Equipment list : equipmentList){
if (0 != list.getParentId() ){
newAllList=getPatentId(newAllList,list.getParentId(),idList);
}
}
equipmentList.addAll(newAllList);
return new PageInfo<>(menuList(equipmentList));
}
public List menuList(List allList){
List mapArr = new ArrayList<>();
for (Equipment x : allList){
if (x.getParentId()==0){
mapArr.add(x);
}
}
for (Equipment y : mapArr){
List index = menuChild(y.getId(),allList);
y.setChildren(index);
}
return mapArr;
}
public List menuChild(Long id,List allList){
List mapArr = new ArrayList<>();
for (Equipment x : allList){
if (x.getParentId().equals(id)){
mapArr.add(x);
}
}
for (Equipment y : mapArr){
List index = menuChild(y.getId(),allList);
y.setChildren(index);
}
return mapArr;
}
public List getPatentId(List allList,Long id,List idList){
Boolean bool = true;
for (Long ids : idList){
if(ids.equals(id)){
//如果能找到在id集合中能找到id说明已经改数据已经查过,直接跳过
bool=false;
continue;
}
}
if(bool==true){
idList.add(id);
//根据子节点id查询其父节点信息
Equipment parentById = this.equipmentMapper.selectEquipmentById(id);
if(parentById!=null){
allList.add(parentById);
}
if(parentById.getParentId()!=0){
getPatentId(allList,parentById.getParentId(),idList);
}
}
return allList;
}
cocontroller.java
@GetMapping("/get")
public AjaxResult listAll(Equipment e){
return AjaxResult.success(equipmentService.selectListPage(e,1,10));
}
测试1 查询所有
http://xxxx/eq/get
{
"msg": "操作成功",
"code": 200,
"data": {
"total": 2,
"list": [
{
"id": 1,
"name": "杀虫灯",
"model": null,
"unitPrice": null,
"parentId": 0,
"orderNum": 0,
"type": "M",
"remark": "",
"children": [
{
"id": 20,
"name": "气式杀虫灯",
"model": null,
"unitPrice": null,
"parentId": 1,
"orderNum": 0,
"type": "C",
"remark": "",
"children": [
{
"id": 100,
"name": "VX-1",
"model": "VX-1",
"unitPrice": null,
"parentId": 20,
"orderNum": 0,
"type": "F",
"remark": "比昂牌",
"children": []
}
]
}
]
},
{
"id": 2,
"name": "气象仪",
"model": null,
"unitPrice": null,
"parentId": 0,
"orderNum": 0,
"type": "M",
"remark": "",
"children": []
}
],
"pageNum": 1,
"pageSize": 2,
"size": 2,
"startRow": 0,
"endRow": 1,
"pages": 1,
"prePage": 0,
"nextPage": 0,
"isFirstPage": true,
"isLastPage": true,
"hasPreviousPage": false,
"hasNextPage": false,
"navigatePages": 8,
"navigatepageNums": [
1
],
"navigateFirstPage": 1,
"navigateLastPage": 1
}
}
测试2 模糊查询
http://XXxxx/eq/get?name=V
{
"msg": "操作成功",
"code": 200,
"data": {
"total": 1,
"list": [
{
"id": 1,
"name": "杀虫灯",
"model": null,
"unitPrice": null,
"parentId": 0,
"orderNum": 0,
"type": "M",
"remark": "",
"children": [
{
"id": 20,
"name": "气式杀虫灯",
"model": null,
"unitPrice": null,
"parentId": 1,
"orderNum": 0,
"type": "C",
"remark": "",
"children": [
{
"id": 100,
"name": "VX-1",
"model": "VX-1",
"unitPrice": null,
"parentId": 20,
"orderNum": 0,
"type": "F",
"remark": "比昂牌",
"children": []
}
]
}
]
}
],
"pageNum": 1,
"pageSize": 1,
"size": 1,
"startRow": 0,
"endRow": 0,
"pages": 1,
"prePage": 0,
"nextPage": 0,
"isFirstPage": true,
"isLastPage": true,
"hasPreviousPage": false,
"hasNextPage": false,
"navigatePages": 8,
"navigatepageNums": [
1
],
"navigateFirstPage": 1,
"navigateLastPage": 1
}
}
希望对各位有帮助吧



