将行政区划Json写入数据库java代码

1.下图为导出的地图Json对象

2.将json写入数据库

(1)建表

CREATE TABLE `mshx`.`Untitled`  (
  `area_code` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '行政区划代码',
  `area_name` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '行政区划名称',
  `area_level` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '行政区划等级(province:省,city:市,district:区,street:街道)',
  `parent_code` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL COMMENT '上级行政区划代码',
  PRIMARY KEY (`area_code`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '区域' ROW_FORMAT = DYNAMIC;

(2)Java代码(街道和镇处于同一级,区和县处于同一级)

mapper:

<?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="**.**.mapper.AreaMapper">
    
    <resultMap type="Area" id="AreaResult">
        <result property="areaCode"    column="area_code"    />
        <result property="areaName"    column="area_name"    />
        <result property="areaLevel"    column="area_level"    />
        <result property="parentCode"    column="parent_code"    />
    </resultMap>

    <sql id="selectAreaVo">
        select area_code, area_name, area_level, parent_code from area
    </sql>

    <select id="selectAreaList" parameterType="Area" resultMap="AreaResult">
        <include refid="selectAreaVo"/>
        <where>  
            <if test="areaName != null  and areaName != ''"> and area_name = #{areaName}</if>
            <if test="areaLevel != null  and areaLevel != ''"> and area_level = #{areaLevel}</if>
            <if test="parentCode != null  and parentCode != ''"> and parent_code = #{parentCode}</if>
            <if test="areaCode != null  and areaCode != ''"> and locate(#{areaCode}, area_code ) > 0</if>
        </where>
    </select>

    <select id="selectAreaListByParentCode" parameterType="String" resultMap="AreaResult">
        <include refid="selectAreaVo"/>
        where parent_code = #{parentCode}
    </select>
    
    <select id="selectAreaByAreaCode" parameterType="String" resultMap="AreaResult">
        <include refid="selectAreaVo"/>
        where area_code = #{areaCode}
    </select>
        
    <insert id="insertArea" parameterType="Area">
        insert into area
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="areaCode != null">area_code,</if>
            <if test="areaName != null and areaName != ''">area_name,</if>
            <if test="areaLevel != null and areaLevel != ''">area_level,</if>
            <if test="parentCode != null and parentCode != ''">parent_code,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="areaCode != null">#{areaCode},</if>
            <if test="areaName != null and areaName != ''">#{areaName},</if>
            <if test="areaLevel != null and areaLevel != ''">#{areaLevel},</if>
            <if test="parentCode != null and parentCode != ''">#{parentCode},</if>
         </trim>
    </insert>

    <update id="updateArea" parameterType="Area">
        update area
        <trim prefix="SET" suffixOverrides=",">
            <if test="areaName != null and areaName != ''">area_name = #{areaName},</if>
            <if test="areaLevel != null and areaLevel != ''">area_level = #{areaLevel},</if>
            <if test="parentCode != null and parentCode != ''">parent_code = #{parentCode},</if>
        </trim>
        where area_code = #{areaCode}
    </update>

    <delete id="deleteAreaByAreaCode" parameterType="String">
        delete from area where area_code = #{areaCode}
    </delete>

    <delete id="deleteAreaByAreaCodes" parameterType="String">
        delete from area where area_code in 
        <foreach item="areaCode" collection="array" open="(" separator="," close=")">
            #{areaCode}
        </foreach>
    </delete>
</mapper>

domain:

package com.yc.entity;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import net.jlrnt.support.common.nodeanalysis.NodeBean;

import java.io.Serializable;
import java.util.Collection;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@JsonIgnoreProperties(value = {"id", "parent", "children", "parentId"})
public class Area extends NodeBean implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 行政区划代码
     */
    private String areaCode;

    /**
     * 行政区划名称
     */
    private String areaName;

    /**
     * 行政区划等级(province:省,city:市,district:区,street:街道)
     */
    private String areaLevel;

    /**
     * 父级行政区划代码
     */
    private String parentCode;

    @Override
    public String getId() {
        return areaCode;
    }

    @Override
    public String getParentId() {
        return parentCode;
    }

    public Collection<?> getDistricts() {
        return getChildren();
    }

}

mapper:

package com.yc.mapper;

import com.yc.entity.Area;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;


@Mapper
public interface AreaMapper 
{
    /**
     * 查询区域
     * 
     * @param areaCode 区域主键
     * @return 区域
     */
    public Area selectAreaByAreaCode(String areaCode);

    /**
     * 查询区域列表
     * 
     * @param area 区域
     * @return 区域集合
     */
    public List<Area> selectAreaList(Area area);

    /**
     * 查询区域列表
     *
     * @param parentCode 父级行政区划代码
     * @return 区域集合
     */
    public List<Area> selectAreaListByParentCode(String parentCode);

    /**
     * 新增区域
     * 
     * @param area 区域
     * @return 结果
     */
    public int insertArea(Area area);

    /**
     * 修改区域
     * 
     * @param area 区域
     * @return 结果
     */
    public int updateArea(Area area);

    /**
     * 删除区域
     * 
     * @param areaCode 区域主键
     * @return 结果
     */
    public int deleteAreaByAreaCode(String areaCode);

    /**
     * 批量删除区域
     * 
     * @param areaCodes 需要删除的数据主键集合
     * @return 结果
     */
    public int deleteAreaByAreaCodes(String[] areaCodes);
}

service:

package com.yc.service;

import com.yc.entity.Area;

import java.util.List;
import java.util.Map;

public interface IAreaService {

    /**
     * 查询区域
     *
     * @param areaCode 区域主键
     * @return 区域
     */
    public Area selectAreaByAreaCode(String areaCode);

    /**
     * 查询区域列表
     *
     * @param area 区域
     * @return 区域集合
     */
    public List<Area> selectAreaList(Area area);

    /**
     * 查询区域列表
     *
     * @param parentCode 父级行政区划代码
     * @return 区域集合
     */
    public List<Area> selectAreaListByParentCode(String parentCode);

    /**
     * 新增区域
     *
     * @param area 区域
     * @return 结果
     */
    public int insertArea(Area area);

    /**
     * 修改区域
     *
     * @param area 区域
     * @return 结果
     */
    public int updateArea(Area area);

    /**
     * 批量删除区域
     *
     * @param areaCodes 需要删除的区域主键集合
     * @return 结果 0-正常,1-有下级行政区划
     */
    public String deleteAreaByAreaCodes(String[] areaCodes);

    /**
     * 删除区域信息
     *
     * @param areaCode 区域主键
     * @return 结果 0-正常,1-有下级行政区划
     */
    public String deleteAreaByAreaCode(String areaCode);

//    /**
//     * 导入JSON,写入省/市/区/街道的信息
//     * @param map
//     */
//    public void importAreaData(Map<String, Object> map);

    /**
     * 查询区域树
     * @return
     */
    public List<Area> selectAreaTree();
}

impl:

package com.yc.service;

import com.yc.entity.Area;

import java.util.List;
import java.util.Map;


public interface IAreaService {

    /**
     * 查询区域
     *
     * @param areaCode 区域主键
     * @return 区域
     */
    public Area selectAreaByAreaCode(String areaCode);

    /**
     * 查询区域列表
     *
     * @param area 区域
     * @return 区域集合
     */
    public List<Area> selectAreaList(Area area);

    /**
     * 查询区域列表
     *
     * @param parentCode 父级行政区划代码
     * @return 区域集合
     */
    public List<Area> selectAreaListByParentCode(String parentCode);

    /**
     * 新增区域
     *
     * @param area 区域
     * @return 结果
     */
    public int insertArea(Area area);

    /**
     * 修改区域
     *
     * @param area 区域
     * @return 结果
     */
    public int updateArea(Area area);

    /**
     * 批量删除区域
     *
     * @param areaCodes 需要删除的区域主键集合
     * @return 结果 0-正常,1-有下级行政区划
     */
    public String deleteAreaByAreaCodes(String[] areaCodes);

    /**
     * 删除区域信息
     *
     * @param areaCode 区域主键
     * @return 结果 0-正常,1-有下级行政区划
     */
    public String deleteAreaByAreaCode(String areaCode);

    /**
     * 导入JSON,写入省/市/区/街道的信息
     * @param map
     */
    public void importAreaData(Map<String, Object> map);

    /**
     * 查询区域树
     * @return
     */
    public List<Area> selectAreaTree();
}

controller:

package com.yc.controller;

import java.util.List;
import java.util.Map;

import com.yc.common.core.controller.BaseController;
import com.yc.entity.Area;
import com.yc.service.IAreaService;
import net.jlrnt.platform.uur.service.nativesource.NativeIgnoreIdentityResource;
import net.jlrnt.platform.web.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/area")
@NativeIgnoreIdentityResource
public class AreaController extends BaseController {

    @Autowired
    private IAreaService areaService;

    /**
     * 查询行政区划树
     */
    @GetMapping("/tree")
    public Result<?> tree() {
        List<Area> list = areaService.selectAreaTree();
        return Result.build("0", list);
    }

    /**
     * 导入JSON,写入省/市/区/街道的信息
     *
     * @param param
     * @return
     */
    @NativeIgnoreIdentityResource
    @PostMapping("/importAreaData")
    public Result<?> importAreaData(@RequestBody Map<String, Object> param) {
        try {
            areaService.importAreaData(param);
            return Result.build("新增成功!");
        } catch (Exception e) {
            log.error(e.getMessage());
            return Result.build(1, Constants.RUNTIME_EXCEPTION_MESSAGE);
        }
    }

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值