背景:
前端:入参为字符串和数组类型;通过json字符串传给后台,
后台:后台通过工具解析为Map<String,Object>,然后需要解析出Map里面的数组值做操作;
需求:
入参:
{
“addOrModify”: “add”,
“hrBdName”: “陕西省”,
“hrBdCode”: “1001”,
“hrBuName”: [“宝鸡市”,“咸阳市”,“汉中市”,“渭南市”],
“hrBuCode”: [“100101”,“100102”,“100103”,“100104”]
}
需转化为:List<Map<String,String>>,入库保存;
[{hrBdName=陕西省,hrBdCode=1001,hrBuName=宝鸡市,hrBuCode=100101},
{hrBdName=陕西省,hrBdCode=1001,hrBuName=咸阳市,hrBuCode=100102},
{hrBdName=陕西省,hrBdCode=1001,hrBuName=汉中市,hrBuCode=100103},
{hrBdName=陕西省,hrBdCode=1001,hrBuName=渭南市,hrBuCode=100104}]
Postman入参:
控制层:
转化为Map<String,Object>
public Result addOrModifyConfig(@RequestBody String param) {
try {
Map<String, Object> paramMap = JsonUtils.readValue(param, Map.class);
xxxListService.addOrModifyConfig(paramMap);
} catch (Exception e) {
log.error("XxxListController addOrModifyConfig is error===:" + e.getMessage(), e);
return Result.failure("更新失败");
}
return Result.success().result(null);
}
业务层:
业务逻辑:将入参处理为:List<Map<String,String>>格式:
public Result addOrModifyConfig(Map<String, Object> paramMap) {
String addOrModify = String.valueOf(paramMap.get("addOrModify"));
if(addOrModify.equals("add")){
paramMap.remove("addOrModify");
List<Map<String,String>> list = new ArrayList<>();
String hrBdName = String.valueOf(paramMap.get("hrBdName"));
String hrBdCode = String.valueOf(paramMap.get("hrBdCode"));
// hrBuName,hrBuCode 前台传数组,经过工具转为Map<String,Object>,
// 因为数据比较简单,所以强转为字符串,去除符号[],切割为数组;
String[] hrBuNameArr = String.valueOf(paramMap.get("hrBuName")).
replace("[","").replace("]","").split(",");
String[] hrBuCodeArr = String.valueOf(paramMap.get("hrBuCode")).
replace("[","").replace("]","").split(",");
if(hrBuNameArr.length == hrBuCodeArr.length && hrBuNameArr.length > 0){
for (int j = 0; j < hrBuNameArr.length; j++) {
Map<String,String> map = new HashMap<>();
map.put("bdname",hrBdName);
map.put("bdno",hrBdCode);
map.put("buname",hrBuNameArr[j]);
map.put("buno",hrBuCodeArr[j]);
list.add(map);
}
}
// 2.查询本数据库数据;
List<Map<String,String>> magList = xxxListMapper.selectDepartAll();
Iterator<Map<String, String>> iterator = list.iterator();
while (iterator.hasNext()){
Map<String, String> next = iterator.next();
String bdname = String.valueOf(next.get("bdname"));
String bdno = String.valueOf(next.get("bdno"));
String buname = String.valueOf(next.get("buname"));
String buno = String.valueOf(next.get("buno"));
next.put("isEnable","1");
next.put("dicType","icanDepart");
Iterator<Map<String, String>> iterator2 = magList.iterator();
while (iterator2.hasNext()){
Map<String, String> next2 = iterator2.next();
String bdname2 = String.valueOf(next2.get("bdname"));
String bdno2 = String.valueOf(next2.get("bdno"));
String buname2 = String.valueOf(next2.get("buname"));
String buno2 = String.valueOf(next2.get("buno"));
if(bdname.equals(bdname2) && bdno.equals(bdno2) && buname.equals(buname2) && buno.equals(buno2)){
iterator.remove();
}
}
}
if(list.size() > 0){
xxxListMapper.addOrModifyConfig(list);
}
}
持久层:数据入库代码
<select id="selectDepartAll" resultType="map">
SELECT *
FROM xxx_department_list_dic where 1 = 1
</select>
<!-- 入库保存 -->
<insert id="addOrModifyConfig" parameterType="java.util.List">
<foreach collection="list" item="res" index="index" separator=";">
INSERT IGNORE INTO xxx_department_list_dic
<foreach collection="res.entrySet()" index="key" separator="," open="(" close=")">
${key}
</foreach>
VALUES
<foreach collection="res.entrySet()" item="value" separator="," open="(" close=")">
#{value}
</foreach>
</foreach>
</insert>
最终目的: