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

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

时间:2023-01-03 16:37:23浏览次数:38  
标签:java area List value 四级 private 联动 import com

因工作需要地址联动功能,使用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;
    }

}

 

标签:java,area,List,value,四级,private,联动,import,com
From: https://www.cnblogs.com/AmqvMI/p/17022596.html

相关文章

  • NacosException: java.net.UnknownHostException: jmenv.tbsite.net
    第一次接触Nacos,启动时报错(java.net.UnknownHostException:jmenv.tbsite.net)解决方案。错误信息:Causedby:java.net.UnknownHostException:jmenv.tbsite.net......
  • Javascript条件语句
    Javascript条件语句之ifelse1<!DOCTYPEhtml>2<html>3<head>4<metacharset="utf-8">5<title>helloworld</title>6</head>......
  • JDK的版本有多少种,Java开发者应该选择哪一种?
    JDK的版本有多少种,Java开发者应该选择哪一种?先说结果,一般情况下,我们多数会选择OpenJDK或者AdoptOpenJDK的JDK实现,因为这是最精简最标准的版本,而且没有商业风险。另外,需要结......
  • java AddressUtils获取地址工具类
    /***获取地址类**@authorruoyi*/publicclassAddressUtils{privatestaticfinalLoggerlog=LoggerFactory.getLogger(AddressUtils.class);......
  • JAVA判断字符串是否包含中文或者包含中文字符
      privatestaticPatternpattern=Pattern.compile("[\u4E00-\u9FA5|\\!|\\,|\\。|\\(|\\)|\\《|\\》|\\“|\\”|\\?|\\:|\\;|\\【|\\】]");/***字符串是否......
  • java IpUtils获取ip工具类
    /***获取IP方法**@authorruoyi*/publicclassIpUtils{/***获取客户端IP**@paramrequest请求对象*@returnIP地址......
  • java 生成uuid
    /***提供通用唯一识别码(universallyuniqueidentifier)(UUID)实现**@authorruoyi*/publicfinalclassUUIDimplementsjava.io.Serializable,Comparable<UUID......
  • java 线程Threads工具类
    /***线程相关工具类.**@authorruoyi*/publicclassThreads{privatestaticfinalLoggerlogger=LoggerFactory.getLogger(Threads.class);/*......
  • JavaScript 中的数据是如何存储在内存中的
    JavaScript是什么类型的语言JavaScript是一种弱类型的、动态的语言。那这些特点意味着什么呢?弱类型:支持隐式类型转换的语言称为弱类型语言,不支持隐式类型转换的语言称......
  • java MimeTypeUtils媒体类型工具类
    /***媒体类型工具类**@authorruoyi*/publicclassMimeTypeUtils{publicstaticfinalStringIMAGE_PNG="image/png";publicstaticfinal......