省市区SQL(mysql、postgrel)-接口实现(java)

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,集群版 2核4GB 100GB
推荐场景:
搭建个人博客
云数据库 RDS MySQL,高可用版 2核4GB 50GB
简介: 省市区SQL(mysql、postgrel)-接口实现(java)

后端技术:springboot、mysql、mybatis-plus

一、数据库及sql

下载SQL,导入数据库

MySql-Sql:region.sql_SpringBoot区划代码-Dell文档类资源-CSDN下载

PostGrel-Sql:https://download.csdn.net/download/qq_29752857/24293228

工程文件及SQL:demoregion.rar_SpringBoot区划代码-网络安全文档类资源-CSDN下载

建表语句:

MySQL

DROP TABLE IF EXISTS `region`;
CREATE TABLE `region`  (
  `id` bigint(11) UNSIGNED NOT NULL AUTO_INCREMENT,
  `code` bigint(12) DEFAULT NULL COMMENT '行政区划代码',
  `name` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '名称',
  `parent_id` bigint(11) DEFAULT NULL COMMENT '上级id',
  `level_id` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT 'level_id',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 10003790 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

PostGrel

DROP TABLE IF EXISTS "public"."region";
CREATE TABLE "public"."region" (
  "id" int8 NOT NULL,
  "code" int8,
  "name" varchar(255) COLLATE "pg_catalog"."default",
  "parent_id" varchar COLLATE "pg_catalog"."default",
  "level_id" int2
)
;

二、后台接口(mysql数据库)

springboot依赖

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
 
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.3.2</version>
        </dependency>
 
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.10</version>
        </dependency>
 
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.28</version>
            <scope>runtime</scope>
        </dependency>
 
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf-8
    username: root
    type: com.alibaba.druid.pool.DruidDataSource
    password: 123456
server:
  port: 6969

省市区实体类

import lombok.Data;
 
import java.io.Serializable;
 
@Data
public class Region  implements Serializable {
    private String id;
    private String code;
    private String name;
    private String parentId;
    private String levelId;
    }

省市区mapper

 
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.demo.model.Region;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
 
import java.util.List;
 
@Mapper
public interface RegionMapper  extends BaseMapper<Region> {
    @Select("select * from region where level_id=1")
    List<Region> findListProvince();
    @Select("select * from region where level_id=2 and parent_id=#{parent_id}")
    List<Region> findListCity(Long parent_id);
    @Select("select * from region where level_id=3 and parent_id=#{parent_id}")
    List<Region> findListCounty(Long parent_id);
}

返回实体类

 
import com.fasterxml.jackson.annotation.JsonIgnore;
 
import java.util.LinkedHashMap;
import java.util.List;
 
 
/**
 * $.ajax后需要接受的JSON
 *
 */
public class AjaxJson {
 
  private boolean success = true;// 是否成功
  private String errorCode = "-1";//错误代码
  private String msg = "操作成功";// 提示信息
    private Long count;             //返回表格记录数量
    private List<?> data;           //返回表格数据
  private LinkedHashMap<String, Object> body = new LinkedHashMap<String, Object>();//封装json的map
 
  public static AjaxJson ok(){
    AjaxJson j = new AjaxJson();
    return j;
  }
 
  public static AjaxJson ok(String msg){
    AjaxJson j = new AjaxJson();
    j.setMsg(msg);
    return j;
  }
 
  public static AjaxJson ok(String msg, Object object){
    AjaxJson j = new AjaxJson();
    j.setMsg(msg);
    j.setResult(object);
    return j;
  }
 
  public static AjaxJson ok(Object object){
    AjaxJson j = new AjaxJson();
    j.setResult(object);
    return j;
  }
 
  public static AjaxJson fail(String errorMsg){
    AjaxJson j = new AjaxJson();
    j.setSuccess(false);
    j.setErrorCode("999");//默认错误码
    j.setMsg(errorMsg);
    return j;
  }
 
  public static AjaxJson fail(String errorCode,String errorMsg){
    AjaxJson j = new AjaxJson();
    j.setSuccess(false);
    j.setErrorCode(errorCode);
    j.setMsg(errorMsg);
    return j;
  }
  //返回不分页的layui表数据
    public static AjaxJson layuiTable(List<?> list){
        AjaxJson j = new AjaxJson();
        j.setSuccess(true);
        j.setCount(Long.valueOf(list.size()));
        j.setData(list);
        return j;
    }
  public LinkedHashMap<String, Object> getBody() {
    return body;
  }
 
  public void setBody(LinkedHashMap<String, Object> body) {
    this.body = body;
  }
 
  public void put(String key, Object value){//向json中添加属性,在js中访问,请调用data.map.key
    body.put(key, value);
  }
  
  public void remove(String key){
    body.remove(key);
  }
 
  /**
   * 直接设置result内容
   * @param result
   */
  public void setResult(Object result){
    body.put("result", result);
  }
  @JsonIgnore//返回对象时忽略此属性
  public Object getResult(){
    return body.get("result");
  }
 
  public String getMsg() {
    return msg;
  }
 
  public void setMsg(String msg) {//向json中添加属性,在js中访问,请调用data.msg
    this.msg = msg;
  }
 
 
  public boolean isSuccess() {
    return success;
  }
 
  public void setSuccess(boolean success) {
    this.success = success;
  }
  
 
  public void setErrorCode(String errorCode) {
    this.errorCode = errorCode;
  }
 
  public String getErrorCode() {
    return errorCode;
  }
 
    public Long getCount() {
        return count;
    }
 
    public void setCount(Long count) {
        this.count = count;
    }
 
    public List<?> getData() {
        return data;
    }
 
    public void setData(List<?> data) {
        this.data = data;
    }
}

编写后端接口

import com.example.demo.mapper.RegionMapper;
import com.example.demo.model.Region;
import com.example.demo.util.AjaxJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
 
 
@RestController
@RequestMapping("/admin")
public class AWController {
    @Autowired
    RegionMapper regionMapper;
    @GetMapping("/provincelist")
 
    public AjaxJson provincelist() {
        List<Region> listProvince = regionMapper.findListProvince();
        return AjaxJson.ok(listProvince);
    }
    @GetMapping("/provinceCitylist")
    public AjaxJson provinceCitylist(Long parent_id) {
 
        List<Region> listCity = regionMapper.findListCity(parent_id);
        return AjaxJson.ok(listCity);
    }
    @GetMapping("/cityCountylist")
 
    public AjaxJson cityCountylist(Long parent_id) {
        List<Region> listCounty = regionMapper.findListCounty(parent_id);
        return AjaxJson.ok(listCounty);
    }
}

 生成接口

查询省份

查询市,传入省份ID,示例查询辽宁省

查询县,传入市ID,示例查询鞍山市

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
23小时前
|
SQL 存储 关系型数据库
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server
|
1天前
|
关系型数据库 MySQL 数据库
深入OceanBase分布式数据库:MySQL 模式下的 SQL 基本操作
深入OceanBase分布式数据库:MySQL 模式下的 SQL 基本操作
|
1天前
|
SQL 关系型数据库 MySQL
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version
|
1天前
|
存储 安全 Java
基于Java+MySQL停车场车位管理系统详细设计和实现(源码+LW+调试文档+讲解等)
基于Java+MySQL停车场车位管理系统详细设计和实现(源码+LW+调试文档+讲解等)
|
1天前
|
SQL 存储 关系型数据库
技术笔记:MYSQL常用基本SQL语句总结
技术笔记:MYSQL常用基本SQL语句总结
|
2天前
|
SQL 存储 关系型数据库
Mysql-事务-锁-索引-sql优化-隔离级别
Mysql-事务-锁-索引-sql优化-隔离级别
|
3天前
|
关系型数据库 Java MySQL
Java关于Mysql数据库的事物处理类
Java关于Mysql数据库的事物处理类
|
1天前
|
SQL 运维 关系型数据库
|
1天前
|
存储 关系型数据库 MySQL
|
1天前
|
存储 关系型数据库 MySQL