在最近的工作中,遇到一个问题,个人所做的简历模块中有两个字段,分别是个人信息中的户口所在地和现居住城市。
前端界面中这两个选项框是用到了二级和三级联动,在向后端传输时希望可以通过数组类型进行传输,例如【000000,000001】
前端页面展示如下:
后台数据库实体类字段设计如下:
registered_residence 户口所在地 varchar(50)
current_city 现居住城市 varchar(50)
简历实体字段设计如下:
//户口所在地
private String registeredResidence;
//现居住城市
private String currentCity;
当选择完户口所在地一栏的省外,就会联动出现省下面对应的市,产生联动效果
在协商传输类型时产生疑问:
1.后端数据库中所定义的这两个类型为String类型
2.前台要求后端存入数组类型
拟定解决方案
一:在Java实体类中创建VO类来新建数据类型为数组类型,此变量用来接收数组,如下:
新增简历个人信息时,前端传入户口所在地和现居住城市,后端需按数组类型接收
否则会报错(数据类型不匹配)
在新增简历个人信息的时候需要按照数组接收,存入数据库后按照字符串存储
所以需要循环拆分,之后在拼接成字符串,然后再存入字符串
/**
* 更新简历个人信息(新增&&修改)
*/
@PutMapping("/updateBrief")
public ResponseEntity updateBrief(@RequestBody BriefVo briefVo){
System.out.println("briefVo:" + briefVo);
Brief brief = new Brief();
BeanUtils.copyProperties(briefVo,brief);
StringBuilder registeredResidence = new StringBuilder();
if(briefVo.getCurrentCitys().length > 0){
for(int i = 0;i<briefVo.getRegisteredResidences().length;i++){
registeredResidence.append(briefVo.getRegisteredResidences()[i]);
if(briefVo.getRegisteredResidences().length - i > 1){
registeredResidence.append("-");
} }
}
String registeredResidence1 = registeredResidence.toString();
brief.setRegisteredResidence(registeredResidence1); StringBuilder currentCity = new StringBuilder();
if(briefVo.getCurrentCitys().length > 0){
for(int i = 0;i<briefVo.getCurrentCitys().length;i++){
currentCity.append(briefVo.getCurrentCitys()[i]);
if(briefVo.getCurrentCitys().length- i > 1){
currentCity.append("-");
}
}
}
String currentCity1 = currentCity.toString();
brief.setCurrentCity(currentCity1); String result = briefService.updateBrief(brief);
return ResponseEntity.ok(result);
}
=====================================================
在存入数据库后,数据库中的字段类型为String
再次查看的时候,还需要把字符串类型的数据变为数组传输到
前端进行交互,否则也会报新增时数据类型不匹配的错误
/**
* 查看简历所有信息(点击查看按钮时显示)
*/
@GetMapping("/findAllCus")
public BriefVo find(Brief brief) {
String briefId = brief.getBriefId();
List<BriefEduExperience> eduExperienceList = eduExperienceService.findEduExperienceByBriefId(briefId);
List<BriefWorkExperience> workExperienceList = workExperienceService.findWorkExpericeByBriefId(briefId);
List<BriefProjectExperience> projectExperienceList = projectExperienceService.findProjectExperienceByBriefId(briefId);
List<BriefTrainExperience> trainExperienceList = trainExperienceService.findTrainExpericeByBriefId(briefId);
List<BriefProSkill> proSkillList = proSkillService.findProSkillByBriefId(briefId);
BriefVo briefVo = new BriefVo();
BeanUtils.copyProperties(brief, briefVo);
briefVo.setEduExperienceList(eduExperienceList);
briefVo.setWorkExperienceList(workExperienceList);
briefVo.setProjectExperienceList(projectExperienceList);
briefVo.setTrainExperienceList(trainExperienceList);
briefVo.setProSkillList(proSkillList);
String registeredResidence = brief.getRegisteredResidence();
String[] registeredResidences = registeredResidence.split("-");
String currentCity = brief.getCurrentCity();
String[] currentCitys = currentCity.split("-");
briefVo.setCurrentCitys(currentCitys);
briefVo.setRegisteredResidences(registeredResidences);
return briefVo;
}
=====================================
在完成这步操作之后,又出现了问题,具体问题记不得太清了,小白请包涵
综上,一系列操作之后发现,后端实现起来比较复杂,需要多次循环,
所以为提高效率,决定让前端来传字符串,为接收存储都方便,
如有更好的解决方案,请指教!!!!!!
希望看到这篇博客的博友可以提供解决方案,欢迎交流分享!!!!!!