三级分层查询省市区数据

1、数据库数据

*

2、实现的效果

3、controller层

java 复制代码
@Api(tags = "地址库")
@RestController
@RequestMapping("/basic/city")
public class DataTChinaCityController extends BaseController {

    @Resource
    private IProductSaleDeliveryService productSaleDeliveryService;

    /**
     * 查询地址库-三级联动
     */
    @GetMapping(value = "/queryCity")
    public AjaxResult queryCity() {
        return AjaxResult.success(productSaleDeliveryService.cacheChinaCityDataWhereLevel());
    }
}

4、Service层

bash 复制代码
public interface IProductSaleDeliveryService {

    /**
     * 缓存,查询地址库-三级联动
     *
     * @return 全国城市数据
     */
    Collection<BaseDataTChinaCity> cacheChinaCityDataWhereLevel();

}

5、实现层

bash 复制代码
@Service
public class ProductSaleDeliveryServiceImpl implements IProductSaleDeliveryService {

// 配合guava缓存一起使用,依赖自己搜素添加即可
// 创建缓存
    Cache<String, Object> chinaCityDataCache = Caffeine.newBuilder()
            .expireAfterAccess(2, TimeUnit.HOURS) // 访问后过期时间 2小时
            .initialCapacity(64) // 设置初始容量
            .maximumSize(128) // 设置缓存的最大容量
            .build();

@Resource
    public IBaseDataTChinaCityService baseDataTChinaCityService;

/**
     * 缓存,查询地址库-三级联动
     *
     * @return 全国城市数据
     */
    @Override
    public Collection<BaseDataTChinaCity> cacheChinaCityDataWhereLevel() {
        try {
            // 设置redis的key
            String redisKey = "juepeiscm:basedata:china:city:leveldata";

            if (chinaCityDataCache.getIfPresent(redisKey) != null) {
                return (Collection<BaseDataTChinaCity>) chinaCityDataCache.getIfPresent(redisKey);
            }
            // 三级联动
            Collection<BaseDataTChinaCity> baseDataTChinaCityCollections = baseDataTChinaCityService.queryCity();
            // 放入缓存
            chinaCityDataCache.put(redisKey, baseDataTChinaCityCollections);
            return baseDataTChinaCityCollections;
        } catch (Exception e) {
            log.error("服务产品小程序获取全国城市数据失败=", e);
        }
        return null;
    }
}

6、地址库对象 t_china_city

bash 复制代码
@ApiModel(description ="地址库")
@Data
public class BaseDataTChinaCity extends BaseEntity {
    private static final long serialVersionUID = 1L;

    /**
     * id
     */
    @ApiModelProperty(value = "id")
    private Long indexId;

    @ApiModelProperty(value = "ids")
    private List<Long> indexIds;

    @ApiModelProperty(value = "城市名称")
    @Excel(name = "城市名称")
    private String cityName;

    /**
     * 父级ID
     */
    @ApiModelProperty(value = "父级ID")
    @Excel(name = "父级ID")
    private Long parentId;

    @ApiModelProperty(value = "父级IDs")
    private List<Long> parentIds;

    /**
     * 层级
     */
    @ApiModelProperty(value = "层级")
    @Excel(name = "层级")
    private Integer addressLevel;

    /**
     * 省
     */
    @ApiModelProperty(value = "省")
    @Excel(name = "省")
    private String province;

    /**
     * 市
     */
    @ApiModelProperty(value = "市")
    @Excel(name = "市")
    private String city;

    /**
     * 区
     */
    @ApiModelProperty(value = "区")
    @Excel(name = "区")
    private String district;

    /**
     * 区域
     */
    @ApiModelProperty(value = "区域")
    @Excel(name = "区域")
    private String area;


    /**
     * 0:启用  1:作废
     */
    @ApiModelProperty(value = "0:启用  1:作废")
    @Excel(name = "0:启用  1:作废")
    private Integer status;

    @ApiModelProperty(value = "子级")
    List<BaseDataTChinaCity> children;

    @ApiModelProperty(value = "城市名称")
    String label;
    @ApiModelProperty(value = "城市名称")
    String value;

    public String getLabel() {
        return cityName;
    }

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

    public String getValue() {
        return cityName;
    }

    public void setValue(String value) {
        this.value = value;
    }





    @ApiModelProperty(value = "bms推送状态(0:未推送,1:推送成功,2:推送失败)")
    private Integer bmsSendStatus;

    @ApiModelProperty(value = "bms推送次数")
    private Integer bmsSendNum;

    @ApiModelProperty(value = "bms最后推送时间")
    private Date bmsLastSendTime;

}

7、地址库Service接口

bash 复制代码
public interface IBaseDataTChinaCityService {
/**
     * 查询地址库-三级联动(不带参数)
     */
    Collection<BaseDataTChinaCity> queryCity();
}

8、查询地址库-三级联动(不带参)

java 复制代码
@Service
public class BaseDataTChinaCityServiceImpl implements IBaseDataTChinaCityService {

 @Autowired
    private BaseDataTChinaCityMapper baseDataTChinaCityMapper;


/**
     * 查询地址库-三级联动(不带参)
     *
     * @return 结果
     */
    @Override
    public Collection<BaseDataTChinaCity> queryCity() {
        return queryCityByEntity(null);
    }
}

/**
     * 查询地址库-三级联动(方法提取)
     *
     * @return 结果
     */
    public Collection<BaseDataTChinaCity> queryCityByEntity(BaseDataTChinaCity baseDataTChinaCity) {
        //查询所有城市
        List<BaseDataTChinaCity> provinceList = baseDataTChinaCityMapper.queryCity(baseDataTChinaCity);
        return getCityByEntity(provinceList);
    }

public Collection<BaseDataTChinaCity> getCityByEntity(List<BaseDataTChinaCity> provinceList){
        Map<Long, BaseDataTChinaCity> oneLevel = new HashMap<>();
        Map<Long, List<BaseDataTChinaCity>> twoLevel = new HashMap<>();
        Map<Long, List<BaseDataTChinaCity>> threeLevel = new HashMap<>();
        for (BaseDataTChinaCity city : provinceList) {
            switch (city.getAddressLevel()) {
                case 1:
                    oneLevel.put(city.getIndexId(), city);
                    break;
                case 2:
                    twoLevel.computeIfAbsent(city.getParentId(), k -> new ArrayList<>());
                    twoLevel.get(city.getParentId()).add(city);
                    break;
                case 3:
                    threeLevel.computeIfAbsent(city.getParentId(), k -> new ArrayList<>());
                    threeLevel.get(city.getParentId()).add(city);
                    break;
                default:
                    break;
            }
        }
        for (Map.Entry<Long, List<BaseDataTChinaCity>> tmp :
                twoLevel.entrySet()) {
            for (BaseDataTChinaCity parentEntity :
                    tmp.getValue()) {
                parentEntity.setChildren(threeLevel.get(parentEntity.getIndexId()));
            }
        }
        for (Map.Entry<Long, BaseDataTChinaCity> tmp :
                oneLevel.entrySet()) {
            BaseDataTChinaCity parent = tmp.getValue();
            parent.setChildren(twoLevel.get(parent.getIndexId()));
        }
        return oneLevel.values();
    }

9、地址库Mapper接口

java 复制代码
@Mapper
public interface BaseDataTChinaCityMapper {
/**
     * 查询地址库-三级联动
     *
     * @param baseDataTChinaCity 地址库
     * @return 结果
     */
    List<BaseDataTChinaCity> queryCity(BaseDataTChinaCity baseDataTChinaCity);

}

<select id="queryCity" parameterType="BaseDataTChinaCity" resultMap="BaseDataTChinaCityResult">
        <include refid="selectTChinaCityVo"/>
        <where>
            <if test="parentId != null ">and parent_id = #{parentId}</if>
            <if test="addressLevel != null ">and address_level = #{addressLevel}</if>
            <if test="province != null  and province != ''">and province = #{province}</if>
            <if test="city != null  and city != ''">and city = #{city}</if>
            <if test="district != null  and district != ''">and district = #{district}</if>
            <if test="area != null  and area != ''">and area = #{area}</if>
            <if test="status != null ">and status = #{status}</if>
            <if test="bmsSendStatus != null">and bms_send_status = #{bmsSendStatus}</if>
            <if test="indexId != null">and index_id = #{indexId}</if>
            <if test="indexIds != null and indexIds.size() > 0">
                and index_id in
                <foreach collection="indexIds" item="item" separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
            <if test="parentIds != null and parentIds.size() > 0">
                and parent_id in
                <foreach collection="parentIds" item="item" separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>

<sql id="selectTChinaCityVo">
        select index_id, parent_id, address_level, province, city, district, status,city_name,area from t_china_city
    </sql>

<resultMap type="BaseDataTChinaCity" id="BaseDataTChinaCityResult">
        <result property="indexId" column="index_id"/>
        <result property="cityName" column="city_name"/>
        <result property="parentId" column="parent_id"/>
        <result property="addressLevel" column="address_level"/>
        <result property="province" column="province"/>
        <result property="city" column="city"/>
        <result property="district" column="district"/>
        <result property="area" column="area"/>
        <result property="status" column="status"/>
    </resultMap>

学费了吗?有问题请留言!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

相关推荐
梅小西爱学习11 分钟前
线上CPU飙到100%?别慌,这3个工具比top快10倍!
java·后端·cpu
没有bug.的程序员16 分钟前
金融支付分布式架构实战:从理论到生产级实现
java·分布式·微服务·金融·架构·分布式调度系统
00后程序员张16 分钟前
Jenkins Pipeline post指令详解
java·开发语言
程序员阿达23 分钟前
开题报告之基于SpringBoot框架的路面故障信息上报系统设计与实现
java·spring boot·后端
哞哞不熬夜35 分钟前
JavaEE--SpringIoC
java·开发语言·spring boot·spring·java-ee·maven
滑水滑成滑头36 分钟前
**点云处理:发散创新,探索前沿技术**随着科技的飞速发展,点云处理技术在计算机视觉、自动驾驶、虚拟现实等领域的应用愈发广
java·python·科技·计算机视觉·自动驾驶
千里马-horse1 小时前
fastddsgen.jar 简介
java·jar·fast dds·fastddsgen
TT哇1 小时前
【Maven】Maven设置国内源
java·maven
dyj0951 小时前
【Devops-Jenkins自动将Java Maven工程编译成jar、并打成Docker镜像,并上传Harbor】
java·jenkins·devops
gordon~91 小时前
Spring 的bean是安全的吗
java·安全·spring·bean