栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

获取省市区列表【项目 商城】

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

获取省市区列表【项目 商城】

获取省市区列表【项目 商城】
  • 获取省市区列表
    • 1 获取省市区列表--数据库
    • 2 获取省市区列表-实体类
    • 3 获取省市区列表-持久层
    • 测试
    • 4 获取省市区列表-业务层
    • 测试
    • 5 获取省市区列表-控制层
      • 5.1 设计请求
      • 5.2 编写请求
    • 测试
      • 6 获取省市区列表-前端页面
    • 测试

获取省市区列表 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 {
    
    List findByParent(Integer parent);
}

DistrictMapper.xml




    
    
        SELECT * FROM t_dict_district WHERe parent=#{parent} ORDER BY code ASC
    


DistrictMapper–findByParent


测试
 @Autowired
    private DistrictMapper districtMapper;

    @Test
    public void findByParent(){
        List 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 {
    
    List 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 getParent(String parent) {
        List list = districtMapper.findByParent(parent);
        
        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 list = districtService.getParent("86");
        for (District d : list) {
            System.err.println(d);
        }
    }


}



DistrictServiceTests–getParent


5 获取省市区列表-控制层 5.1 设计请求
/districts/
GET 
String parent
JsonResult>
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> getByParent(String parent){
        List 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代码



2.检查前端页面在提交省市区列表数据时是否有相关name属性和id属性。
3.运行前端看是否还可以正常保存数据(除了省市区之外)。

addAddress–js


测试

READ–获取省市区列表

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/962507.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号