springboot单表三级联动模糊查询,单表三级菜单模糊搜索,分页三级联动模糊查询,树形模糊查询

今天学习到了做一个三级联动,还要可以进行模糊查询,看了很多CSDN的文章,写的都不太清楚,对于俺这种小白极为不友好,看到这个大佬的文章,非常感谢,自己就照抄了一波 链接

SQL

CREATE 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 就是最普通的查询所有

    <select id="selectEquipmentList" parameterType="Equipment" resultMap="EquipmentResult">
        <include refid="selectEquipmentVo"/>
        <where>
            <if test="name != null  and name != ''"> and `name` like concat('%', #{name}, '%')</if>
            <if test="model != null  and model != ''"> and model = #{model}</if>
            <if test="parentId != null  and parentId != ''"> and parentId = #{parent_id}</if>
            <if test="id != null  and id != ''"> and id = #{id}</if>
            <if test="type != null  and type != ''"> and `type` = #{type}</if>
        </where>
    </select>

mapper.java

public List<Equipment> selectEquipmentList(Equipment equipment);

serviceImpl.java

    @Override
    public PageInfo<Equipment> selectListPage(Equipment equipment,Integer PageNum,Integer PageSize){
        PageHelper.startPage(PageNum,PageSize);

        List<Equipment> equipmentList1 = equipmentMapper.selectEquipmentList(equipment);
        List<Equipment> equipmentList = equipmentList1.stream().sorted((menu1,menu2)->{
            return (menu1.getOrderNum() == null ? 0 :menu1.getOrderNum()) - (menu2.getOrderNum() ==null ? 0: menu2.getOrderNum());
        }).collect(Collectors.toList());
        List<Equipment> newAllList = new ArrayList<>();
        List<Long> 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<Equipment> menuList(List<Equipment> allList){
        List<Equipment> mapArr = new ArrayList<>();

        for (Equipment x : allList){
            if (x.getParentId()==0){
                mapArr.add(x);
            }
        }

        for (Equipment y : mapArr){
            List<Equipment> index = menuChild(y.getId(),allList);
            y.setChildren(index);
        }
        return mapArr;
    }
    public List<Equipment> menuChild(Long id,List<Equipment> allList){
        List<Equipment> mapArr = new ArrayList<>();

        for (Equipment x : allList){
            if (x.getParentId().equals(id)){
                mapArr.add(x);
            }
        }

        for (Equipment y : mapArr){
            List<Equipment> index = menuChild(y.getId(),allList);
            y.setChildren(index);
        }
        return mapArr;
    }

    public List<Equipment> getPatentId(List<Equipment> allList,Long id,List<Long> 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
    }
}

希望对各位有帮助吧


版权声明:本文为qq_43445258原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。