当前位置:网站首页>Get the list of provinces and cities 【 project mall 】
Get the list of provinces and cities 【 project mall 】
2022-06-10 02:36:00 【Sun star moon cloud】
Get the list of provinces and cities 【 project Shopping Mall 】
- Get the list of provinces and cities
Get the list of provinces and cities
1 Get the list of provinces and cities – database
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',' Dongcheng District ');
insert into `t_dict_district` (`id`, `parent`, `code`, `name`) values('2','110100','110102',' Xicheng district ');
insert into `t_dict_district` (`id`, `parent`, `code`, `name`) values('3','110100','110103',' Chongwen District ');
insert into `t_dict_district` (`id`, `parent`, `code`, `name`) values('4','110100','110104',' Xuanwu District ');
insert into `t_dict_district` (`id`, `parent`, `code`, `name`) values('5','110100','110105',' Chaoyang District ');
parent Property represents the code number of the parent region , The parent code number of the province +86.
t_dict_district.sql
2 Get the list of provinces and cities - Entity class
Create a District Entity class .
package com.cy.store.entity;
import java.io.Serializable;
import java.util.Objects;
/** province / City / Entity class of area data */
public class District implements Serializable {
private Integer id;
private String parent;
private String code;
private String name;
District
3 Get the list of provinces and cities - Persistence layer
Query statement , Query according to the parent code .
select * from t_dict_district where parent =? order by code ASC
Abstract method definition .
DistrictMapper
package com.cy.store.mapper;
import com.cy.store.entity.District;
import java.util.List;
public class DistrictMapper {
/** * Query information according to parent code * @param parent Parent code * @return List of all areas under a parent area */
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">
<!-- Access to all provinces in the country / All cities in a province / All districts of a city :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
test
@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 Get the list of provinces and cities - The business layer
1. Create an interface IDistrictService, And define abstract methods .
package com.cy.store.service;
import com.cy.store.entity.District;
import java.util.List;
public interface IDistrictService {
/** * Query the information of the area according to the parent code ( Provincial city ) * @param parent Parent code * @return Multiple area information */
List<District> getParent(String parent);
}
2. establish DistrictServiceImpl Implementation class , Implement abstract methods .
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);
/** * During network data transmission , In order to avoid invalid data transmission , Invalid data can be set to null * Can save traffic , On the other hand, it improves efficiency */
for (District d:list) {
d.setId(null);
d.setParent(null);
}
return list;
}
}
DistrictService–getParent
test
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: Indicates that the current class is a test class , Will not be packaged with the project
@SpringBootTest
//@RunWith: Means to start this unit test class ( Unit test classes cannot be run ), You need to pass a parameter , Must be SpringRunner The instance type of
//@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 Get the list of provinces and cities - Control layer
5.1 Design request
/districts/
GET
String parent
JsonResult<List<District>>
5.2 Write a request
Create a class DistrictController class , Write a method to process the request in the class .
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;
// Anyone who districts The first requests are intercepted getByParent() Method
@RequestMapping({
"/",""})
public JsonResult<List<District>> getByParent(String parent){
List<District> data = districtService.getParent(parent);
return new JsonResult<>(OK,data);
}
}
LoginInterceptorConfigurer–districts
districts Request to be added to the white list
patterns.add("/districts/**");
test
Direct request server , To access and test
http://localhost:8080/districts?parent=86

http://localhost:8080/districts?parent=610000
6 Get the list of provinces and cities - Front page
addAddress.html
1. Comment out through js To complete the list of provinces and cities js Code
<!-- <script type="text/javascript" src="../js/distpicker.data.js"></script>-->
<!-- <script type="text/javascript" src="../js/distpicker.js"></script>-->
2. Check whether the front page is relevant when submitting the list data of provinces and cities name Properties and id attribute .
3. Run the front end to see if the data can be saved normally ( Except for the provincial and urban areas ).
addAddress–js
test

READ– Get the list of provinces and cities
边栏推荐
- JDBC入门练习,是版本导致的吗,这错误是什么意思啊?
- 51單片機入門——紅外通信
- SSL证书安装后网站还是显示不安全
- 28. How much do you know about subject management?
- Can write a desktop digital hour clock program without programming, which can be easily realized in only 3 steps
- Several evidences to prove marital infidelity
- Web server-side technical test question bank
- 正则表达式的边界问题。
- JS sequence to obtain the distance between two places
- 35 year old workplace anxiety, more and more people choose this
猜你喜欢
mysql 8.0.29 winx64. Graphic tutorial of zip installation and configuration method

微信小程序 音乐播放代码(播放方式,歌词滚动)

2、自然語言處理入門

Simulated 100 questions and answers of the third batch of Guangdong Provincial Safety Officer a certificate (principal) examination in 2022

openGauss“用户故事”正式上线!一键分享实践经验,限量版礼物等你拿

Can write a desktop digital hour clock program without programming, which can be easily realized in only 3 steps

How to use Google home speaker voice to control zhiting home cloud devices?

Stepper motor summary

How to do enterprise digital transformation? Three fusions and three transformations

获取省市区的名称【项目 商城】
随机推荐
Detailed billing information tables of cptevents and drgcodes (XIII)
flutter 双端扫码下载app
After the SSL certificate is installed, the website still shows insecurity
51 Introduction au micro - ordinateur à puce unique - - communication infrarouge
mysql 8.0.29 winx64. Graphic tutorial of zip installation and configuration method
Li Kou daily question - day 17 -349 Intersection of two data
command
openGauss“用户故事”正式上线!一键分享实践经验,限量版礼物等你拿
“无法访问 您可能没有权限使用网络资源”解决办法
Double pointer | 27 Removing Elements
uni-app 移动端本地储存数据库sqlite,无存储限制
Tips for college students' life: using tempermonkey to learn online lessons | check questions
Introduction to 51 single chip microcomputer infrared communication
Screen resolution
pycharm中numpy的安装与使用
^27 timer related problems
Postgresql中如何终止正在执行的查询
3、NLP模型
获取省市区列表【项目 商城】
Stepper motor summary