数据量太大了 , 就不给大家了!
四、具体实现省市区的实体类就不展示了 , 大家根据自己公司的设计稍微改动即可使用!
1. 树形VO/** * 地址业务父子 * * @author wangzhenjun * @date 2022/9/2 16:26 */@Datapublic class AddressVO implements Serializable {/*** ID*/@ApiModelProperty(valuehttps://www.huyubaike.com/biancheng/= "https://www.huyubaike.com/biancheng/ID")private Integer id;/*** 编码*/@ApiModelProperty(valuehttps://www.huyubaike.com/biancheng/= "")private String code;/*** 名称*/@ApiModelProperty(valuehttps://www.huyubaike.com/biancheng/= "")private String name; /*** 父编码*/private String parentCode;/*** 市区*/private List<AddressVO> children;}
2. 具体实现@Overridepublic Result address() {long l = System.currentTimeMillis();// 获取省市区List<AddressProvince> provinceList = addressProvinceMapper.selectList(Wrappers.<AddressProvince>lambdaQuery().eq(AddressProvince::getIsDeleted, 0).eq(AddressProvince::getIsLatest, 1));List<AddressCity> cityList = addressCityMapper.selectList(Wrappers.<AddressCity>lambdaQuery().eq(AddressCity::getIsDeleted, 0).eq(AddressCity::getIsLatest, 1));List<AddressDistrict> districtList = addressDistrictMapper.selectList(Wrappers.<AddressDistrict>lambdaQuery().eq(AddressDistrict::getIsDeleted, 0).eq(AddressDistrict::getIsLatest, 1));// 按照省code进行分组Map<String, List<AddressCity>> cityMap = cityList.stream().collect(Collectors.groupingBy(AddressCity::getProvinceCode));// 按照市code进行分组Map<String, List<AddressDistrict>> districtMap = districtList.stream().collect(Collectors.groupingBy(AddressDistrict::getCityCode));List<AddressVO> result = new ArrayList<>();for (AddressProvince province : provinceList) {// 获取某个省下的所有市List<AddressCity> addressCityList = cityMap.get(province.getCode());// 给树形对象赋省的数据AddressVO addressProvinceVO = new AddressVO();addressProvinceVO.setId(province.getId());addressProvinceVO.setCode(province.getCode());addressProvinceVO.setName(province.getName());List<AddressVO> cityResult = new ArrayList<>();for (AddressCity addressCity : addressCityList) {// 获取某个市下的所有区List<AddressDistrict> addressDistrictList = districtMap.get(addressCity.getCode());// 给树形对象赋市的数据AddressVO addressCityVO = new AddressVO();addressCityVO.setId(addressCity.getId());addressCityVO.setCode(addressCity.getCode());addressCityVO.setName(addressCity.getName());addressCityVO.setParentCode(province.getCode());List<AddressVO> districtResult = new ArrayList<>();// 便利每个市下面的所有区for (AddressDistrict addressDistrict : addressDistrictList) {// 给树形对象赋区的数据AddressVO addressDistrictVO = new AddressVO();addressDistrictVO.setId(addressDistrict.getId());addressDistrictVO.setCode(addressDistrict.getCode());addressDistrictVO.setName(addressDistrict.getName());addressDistrictVO.setParentCode(addressCity.getCode());districtResult.add(addressDistrictVO);}addressCityVO.setChildren(districtResult);cityResult.add(addressCityVO);}addressProvinceVO.setChildren(cityResult);result.add(addressProvinceVO);}System.out.println(System.currentTimeMillis() - l);return Result.success(result);}
4. 计算时长我们可以看到 , 一共297毫秒 , 小编的机器比较卡 , 支持cpu在60%情况下 , 200ms应该问题不大 , 可以放到redis缓存起来 , 这样减少IO交互 , 减少数据库的压力!!
文章插图
五、前端vue此时address为数组 , 按序放了选择的value , props:可以指定显示和选择的名称 , 默认是
value和label
<el-cascaderv-model="addReceiverForm.address"placeholder="请选择收货人地址":options="options":props="{checkStrictly: true,value:'code',label:'name'}"></el-cascader>========data:============addReceiverForm:{ address:[]}options: [],========method:============// 获取省市区initAddress() {listAddressAndChild().then(data =https://www.huyubaike.com/biancheng/> {this.options = data.data}).catch(() => {});},
经验总结扩展阅读
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 谈一次恋爱就心满意足的星座
- 樊文花水疗多久做一次合适?
- Java代码审计之实战某博客
- 洗碗机洗一次碗大概花多少钱 洗碗机不能洗什么
- 一次性储奶袋怎么加热
- 新筷子第一次用要开水煮吗
- java常用注解校验参数
- 记一次 .NET 某电子病历 CPU 爆高分析
- 【Java8新特性】- Optional应用
- Java 最长公共前缀