欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 新闻 > 会展 > 省市区三级联动(后端)

省市区三级联动(后端)

2025/1/15 1:10:54 来源:https://blog.csdn.net/2301_78703470/article/details/144919881  浏览:    关键词:省市区三级联动(后端)

前提:springboot、mybatis-plus、swagger

数据库:

文章顶部

实体类:

City

package com.itfly.entity;import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import java.time.LocalDateTime;
import java.io.Serializable;import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;/*** <p>* 基础表-省市区* </p>** @author author* @since 2025-01-02*/
@Data
@ApiModel(value="City对象", description="市")
public class City implements Serializable {private static final long serialVersionUID = 1L;@ApiModelProperty(value = "ID")@TableId(value = "city_id", type = IdType.AUTO)private Long cityId;@ApiModelProperty(value = "创建时间")private LocalDateTime createTime;@ApiModelProperty(value = "更新时间")private LocalDateTime updateTime;@ApiModelProperty(value = "名称")private String name;@ApiModelProperty(value = "区域编号")private String code;@ApiModelProperty(value = "层级 1,2,3")private Integer level;@ApiModelProperty(value = "父级ID")private Integer parentId;@ApiModelProperty(value = "拼音首字母缩写")private String acronym;@ApiModelProperty(value = "排序")private Integer sort;}

DTO:省份---ProvinceDto 

package com.itfly.dto.city;import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;import java.util.List;@Data
@ApiModel(value="ProvinceDto", description="省")
public class ProvinceDto {@ApiModelProperty(value = "ID")private Long cityId;@ApiModelProperty(value = "名称")private String name;@ApiModelProperty(value = "区域编号")private String code;@ApiModelProperty(value = "层级 1,2,3")private Integer level;@ApiModelProperty(value = "父级ID")private Integer parentId;@ApiModelProperty(value = "2级市列表")private List<CityDto> cityList;
}

市:CityDto

package com.itfly.dto.city;import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;import java.util.List;/*** <p>* 基础表-省市区* </p>** @author author* @since 2025-01-02*/
@Data
@ApiModel(value="CityDto", description="市")
public class CityDto {@ApiModelProperty(value = "ID")private Long cityId;@ApiModelProperty(value = "名称")private String name;@ApiModelProperty(value = "区域编号")private String code;@ApiModelProperty(value = "层级 1,2,3")private Integer level;@ApiModelProperty(value = "父级ID")private Integer parentId;@ApiModelProperty(value = "3级区县列表")private List<DistrictDto> districtList;}

区县:DistrictDto

package com.itfly.dto.city;import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;@Data@ApiModel(value="DistrictDto", description="区县")
public class DistrictDto {@ApiModelProperty(value = "ID")private Long cityId;@ApiModelProperty(value = "名称")private String name;@ApiModelProperty(value = "区域编号")private String code;@ApiModelProperty(value = "层级 1,2,3")private Integer level;@ApiModelProperty(value = "父级ID")private Integer parentId;}

 业务层逻辑:

控制器:

package com.itfly.controller;import com.itfly.conf.result.R;
import com.itfly.service.ICityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;/*** <p>* 基础表-省市区 前端控制器* </p>** @author author* @since 2025-01-02*/
@RestController
@RequestMapping("/city")
@Api(tags = "省市区管理")
public class CityController {@Autowiredprivate ICityService cityService;//查询省市列表@GetMapping("/getAll")@ApiOperation(value = "查询省市列表(三级联动)")public R getProvince() {return cityService.getProvince();}
}

service:

package com.itfly.service;import com.itfly.conf.result.R;
import com.itfly.entity.City;
import com.baomidou.mybatisplus.extension.service.IService;/*** <p>* 基础表-省市区 服务类* </p>** @author author* @since 2025-01-02*/
public interface ICityService extends IService<City> {R getProvince();
}
ServiceImpl
package com.itfly.service.impl;import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itfly.conf.result.R;
import com.itfly.dto.city.DistrictDto;
import com.itfly.dto.city.ProvinceDto;
import com.itfly.entity.City;
import com.itfly.dto.city.CityDto;
import com.itfly.mapper.CityMapper;
import com.itfly.service.ICityService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itfly.vo.CityVo;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;import java.util.List;/*** <p>* 基础表-省市区 服务实现类* </p>** @author author* @since 2025-01-02*/
@Service
public class CityServiceImpl extends ServiceImpl<CityMapper, City> implements ICityService {@Overridepublic R getProvince() {// 创建一个QueryWrapper用于查询省份(level为1)QueryWrapper<City> queryWrapperProvince = new QueryWrapper<>();queryWrapperProvince.eq("level", 1);// 查询所有省份List<City> listProvince = list(queryWrapperProvince);// 将查询到的省份列表转换为ProvinceDto列表List<ProvinceDto> provinceDtos = listProvince.stream().map(province -> {ProvinceDto provinceDto = new ProvinceDto();// 设置省份的基本信息provinceDto.setCityId(province.getCityId());provinceDto.setName(province.getName());provinceDto.setCode(province.getCode());provinceDto.setLevel(province.getLevel());provinceDto.setParentId(province.getParentId());// 创建一个QueryWrapper用于查询城市(level为2且parent_id为省份的cityId)QueryWrapper<City> queryWrapperCity = new QueryWrapper<>();queryWrapperCity.eq("level", 2).eq("parent_id", province.getCityId());// 查询所有城市List<City> listCity = list(queryWrapperCity);// 将查询到的城市列表转换为CityDto列表List<CityDto> cityDtos = listCity.stream().map(city -> {CityDto cityDto = new CityDto();// 设置城市的基本信息cityDto.setCode(city.getCode());cityDto.setName(city.getName());cityDto.setLevel(city.getLevel());cityDto.setParentId(city.getParentId());cityDto.setCityId(city.getCityId());// 创建一个QueryWrapper用于查询区县(level为3且parent_id为城市的cityId)QueryWrapper<City> queryWrapperArea = new QueryWrapper<>();queryWrapperArea.eq("level", 3).eq("parent_id", city.getCityId());// 查询所有区县List<City> listArea = list(queryWrapperArea);// 将查询到的区县列表转换为DistrictDto列表List<DistrictDto> areaDtos = listArea.stream().map(area -> {DistrictDto areaDto = new DistrictDto();// 设置区县的基本信息areaDto.setCode(area.getCode());areaDto.setName(area.getName());areaDto.setLevel(area.getLevel());areaDto.setParentId(area.getParentId());areaDto.setCityId(area.getCityId());return areaDto;}).toList();// 设置城市对应的区县列表cityDto.setDistrictList(areaDtos);return cityDto;}).toList();// 设置省份对应的城市列表provinceDto.setCityList(cityDtos);return provinceDto;}).toList();// 返回封装好的省份、城市和区县列表return R.Success(provinceDtos);}}

打开swagger查看:

00ba11ef9b174a13a7f5d55e9d5524ff.png

 

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com