1.返回Map集合到前台:
@GetMapping("/role_change/userList")
@ResponseBody
public Object userList(UserPrinciple userPrinciple) {
List<Map<String, Object>> results = new ArrayList<>();
Map<String, Object> map = new HashMap<>();
map.put("adminState", "1");
map.put("userList", reportService.cutOverUsers(userPrinciple.getUser().getRoleId()));//超管界面
results.add(map);
return results;
}
2.List嵌套Map集合到前台:
2.1:service
public List<HashMap<Object, Object>> updateGroupItems(List<OrganizationGroup> organizationGroup) {
//存储返回
List<HashMap<Object, Object>> list = new ArrayList<HashMap<Object, Object>>();
organizationGroup.forEach(q -> {
HashMap<Object, Object> maps = new HashMap<Object, Object>();
//存map返回 id 和对应idkey
maps.put("id", q.getIdKey());
maps.put("organizationId", q.getIdKey());
maps.put("idKey", q.getIdKey());
list.add(maps);
// 利用list中的元素创建HashSet集合,此时set中进行了去重操作
HashSet set = new HashSet(list);
// 清空list集合
list.clear();
// 将去重后的元素重新添加到list中
list.addAll(set);
});
return list;
}
2.2:controller层
@RequestMapping("/groupItemChange")
@CustomerVisitHistory
public Object groupItemChange(@RequestBody List<OrganizationGroup> organizationGroup, HttpServletRequest req) {
Map maps = new HashMap<String, String>();
if (organizationGroup != null) {
Map<String, String> map = JWTUtil.verificationToken(req.getHeader("token"));
if (StringUtils.containsIgnoreCase(map.get("message"), "success")) {
//接收参数机构数据 进行修改
List<HashMap<Object, Object>> hashMaps = organizationUserService.updateGroupItem(organizationGroup);
if (hashMaps != null) {
maps.put("return", hashMaps);
maps.put("Message", "Succeed");
} else {
maps.put("return", hashMaps);
}
} else {
maps.put("NoLogin", "请输入正确Token");
}
}
return maps;
}
2.3 响应示例
3.Map互相调用:
控制层:
public String userUserAgencyDiscount() {
var loginUser = getLoginUser();
var req = readJson();
var discountState = (Integer) req.get("discountState");
var result = new HashMap<String, Object>();
result.put("success", 0);
Map map = cnUserService.editUserDiscountState(discountState);
Integer succeed = (Integer) map.get("succeed");
String error = (String) map.get("error");
if (succeed > 0) {
result.put("success", 1);
} else {
result.put("error", error);
}
writeJson(result);
return null;
}
Service:
Map editUserDiscountState(Integer discountState);
Serviceimpl:
@Override
public Map editUserDiscountState(Integer discountState) {
var loginUser = getLoginUser();
var result = new HashMap<String, Object>();
result.put("succeed", 0);
if (discountState != null) {
//存在编辑
int i = userMapper.editUserDiscountState(discountState, loginUser.getId());
if (i > 0) {
result.put("succeed", 1);
userMapper.insertDiscountStatusRecords(new CnUserDiscountStatusRecords(loginUser.getId(), LocalDateTime.now(), "编辑"));//操作日志
} else {
result.put("error", "数据错误,修改失败!");
}
} else {
result.put("error", "数据错误,请联系管理员!");
}
return result;
}