java多级联动

应用场景

类似省市区县镇一类的多级联动,在实现过程中,发现有多重foreach循环嵌套,可以用递归方式解决。实例使用的是springboot+mybatisPlus+mysql

数据库字段

create table ot_car_key_model_mapping
(
    id                integer default nextval('ot_car_key_model_mapping_id_seq'::regclass) not null
        constraint ot_car_key_model_mapping_copy1_pkey
            primary key,
    parent_id         integer,
    row_type          varchar(32),
    type_code         varchar(32),
    type_desc_zh      varchar(255),
    type_desc_en      varchar(255),
    key_model_group   varchar(16),
    source_group      varchar(16),
    car_model_desc_zh varchar(32),
    car_model_desc_en varchar(32)
);

alter table ot_car_key_model_mapping
    owner to postgres;

controller

    public SheerResponse carModelAllInfo() {
        return carKeyModelMappingService.carModelAllInfo();
    }

serivce

/**
     * 获取车型集合
     * @return SheerResponse
     */
    public SheerResponse carModelAllInfo() {
        SheerResponse sheerResponse = new SheerResponse();
        //查询所有集合
        List<CarKeyModelMappingPO> allList = carKeyModelMappingDAO.selectList(null);
        //递归组合数据
        List<QueryCarKeyModelMappingDTO> brandDTOList = getMappingList(allList,0);

        //验证返回数据是否为空
        if(brandDTOList.size()>0){
            sheerResponse.tick(SheerGlobalResponseCode.SUCCESS, "查询成功");
            sheerResponse.setData(brandDTOList);
            return sheerResponse;
        }
        return sheerResponse.fail("无数据");
    }
    /**
     * 递归遍历车型信息
     * @param list 信息集合
     * @param pid 父类id
     * @return List<QueryCarKeyModelMappingDTO>
     */
    public List<QueryCarKeyModelMappingDTO> getMappingList(List<CarKeyModelMappingPO> list, Integer pid) {
        List<QueryCarKeyModelMappingDTO> result = new ArrayList<QueryCarKeyModelMappingDTO>();
        QueryCarKeyModelMappingDTO dto;

        for (int i=0;i<list.size();i++){
            dto = new QueryCarKeyModelMappingDTO();
            CarKeyModelMappingPO carKeyModelMappingPO = list.get(i);
            BeanUtils.copyProperties(carKeyModelMappingPO,dto);
            //如果父id为空,则为第一级
            if(dto.getParentId() == null ){
                dto.setParentId(0);
            }
            if(pid.equals(dto.getParentId())){
                dto.setMappingDtos(getMappingList(list, dto.getId()));
                result.add(dto);
            }
        }

        return result;
    }

DAO

@Repository("carKeyModelMappingDAO")
public interface CarKeyModelMappingDAO extends BaseMapper<CarKeyModelMappingPO> {

}

查询结果

postman测试结果
由于数据量过多,只截取部分数据

在这里插入图片描述

{
    "message": "查询成功",
    "code": 1,
    "codeName": "SUCCESS",
    "data": [
        {
            "id": 525,
            "parentId": 0,
            "rowType": "brand",
            "typeCode": "BMW",
            "typeDescZh": null,
            "typeDescEn": null,
            "keyModelGroup": null,
            "sourceGroup": null,
            "carModelDescZh": null,
            "carModelDescEn": null,
            "mappingDtos": [
                {
                    "id": 526,
                    "parentId": 525,
                    "rowType": "car_system",
                    "typeCode": "1er",
                    "typeDescZh": "1系",
                    "typeDescEn": "1er",
                    "keyModelGroup": null,
                    "sourceGroup": null,
                    "carModelDescZh": null,
                    "carModelDescEn": null,
                    "mappingDtos": [
                        {
                            "id": 540,
                            "parentId": 526,
                            "rowType": "e_series",
                            "typeCode": "F20",
                            "typeDescZh": "1系运动型两厢轿车",
                            "typeDescEn": "1er 5-doors",
                            "keyModelGroup": null,
                            "sourceGroup": "NSC",
                            "carModelDescZh": null,
                            "carModelDescEn": null,
                            "mappingDtos": [
                                {
                                    "id": 576,
                                    "parentId": 540,
                                    "rowType": "model",
                                    "typeCode": "1R51",
                                    "typeDescZh": "118i(1R51)",
                                    "typeDescEn": "118i(1R51)",
                                    "keyModelGroup": null,
                                    "sourceGroup": null,
                                    "carModelDescZh": null,
                                    "carModelDescEn": null,
                                    "mappingDtos": [
                                        {
                                            "id": 676,
                                            "parentId": 576,
                                            "rowType": "variant",
                                            "typeCode": "ZHL",
                                            "typeDescZh": "118i 运动设计套装",
                                            "typeDescEn": "118i Urban Line",
                                            "keyModelGroup": null,
                                            "sourceGroup": null,
                                            "carModelDescZh": null,
                                            "carModelDescEn": null,
                                            "mappingDtos": [
                                                {
                                                    "id": 866,
                                                    "parentId": 676,
                                                    "rowType": "profile",
                                                    "typeCode": "201807",
                                                    "typeDescZh": null,
                                                    "typeDescEn": null,
                                                    "keyModelGroup": "Others",
                                                    "sourceGroup": null,
                                                    "carModelDescZh": null,
                                                    "carModelDescEn": null,
                                                    "mappingDtos": []
                                                }
                                            ]
                                        },
                                        {
                                            "id": 834,
                                            "parentId": 576,
                                            "rowType": "variant",
                                            "typeCode": "ZVA",
                                            "typeDescZh": "118i 领先型",
                                            "typeDescEn": "118i Vantage",
                                            "keyModelGroup": null,
                                            "sourceGroup": null,
                                            "carModelDescZh": null,
                                            "carModelDescEn": null,
                                            "mappingDtos": [
                                                {
                                                    "id": 875,
                                                    "parentId": 834,
                                                    "rowType": "profile",
                                                    "typeCode": "201807",
                                                    "typeDescZh": null,
                                                    "typeDescEn": null,
                                                    "keyModelGroup": "Others",
                                                    "sourceGroup": null,
                                                    "carModelDescZh": null,
                                                    "carModelDescEn": null,
                                                    "mappingDtos": []
                                                }
                                            ]
                                        }
                                    ]
                                },

<–!刚入职三周的实习生,如有不足,请多指教,谢谢–>

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值