获取省市区列表
1 获取省市区列表–数据库
create table `t_dict_district` ( `id` int (11) NOT NULL AUTO_INCREMENT, `parent` varchar (6) DEFAULT NULL, `code` varchar (6) DEFAULT NULL, `name` varchar (16) DEFAULT NULL, PRIMARY KEY (`id`) ); insert into `t_dict_district` (`id`, `parent`, `code`, `name`) values('1','110100','110101','东城区'); insert into `t_dict_district` (`id`, `parent`, `code`, `name`) values('2','110100','110102','西城区'); insert into `t_dict_district` (`id`, `parent`, `code`, `name`) values('3','110100','110103','崇文区'); insert into `t_dict_district` (`id`, `parent`, `code`, `name`) values('4','110100','110104','宣武区'); insert into `t_dict_district` (`id`, `parent`, `code`, `name`) values('5','110100','110105','朝阳区');
parent 属性表示父区域的代码号,省的父代码号+86。
t_dict_district.sql
2 获取省市区列表-实体类
创建个District实体类。
package com.cy.store.entity; import java.io.Serializable; import java.util.Objects; /** 省/市/区数据的实体类 */ public class District implements Serializable { private Integer id; private String parent; private String code; private String name;
District
3 获取省市区列表-持久层
查询语句,根据父代号进行查询。
select * from t_dict_district where parent =? order by code ASC
抽象方法定义。
DistrictMapper
package com.cy.store.mapper; import com.cy.store.entity.District; import java.util.List; public class DistrictMapper { /** * 根据父代号查询信息 * @param parent 父代号 * @return 某个父区域下的所有区域列表 */ List<District> findByParent(Integer parent); }
DistrictMapper.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.cy.store.mapper.DistrictMapper"> <!-- 获取全国所有省/某省所有市/某市所有区:List<District> findByParent(String parent) --> <select id="findByParent" resultType="com.cy.store.entity.District"> SELECT * FROM t_dict_district WHERE parent=#{parent} ORDER BY code ASC </select> </mapper>
DistrictMapper–findByParent
测试
@Autowired private DistrictMapper districtMapper; @Test public void findByParent(){ List<District> list = districtMapper.findByParent("110100"); for (District d:list) { System.out.println(d); } }
master → origin/master
4 获取省市区列表-业务层
1.创建接口IDistrictService,并定义抽象方法。
package com.cy.store.service; import com.cy.store.entity.District; import java.util.List; public interface IDistrictService { /** * 根据父代号来查询区域的信息(省市区) * @param parent 父代号 * @return 多个区域信息 */ List<District> getParent(String parent); }
2.创建DistrictServiceImpl实现类,实现抽象的方法。
package com.cy.store.service.impl; import com.cy.store.entity.District; import com.cy.store.mapper.DistrictMapper; import com.cy.store.service.IDistrictService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class DistrictServiceImpl implements IDistrictService { @Autowired private DistrictMapper districtMapper; @Override public List<District> getParent(String parent) { List<District> list = districtMapper.findByParent(parent); /** * 在进行网络数据传输时,为了尽量避免无效的数据传递,可以将无效数据设置为null * 可以节省流量,另一方面提升了效率 */ for (District d:list) { d.setId(null); d.setParent(null); } return list; } }
DistrictService–getParent
测试
package com.cy.store.service; import com.cy.store.entity.Address; import com.cy.store.entity.District; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.yaml.snakeyaml.events.Event; import java.util.List; //@SpringBootTest:表示标注当前的类是测试类,不会随同项目一块打包 @SpringBootTest //@RunWith:表示启动这个单元测试类(单元测试类是不能够运行的),需要传递一个参数,必须是SpringRunner的实例类型 //@RunWith(SpringRunner.class) public class DistrictServiceTests { @Autowired private IDistrictService districtService; @Test public void getParent(){ List<District> list = districtService.getParent("86"); for (District d : list) { System.err.println(d); } } }
DistrictServiceTests–getParent
5 获取省市区列表-控制层
5.1 设计请求
/districts/ GET String parent JsonResult<List<District>>
5.2 编写请求
创建一个类DistrictController类,在类中编写处理请求的方法。
package com.cy.store.controller; import com.cy.store.entity.District; import com.cy.store.service.IDistrictService; import com.cy.store.util.JsonResult; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController @RequestMapping("districts") public class DistrictController extends BaseController { @Autowired private IDistrictService districtService; //凡是districts开头的请求都被拦截到getByParent()方法 @RequestMapping({"/",""}) public JsonResult<List<District>> getByParent(String parent){ List<District> data = districtService.getParent(parent); return new JsonResult<>(OK,data); } }
LoginInterceptorConfigurer–districts
districts请求添加到白名单中
patterns.add("/districts/**");
测试
直接请求服务器,来访问进行测试
http://localhost:8080/districts?parent=86
http://localhost:8080/districts?parent=610000
6 获取省市区列表-前端页面
addAddress.html
1.注释掉通过js来完成的省市区列表的js代码
<!-- <script type="text/javascript" src="../js/distpicker.data.js"></script>--> <!-- <script type="text/javascript" src="../js/distpicker.js"></script>-->
2.检查前端页面在提交省市区列表数据时是否有相关name属性和id属性。
3.运行前端看是否还可以正常保存数据(除了省市区之外)。
addAddress–js
测试
READ–获取省市区列表