1.实体类举例(市区同理)
import com.baomidou.mybatisplus.annotation.*;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.data.annotation.CreatedDate;
import java.io.Serializable;
import java.util.Date;
/**
* 省份名称表
*
* @author Natalia_Portman
*/
@Data
@TableName(value = "t_act_province")
public class Province implements Serializable {
@TableId(type = IdType.ASSIGN_UUID,value = "ID")
private String id;
@TableField(value = "CODE")
@ApiModelProperty(value = "省份编码")
private String code;
@TableField(value = "NAME")
@ApiModelProperty(value = "省份名称")
private String name;
@TableField(value = "DELETE_FLAG", fill = FieldFill.INSERT)
@ApiModelProperty(value = "删除状态 1-可用 2-删除")
private String deleteFlag;
@TableField(value = "CREATE_USER_ID", fill = FieldFill.INSERT)
@ApiModelProperty(value = "创建人id")
private String createUserId;
@CreatedDate
@TableField(value = "CREATE_TIME", fill = FieldFill.INSERT)
@ApiModelProperty(value = "创建时间")
private Date createTime;
@TableField(value = "UPDATE_USER_ID", fill = FieldFill.UPDATE)
@ApiModelProperty(value = "修改人id")
private String updateUserId;
@TableField(value = "UPDATE_TIME", fill = FieldFill.UPDATE)
@ApiModelProperty(value = "修改时间")
private Date updateTime;
}
2.mapper举例(可加注释@Component,因项目里配置过,所以我这里没加。市区同理)
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.yss.accountmanager.entity.Province;
public interface ProvinceMapper extends BaseMapper<Province> {
}
3.server举例
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.yss.accountmanager.entity.AttachmentDO;
import com.yss.accountmanager.entity.City;
import com.yss.accountmanager.entity.Province;
import com.yss.accountmanager.entity.Town;
import com.yss.accountmanager.mapper.CityMapper;
import com.yss.accountmanager.mapper.ProvinceMapper;
import com.yss.accountmanager.mapper.TownMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.security.PublicKey;
import java.util.Date;
import java.util.List;
/**
* @author Natalia_Portman
*/
@Service
public class CityService {
@Autowired
private CityMapper cityMapper;
@Autowired
private TownMapper townMapper;
@Autowired
private ProvinceMapper provinceMapper;
/**
* 查询省份列表
* @return
*/
public List<Province> queryProvinceList(){
return new LambdaQueryChainWrapper<>(provinceMapper).eq(Province::getDeleteFlag,1).list();
}
/**
* 根据省份code查询城市列表
* @param provinceCode
* @return
*/
public List<City> queryCityList(String provinceCode) {
return new LambdaQueryChainWrapper<>(cityMapper).eq(City::getProvinceCode,provinceCode).list();
}
/**
* 根据城市code查询区县列表
* @param cityCode
* @return
*/
public List<Town> queryTownList(String cityCode){
return new LambdaQueryChainWrapper<>(townMapper).eq(Town::getCityCode,cityCode).list();
}
/**
* 添加省份列表
* @param province
* @return
*/
public int addProvince(Province province){
return provinceMapper.insert(province);
}
/**
* 添加城市列表
* @param city
* @return
*/
public int addCity(City city){
return cityMapper.insert(city);
}
/**
* 添加区县列表
* @param town
* @return
*/
public int addTown(Town town){
town.setCreateTime(new Date());
return townMapper.insert(town);
}
/**
* 修改省份
* @param id
* @param province
* @return
*/
public int updateProvinceById(String id,Province province){
LambdaUpdateWrapper<Province> updateWrapper =new LambdaUpdateWrapper<>();
updateWrapper.eq(Province::getId,id);
return provinceMapper.update(province,updateWrapper);
}
public void updateProvinceById(Province province) {
UpdateWrapper<Province> updateWrapper =new UpdateWrapper<>();
if (StringUtils.isNotBlank(province.getId())) {
updateWrapper.set("ID",province.getId());
}
if (StringUtils.isNotBlank(province.getName())) {
updateWrapper.set("NAME",province.getName());
}
if (StringUtils.isNotBlank(province.getCode())) {
updateWrapper.set("CODE",province.getCode());
}
provinceMapper.update(new Province(),updateWrapper);
}
public void updateCityById(City city) {
UpdateWrapper<City> updateWrapper =new UpdateWrapper<>();
if (StringUtils.isNotBlank(city.getId())) {
updateWrapper.set("ID",city.getId());
}
if (StringUtils.isNotBlank(city.getCode())) {
updateWrapper.set("CODE",city.getCode());
}
if (StringUtils.isNotBlank(city.getName())) {
updateWrapper.set("NAME",city.getName());
}
if (null != city.getCreateTime()) {
updateWrapper.set("CREATE_TIME",city.getCreateTime());
}
cityMapper.update(new City(),updateWrapper);
}
public void updateTownById(Town town) {
UpdateWrapper<Town> updateWrapper =new UpdateWrapper<>();
if(StringUtils.isNotBlank(town.getId())) {
updateWrapper.set("ID",town.getId());
}
if (StringUtils.isNotBlank(town.getCode())) {
updateWrapper.set("CODE",town.getCode());
}
if (StringUtils.isNotBlank(town.getName())) {
updateWrapper.set("NAME",town.getName());
}
if (null != town.getCreateTime()) {
updateWrapper.set("CREATE_TIME",town.getCreateTime());
}
townMapper.update(new Town(),updateWrapper);
}
/**
* 根据用户id删除省份
* @param id
* @return
*/
public int deleteProvinceById(String id){
LambdaUpdateWrapper<Province> updateWrapper = new LambdaUpdateWrapper<>();
updateWrapper.set(Province::getDeleteFlag,"2").eq(Province::getId,id);
return this.provinceMapper.update(new Province(),updateWrapper);
}
/**
* 根据用户id删除
* @param id
* @return
*/
public int deleteCityById(String id){
LambdaUpdateWrapper<City> updateWrapper =new LambdaUpdateWrapper<>();
updateWrapper.set(City::getDeleteFlag,"2").eq(City::getId,id);
return this.cityMapper.update(new City(),updateWrapper);
}
/**
* 根据用户id删除区县列表数据
* @param id
* @return
*/
public int deleteTownById(String id){
LambdaUpdateWrapper<Town> updateWrapper =new LambdaUpdateWrapper<>();
updateWrapper.set(Town::getDeleteFlag,"2").eq(Town::getId,id);
return this.townMapper.update(new Town(),updateWrapper);
}
}
4.controller 举例
import com.yss.accountmanager.entity.City;
import com.yss.accountmanager.entity.Province;
import com.yss.accountmanager.entity.Town;
import com.yss.accountmanager.service.CityService;
import com.yss.base.common.result.ObjectRestResponse;
import io.swagger.annotations.Api;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
*@author Natalia_Portman
*/
@Api(tags = "省市区县")
@RestController
public class CityController{
@Autowired
private CityService cityService;
/**
* 查询省份列表
* @return
*/
@GetMapping(value = "/province")
public ObjectRestResponse<List<Province>> queryProvinceList(){
return new ObjectRestResponse<List<Province>>()
.rel(true)
.data(cityService.queryProvinceList());
}
/**
* 根据ProvinceCode查询城市列表
* @param provinceCode
* @return
*/
@GetMapping(value = "/city")
public ObjectRestResponse<List<City>> queryCityList(@RequestParam String provinceCode){
return new ObjectRestResponse<List<City>>()
.rel(true)
.data(cityService.queryCityList(provinceCode));
}
/**
* 根据CityCode查询区县列表
* @param cityCode
* @return
*/
@GetMapping(value = "/town")
public ObjectRestResponse<List<Town>> queryTownList(@RequestParam String cityCode){
return new ObjectRestResponse<List<Town>>()
.rel(true)
.data(cityService.queryTownList(cityCode));
}
/**
* 添加省列表
* @param province
* @return
*/
@PostMapping(value = "/addProvince")
public ObjectRestResponse<Integer> addProvinceList(@RequestBody Province province){
return new ObjectRestResponse<Integer>()
.rel(true)
.data(cityService.addProvince(province));
}
/**
* 添加城市列表
* @param city
* @return
*/
@PostMapping(value = "/addCity")
public ObjectRestResponse<Integer> addCityList(@RequestBody City city){
return new ObjectRestResponse<Integer>()
.rel(true)
.data(cityService.addCity(city));
}
/**
* 添加区县列表
* @param town
* @return
*/
@PostMapping(value = "/addTown")
public ObjectRestResponse<Integer> addTownList(@RequestBody Town town){
return new ObjectRestResponse<Integer>()
.rel(true)
.data(cityService.addTown(town));
}
/**
* 根据id删除省份列表
* @param id
* @return
*/
@PostMapping(value = "/deleteProvinceById")
public ObjectRestResponse<Integer> deleteProvinceById(@RequestParam("id") String id){
return new ObjectRestResponse<Integer>()
.rel(true)
.data(cityService.deleteProvinceById(id));
}
/**
* 根据id删除城市列表
* @param id
* @return
*/
@PostMapping(value = "/deleteCityById")
public ObjectRestResponse<Integer> deleteCityById(@RequestParam("id") String id){
return new ObjectRestResponse<Integer>()
.rel(true)
.data(cityService.deleteCityById(id));
}
/**
* 根据id删除区县列表
* @param id
* @return
*/
public ObjectRestResponse<Integer> deleteTownById(@RequestParam("id") String id){
return new ObjectRestResponse<Integer>()
.rel(true)
.data(cityService.deleteTownById(id));
}
}
6 entry举例
import lombok.Data;
import java.util.List;
@Data
public class Entry {
private String code;
private String name;
private List<Entry> children;
}
7 test举例
import com.alibaba.fastjson.JSONObject;
import com.yss.accountmanager.entity.City;
import com.yss.accountmanager.entity.Entry;
import com.yss.accountmanager.entity.Province;
import com.yss.accountmanager.entity.Town;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.io.*;
import java.util.List;
@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest( webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class Sql {
@Autowired
CityService cityService;
@Test
public void test1() throws IOException {
FileInputStream inputStream = new FileInputStream("D:\\data.json");
InputStreamReader reader = new InputStreamReader(inputStream, "UTF-8");
int r;
StringBuilder sb = new StringBuilder();
while ((r = reader.read()) != -1) {
sb.append((char) r);
}
List<Entry> list = JSONObject.parseArray(sb.toString(), Entry.class);
for (Entry entry : list) {
//省 入库
Province province = new Province();
province.setCode(entry.getCode());
province.setName(entry.getName());
cityService.addProvince(province);
//市
List<Entry> children = entry.getChildren();
for (Entry child : children) {
//市
City city = new City();
city.setCode(child.getCode());
city.setName(child.getName());
city.setProvinceCode(province.getCode());
cityService.addCity(city);
//县或区
List<Entry> children1 = child.getChildren();
for (Entry entry1 : children1) {
Town town =new Town();
town.setCode(entry1.getCode());
town.setName(entry1.getName());
town.setCityCode(city.getCode());
cityService.addTown(town);
}
}
}
System.out.println();
}
}