全国省市区三级联动(简单易懂)

实现全国省市区三级联动

1.数据库

脚本下载地址链接:https://pan.baidu.com/s/1099yqKDeLMCBrTfw9qo-QA
提取码:942y

2.服务端实现

2.1控制器

@RestController
@CrossOrigin
@RequestMapping("/api")
@Api(value = "省市区联动接口",tags = "介绍管理")
public class AreaController {
    @Autowired
    private getAreaService getAreaService;


    @GetMapping("/cities/{provinceId}")
    @ApiOperation("查找省所在的市")
    @ResponseBody
    public ResultVO listCity(@PathVariable("provinceId") String provinceId){
        List<City> cities = getAreaService.getCityByProvince(provinceId);
        return new ResultVO(ResStatus.OK,"success",cities);
    }

    @GetMapping("/districts/{cityId}")
    @ApiOperation("查找市所在的区")
    @ResponseBody
    public ResultVO listArea(@PathVariable("cityId") String cityId){
        List<Area> areas = getAreaService.getAreaByCity(cityId);
        return new ResultVO(ResStatus.OK,"success",areas);
    }

    @GetMapping("/provinces")
    @ApiOperation("查找全国所有的省份")
    @ResponseBody
    public ResultVO listProvince(){
        List<Province> provinceList = getAreaService.getAllProvince();
        return new ResultVO(ResStatus.OK,"success",provinceList);
    }
}

2.2service层

import com.qfedu.fmmall.entity.Area;
import com.qfedu.fmmall.entity.City;
import com.qfedu.fmmall.entity.Province;

import java.util.List;

public interface getAreaService {
    public List<City> getCityByProvince(String id);

    public List<Area> getAreaByCity(String id);

    public List<Province> getAllProvince();
}
@Service
public class getAreaServiceImpl implements getAreaService {
   @Autowired
   private GetAreaMapper getAreaMapper;

    @Override
    public List<City> getCityByProvince(String provinceId) {
       return getAreaMapper.getCityByProvince(provinceId);
    }

    @Override
    public List<Area> getAreaByCity(String cityId) {
        return getAreaMapper.getAreaByCity(cityId);
    }

    @Override
    public List<Province> getAllProvince() {
        List<Province> provinceList = getAreaMapper.getAllProvince();
        return provinceList;
    }
}

2.3mapper层

<resultMap id="getCityByProvince" type="com.qfedu.fmmall.entity.City">
    <id column="id" property="id" jdbcType="INTEGER" />
    <result column="cityID" property="cityID"/>
    <result column="city" property="city" jdbcType="VARCHAR" />
    <result column="father" property="father" jdbcType="VARCHAR" />
</resultMap>

<resultMap id="getAreaByCity" type="com.qfedu.fmmall.entity.Area">
        <id column="id" property="id" jdbcType="INTEGER" />
        <result column="areaID" property="areaID"/>
        <result column="area" property="area" jdbcType="VARCHAR" />
        <result column="father" property="father" jdbcType="VARCHAR" />
</resultMap>

<resultMap id="getAllProvince" type="com.qfedu.fmmall.entity.Province">
        <id column="id" property="id" jdbcType="INTEGER" />
        <result column="provinceID" property="provinceID"/>
        <result column="province" property="province" jdbcType="VARCHAR" />
</resultMap>

<select id="getCityByProvince" resultType="com.qfedu.fmmall.entity.City" resultMap="getCityByProvince">
    select id,cityID,city,father
    from hat_city c where c.father=#{id}
</select>

<select id="getAreaByCity" resultType="com.qfedu.fmmall.entity.Area" resultMap="getAreaByCity">
        select
            id,areaID,area,father
        from hat_area a where a.father =#{id}
</select>

<select id="getAllProvince" resultType="com.qfedu.fmmall.entity.Province">
    select id,provinceID,province
    from hat_province
</select>

3.前端实现

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Vue ElementUI CityData</title>
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
</head>
<body>
<div id="app">
    <label>
        <select v-model="selectedProvince" @change="getCities">
            <option value="">请选择省份</option>
            <option v-for="province in provinces" :value="province.provinceID">{{province.province}}</option>
        </select>
    </label>

    <label>
        <select v-model="selectedCity" @change="getDistricts">
            <option value="city.name">请选择城市</option>
            <option v-for="city in cities" :value="city.cityID">{{city.city}}</option>
        </select>
    </label>

    <label>
        <select v-model="selectedDistrict">
            <option value="district.name">请选择区县</option>
            <option v-for="district in districts" :value="district.areaID">{{district.area}}</option>
        </select>
    </label>

</div>
<script src="/js/Vue.js"></script>
<script src="/js/axios.min.js"></script>
<script src="https://unpkg.com/element-ui/lib/index.js"></script>
<script>
    var vm = new Vue({
        el: '#app',
        data:{
                provinces: [],
                cities: [],
                districts: [],
                selectedProvince: '',
                selectedCity: '',
                selectedDistrict: '',
        },
        methods: {
            getProvinces() {
                axios.get('http://localhost:8080/api/provinces')
                    .then(response => {
                        this.provinces = response.data.data;
                    })
                    .catch(error => {
                        console.error(error);
                    });
            },
            getCities() {
                const provinceId = this.selectedProvince;
                if (!provinceId) {
                    this.cities = [];
                    this.districts = [];
                    return;
                }
                axios.get('http://localhost:8080/api/cities/'+provinceId)
                    .then(response => {
                        this.cities = response.data.data;
                        this.districts = [];
                    })
                    .catch(error => {
                        console.error(error);
                    });
            },
            getDistricts() {
                const cityId = this.selectedCity;
                if (!cityId) {
                    this.districts = [];
                    return;
                }
                axios.get('http://localhost:8080/api/districts/'+cityId)
                    .then(response => {
                        this.districts = response.data.data;
                    })
                    .catch(error => {
                        console.error(error);
                    });
            }
        },
        mounted() {
            this.getProvinces();
        }
    });
</script>
</body>
</html>
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序猿阿文

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值