Mysql全国省市区数据,以及【java】树形结构Api

Mysql全国省市区数据,以及【java】树形结构Api

全国省市区sql语句

本文资源已绑定,直接下载即可

Controller层

@Api(tags = {"省市表"})
@RestController
@RequestMapping("/areaData")
public class AreaDataController {

    @Resource
    private IAreaDataService areaDataService;

    @ApiOperation(value = "查询省市树形数据")
    @GetMapping("/findCityTree")
    public AjaxResult findCityTree(){
        return AjaxResult.success(areaDataService.findCityTree());
    }
}

Service层

@Service
public class AreaDataServiceImpl implements IAreaDataService {

    @Resource
    private AreaDataMapper areaDataMapper;

    @Override
    public List<TreeSelect> findCityTree() {
        List<AreaData> areaDataList = this.areaDataMapper.selectList();
        if(CollectUtils.isEmpty(areaDataList)){
            return Lists.newArrayList();
        }
        return buildCityTree(areaDataList);
    }

    /**
     * 获取前端需要的树形结构
     * @param areaDataList 省市区数据
     * @return 树形数据
     */
    private List<TreeSelect> buildCityTree(List<AreaData> areaDataList) {
        List<AreaData> returnList = Lists.newArrayList();
        List<Long> tempList = areaDataList.stream().map(AreaData::getId).collect(Collectors.toList());
        for (AreaData area : areaDataList) {
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(area.getParentId())) {
                recursionFn(areaDataList, area);
                returnList.add(area);
            }
        }
        if (returnList.isEmpty()) {
            returnList = areaDataList;
        }
        return returnList.stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    /**
     * 递归列表
     */
    private void recursionFn(List<AreaData> list, AreaData t) {
        // 得到子节点列表
        List<AreaData> childList = getChildList(list, t);
        t.setChildren(childList);
        for (AreaData tChild : childList) {
            if (getChildList(list, tChild).size() > 0) {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<AreaData> getChildList(List<AreaData> list, AreaData c) {
        List<AreaData> cityList = Lists.newArrayList();
        for (AreaData n : list) {
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == c.getId()) {
                cityList.add(n);
            }
        }
        return cityList;
    }
}

所需的实体类

@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@ApiModel(value="AreaData对象", description="")
public class AreaData implements Serializable {

    private static final long serialVersionUID = 1L;

    private Long id;

    private String cityName;

    private Long parentId;

    private String shortName;

    private Integer depth;

    private String cityCode;

    private String zipCode;

    private String mergerName;

    private String longitude;

    private String latitude;

    private String pinyin;

    private Integer isUse;

    private List<AreaData> children;
}

public class TreeSelect implements Serializable
{
    private static final long serialVersionUID = 1L;

    /** 节点ID */
    private Long id;

    /** 节点名称 */
    private String label;

    /** 子节点 */
    @JsonInclude(JsonInclude.Include.NON_EMPTY)
    private List<TreeSelect> children;

    public TreeSelect()
    {

    }

    public TreeSelect(AreaData areaData) {
        this.id = areaData.getId();
        this.label = areaData.getCityName();
        List<AreaData> children = areaData.getChildren();
        if(CollectUtils.isNotEmpty(children)){
            this.children = children.stream().map(TreeSelect::new).collect(Collectors.toList());
        }
    }

    public Long getId()
    {
        return id;
    }

    public void setId(Long id)
    {
        this.id = id;
    }

    public String getLabel()
    {
        return label;
    }

    public void setLabel(String label)
    {
        this.label = label;
    }

    public List<TreeSelect> getChildren()
    {
        return children;
    }

    public void setChildren(List<TreeSelect> children)
    {
        this.children = children;
    }
}

Mapper(Dao)层

public interface AreaDataMapper {
    List<AreaData> selectList();
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.yph.system.mapper.AreaDataMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.yph.system.api.domain.AreaData">
        <result column="id" property="id"/>
        <result column="city_name" property="cityName"/>
        <result column="parent_id" property="parentId"/>
        <result column="short_name" property="shortName"/>
        <result column="depth" property="depth"/>
        <result column="city_code" property="cityCode"/>
        <result column="zip_code" property="zipCode"/>
        <result column="merger_name" property="mergerName"/>
        <result column="longitude" property="longitude"/>
        <result column="latitude" property="latitude"/>
        <result column="pinyin" property="pinyin"/>
        <result column="is_use" property="isUse"/>
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id, city_name, parent_id, short_name, depth, city_code, zip_code
    </sql>

    <select id="selectList" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from area_data
    </select>
</mapper> 

更多有趣内容可以关注公众号~
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值