后端技术: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,示例查询鞍山市