前提:springboot、mybatis-plus、swagger
数据库:
文章顶部
实体类:
City
java
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
java
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
java
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
java
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;
}
业务层逻辑:
控制器:
java
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 {
@Autowired
private ICityService cityService;
//查询省市列表
@GetMapping("/getAll")
@ApiOperation(value = "查询省市列表(三级联动)")
public R getProvince() {
return cityService.getProvince();
}
}
service:
java
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
java
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 {
@Override
public 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查看: