地区sql文件在我上传的资源中sys_region.sql,直接导入数据库即可,大家放心不需要积分的哟!
地区资源下载地址,冲!
- Controller
/**
* <p>
* 省市区三级联动
* </p>
*
* @author LiWT
* @since 2021-07-18
*/
@RestController
@RequestMapping("/region")
public class RegionController {
@Autowired
private IRegionService regionService;
/**
* 省市区三级联动查询
* 省:{parentId:10000000,levelId:1}
* 市:{parentId:所选省的id,levelId:2}
* 区:{parentId:所选市的id,levelId:3}
*
* @param region parentId、levelId
* @return 省市区列表
*/
@PostMapping("/query")
public R query(@RequestBody Region region) {
return R.ok(regionService.query(region));
}
}
- Service
/**
* <p>
* 服务类
* </p>
*
* @author LiWT
* @since 2021-07-18
*/
public interface IRegionService extends IService<Region> {
/**
* 省市区联动查询
*
* @param region 父级ID、层级
* @return 省市区列表
*/
List<Region> query(Region region);
}
- ServiceImpl
/**
* <p>
* 服务实现类
* </p>
*
* @author LiWT
* @since 2021-07-18
*/
@Service
public class RegionServiceImpl extends ServiceImpl<RegionMapper, Region> implements IRegionService {
@Override
public List<Region> query(Region region) {
LambdaQueryWrapper<Region> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(Region::getParentId, region.getParentId());
queryWrapper.eq(Region::getLevelId, region.getLevelId());
return this.baseMapper.selectList(queryWrapper);
}
}
- Mapper
/**
* <p>
* Mapper 接口
* </p>
*
* @author LiWT
* @since 2021-07-18
*/
@Mapper
@Repository
public interface RegionMapper extends BaseMapper<Region> {
}
- Entity
/**
* <p>
* 省市区地区表
* </p>
*
* @author LiWT
* @since 2021-07-18
*/
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("sys_region")
@ApiModel(value="Region对象", description="")
public class Region extends Model<Region> {
private static final long serialVersionUID = 1L;
@TableId(value = "id", type = IdType.AUTO)
private Long id;
@ApiModelProperty(value = "行政区划代码")
private Long code;
@ApiModelProperty(value = "名称")
private String name;
@ApiModelProperty(value = "上级id")
private Long parentId;
@ApiModelProperty(value = "层级")
private String levelId;
@Override
protected Serializable pkVal() {
return this.id;
}
}