java国内地址三级、四级联动,附四级联动数据库

因工作需要地址联动功能,使用java开发

实体类

import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.annotation.IdType;
import com.wuling.common.domain.BaseEntity;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableField;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

/**
 * <p>
 * 地址区域表
 * </p>
 *
 * 
 * @since 2023-01-03
 */
@Data
@EqualsAndHashCode(callSuper = true)
@Accessors(chain = true)
@TableName("base_area")
@ApiModel(value="Area对象", description="地址区域表")
public class Area extends BaseEntity {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "主键id")
    @TableId(value = "id", type = IdType.AUTO)
    private Long id;

    @ApiModelProperty(value = "父级")
    @TableField("parent_id")
    private Integer parentId;

    @ApiModelProperty(value = "名称")
    @TableField("name")
    private String name;

    @ApiModelProperty(value = "简称")
    @TableField("short_name")
    private String shortName;

    @ApiModelProperty(value = "经度")
    @TableField("longitude")
    private String longitude;

    @ApiModelProperty(value = "纬度")
    @TableField("latitude")
    private String latitude;

    @ApiModelProperty(value = "级别 1 省 2 市 3 区 4 乡镇/街道")
    @TableField("level")
    private Integer level;

    @ApiModelProperty(value = "排序")
    @TableField("sort")
    private Integer sort;

    @ApiModelProperty(value = "状态 0 无效 1 有效")
    @TableField("status")
    private Boolean status;


}

vo

import com.wuling.base.domain.Area;
import lombok.Data;
import lombok.experimental.Accessors;

import java.util.List;

@Data
@Accessors(chain = true)
public class AreaVo extends Area {

    private List<AreaVo> children;

}
树状结构 AreaTreeReq
import com.wuling.base.domain.Area;
import lombok.Data;
import lombok.experimental.Accessors;

@Data
@Accessors(chain = true)
public class AreaTreeReq extends Area {
}
AreaController
    @ApiOperation(value = "树状返回三级地址")
    @GetMapping("/tree/list")
    public AjaxResult list(){

        return AjaxResult.success(areaService.treeList());
    }

实现类AreaServiceImpl

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wuling.base.domain.Area;
import com.wuling.base.domain.vo.AreaVo;
import com.wuling.base.mapper.AreaMapper;
import com.wuling.base.service.IAreaService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 地址区域表 服务实现类
 * </p>
 *
 * @author 开发人姓名
 * @since 2023-01-03
 */
@Service
public class AreaServiceImpl extends ServiceImpl<AreaMapper, Area> implements IAreaService {

    @Autowired
    private AreaMapper mapper;


//    public List<AreaVo> treeList() {
//        return null;
//    }

    @Override
    public List<AreaVo> treeList() {
        QueryWrapper queryWrapper = new QueryWrapper();
        List<Area> list = mapper.selectList(queryWrapper);
        List<AreaVo> list1 = new ArrayList();
        for(Area area : list){
            AreaVo areaVo = new AreaVo();
            if(area.getParentId() == 0){
                BeanUtils.copyProperties(area, areaVo);
                areaVo.setChildren(formulaClothClassNextTree(list,(area.getId())));
                list1.add(areaVo);

            }
        }
        return list1;
    }

    public List<AreaVo> formulaClothClassNextTree(List<Area> list, Long formulaClothClassId) {
        List<AreaVo> list1 = new ArrayList();
        for(Area area : list){
            AreaVo areaVo = new AreaVo();
            if(area.getParentId().toString().equals(formulaClothClassId.toString() )){
                BeanUtils.copyProperties(area, areaVo);
                areaVo.setChildren(formulaClothClassNextTree1(list,area.getId()));
                list1.add(areaVo);
            }
        }
        return list1;
    }

    public List<AreaVo> formulaClothClassNextTree1(List<Area> list, Long formulaClothClassId) {
        List<AreaVo> list1 = new ArrayList();
        for(Area area : list){
            AreaVo areaVo = new AreaVo();
            if(area.getParentId().toString().equals(formulaClothClassId.toString())){
                BeanUtils.copyProperties(area, areaVo);
                areaVo.setChildren(formulaClothClassNextTree1(list,area.getId()));
                list1.add(areaVo);
            }
        }
        return list1;
    }

}

 

 

四级联动数据库

https://musetransfer.com/s/4ays3o8jz 请点击链接获取《无主题传输》, 有效期至1月10日

posted @ 2023-01-03 16:18  铁锅炖猫  阅读(479)  评论(0编辑  收藏  举报