当前位置:网站首页>获取省市区的名称【项目 商城】
获取省市区的名称【项目 商城】
2022-06-10 02:33:00 【日星月云】
获取省市区的名称
1 获取省市区的名称–持久层
1.根据当前的code来获取当前省市区的名称,对应就是一条查询语句。
select * from t_dist_district where code=?
2.在DistrictMapper接口中定义出来。
String findNameByCode(String code);
3.在DistrictMapper.xml文件中添加抽象方法的映射。
<!-- 根据省/市/区的行政代号获取省/市/区的名称:String findNameByCode(String code) -->
<select id="findNameByCode" resultType="java.lang.String">
SELECT name FROM t_dict_district WHERE code=#{code}
</select>
DistrictMapper–findByParent
测试
4.单元测试编写。
package com.cy.store.mapper;
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 java.util.List;
//@SpringBootTest:表示标注当前的类是测试类,不会随同项目一块打包
@SpringBootTest
//@RunWith:表示启动这个单元测试类(单元测试类是不能够运行的),需要传递一个参数,必须是SpringRunner的实例类型
//@RunWith(SpringRunner.class)
public class DistrictMapperTests {
@Autowired
private DistrictMapper districtMapper;
@Test
public void findByParent(){
List<District> list = districtMapper.findByParent("110100");
for (District d:list) {
System.out.println(d);
}
}
@Test
public void findNameByCode(){
String name = districtMapper.findNameByCode("610000");
System.out.println(name);
}
}
DistrictMapperTests–findByParent
2 获取省市区的名称–业务层
1.在业务层没有异常要处理。
2.定义对应的业务层接口的抽象方法。
String getNameByCode(String code);
3.在子类中进行实现
@Override
public String getNameByCode(String code) {
return districtMapper.findNameByCode(code);
}
DistrictService–getNameByCode
测试
4.测试可以省略不写(超过8行以上的代码都要进行独立的测试)
3 获取省市区的名称–业务层优化
1.AddressServiceImpl添加地址层依赖于IDistrictService层。
//在添加用户的收货地址的业务层依赖于IDistrictService的业务接口
@Autowired
private IDistrictService districtService;
2.在addNewAddress方法中IDistrictService接口中获取到的省市区数据转移到address对象中,这个对象中就包含了所有的用户收货地址信息。
//对address对象中的数据进行补全:省市区
String provinceName = districtService.getNameByCode(address.getProvinceCode());
String cityName = districtService.getNameByCode(address.getCityCode());
String areaName = districtService.getNameByCode(address.getAreaCode());
address.setProvinceName(provinceName);
address.setCityName(cityName);
address.setAreaName(areaName);
AddressServiceImpl–IDistrictService
4 获取省市区的名称–前端页面
1.addAddress.html页面中来编写对应省市区展示即根据隐晦的不同选择来显示对应的标签中的内容。
2.编写相关的事件代码。
<!-- showList -->
<script type="text/javascript">
//value属性用于表示当前区域的code值
let defaultOption = '<option value="0">----- 请选择 -----</option>';
$(document).ready(function() {
showProvinceList();
//设置默认的“请选择”的值,作为控件的默认值
$("#city-list").append(defaultOption);
$("#area-list").append(defaultOption);
});
/** * change()函数用于测试某个控件是否发生改变,,一旦发生改变就会触发参数的函数。 * 需要传递一个function(){} */
$("#province-list").change(function() {
showCityList();
});
$("#city-list").change(function() {
showAreaList();
});
//省的下拉列表数据展示
function showProvinceList() {
$("#province-list").append(defaultOption);
$.ajax({
url: "/districts",
type: "GET",
data: "parent=86",
dataType: "JSON",
success: function(json) {
if (json.state == 200) {
let list = json.data;
console.log("count=" + list.length);
for (let i = 0; i < list.length; i++) {
console.log(list[i].name);
let option = '<option value="' + list[i].code + '">' + list[i].name + '</option>';
$("#province-list").append(option);
}
}else{
alert("省/直辖市信息加载失败!");
}
}
});
}
function showCityList() {
//先获取到行政区父代码
let parent = $("#province-list").val();
//表示清空select下拉列表中的所有option元素
$("#city-list").empty();
$("#area-list").empty();
//填充默认值
$("#city-list").append(defaultOption);
$("#area-list").append(defaultOption);
if (parent == 0) {
return;
}
$.ajax({
url: "/districts",
type: "GET",
data: "parent=" + parent,
dataType: "JSON",
success: function(json) {
if (json.state == 200) {
let list = json.data;
console.log("count=" + list.length);
for (let i = 0; i < list.length; i++) {
console.log(list[i].name);
let option = '<option value="' + list[i].code + '">' + list[i].name + '</option>';
$("#city-list").append(option);
}
}else{
alert("城市信息加载失败!");
}
}
});
}
function showAreaList() {
let parent = $("#city-list").val();
$("#area-list").empty();
$("#area-list").append(defaultOption);
if (parent == 0) {
return;
}
$.ajax({
url: "/districts",
type: "GET",
data: "parent=" + parent,
dataType: "JSON",
success: function(json) {
if (json.state == 200) {
let list = json.data;
console.log("count=" + list.length);
for (let i = 0; i < list.length; i++) {
console.log(list[i].name);
let option = '<option value="' + list[i].code + '">' + list[i].name + '</option>';
$("#area-list").append(option);
}
}else{
alert("县区信息加载失败!");
}
}
});
}
</script>
<!-- showList -->
addAddress.html–showList
测试

README–获取省市区的名称
边栏推荐
- Detailed billing information tables of cptevents and drgcodes (XIII)
- CSP 202112-1 sequence query (detailed explanation)
- Dichotomy | 35 Search insert location
- JDBC入门练习,是版本导致的吗,这错误是什么意思啊?
- Smart cloud light gateway service to improve production management efficiency
- Mongodb learning and sorting (concept analysis)
- How to take a screenshot of the drop-down menu of the software
- Double pointer | 27 Removing Elements
- VirtualBox virtual machine network card settings
- Unity netcode for GameObject 1.0.0 learning notes (I) - Hello World demo
猜你喜欢

如何对软件的下拉菜单截图

Cordova website packaging_ Cordova packaging website

程序员日常开发的八荣八耻

2、自然语言处理入门

How to take a screenshot of the drop-down menu of the software

Unity基本操作

The sideline income is three times as much as I do as a programmer. What is life like on side B outside of work?

JDBC introductory exercise, is it caused by the version? What does this error mean?

1. Getting started with pytorch

MySQL table management (file)
随机推荐
Internal resources of stm32
Screen resolution
Apache POI learning notes - Excel
Simulated 100 questions and answers of the third batch of Guangdong Provincial Safety Officer a certificate (principal) examination in 2022
JDBC introductory exercise, is it caused by the version? What does this error mean?
Operation of simulated examination platform of recurrent training question bank for operation of refrigeration and air conditioning equipment in 2022
CSP 202104-1 灰度直方图
Mysql database foreign key foreing key
Can write a desktop digital hour clock program without programming, which can be easily realized in only 3 steps
3. NLP model
二分法 | 35. 搜索插入位置
How to solve the problem of incorrect IP comments after WordPress starts CDN
“无法访问 您可能没有权限使用网络资源”解决办法
2022 mobile crane driver test questions simulation test platform operation
1、pytorch入门
Boundary problems of regular expressions.
Technical dry goods | linkis1.0.2 installation and use guide
Lm03 who told you that cross species must be arbitraged?
Several evidences to prove marital infidelity
Unity basic operation