问题:
最近,项目中发现了一个问题,一个返回分页视图的列表。(该视图也是实体类),返回数据需要对某个属性进行修改,首先根据条件查出符合条件的所有数据,其次遍历这些数据,对某个属性进行修改(并没有保存)。最后发现,该视图对应的数据表字段内容,也被修改。
public PageDataResponse<PropertyCheck> queryPropertyCheck(
@RequestParam(required = false, defaultValue = "1") Integer currentPage,
@RequestParam(required = false, defaultValue = "10") Integer pageSize,
@RequestParam(required = false) String name,
@RequestParam(required = false) Long beginTime,
@RequestParam(required = false) Long endTime
) {
currentPage = currentPage > 0 ? currentPage - 1 : currentPage;
Page<PropertyCheck> page = propertyService
.queryPropertyCheck(name, beginTime, endTime, currentPage, pageSize);
List<PropertyCheck> propertyChecks = page.getContent();
propertyService.updatePropertyCheckStatus(propertyChecks);
return mgrAssembler.toPageDataResponse(page);
}
其中,updatePropertyCheckStatus方法:
public void updatePropertyCheckStatus(List<PropertyCheck> propertyChecks) {
propertyChecks.forEach(propertyCheck -> {
propertyCheck.setDepartmentName(
departmentRegionNodeService.findNameById(propertyCheck.getDepartmentRegionId()));
List<String> regionIdListByParentId = departmentRegionNodeService.findDepartmentRegionIdListByParentId(propertyCheck.getDepartmentRegionId());
Set<String> usernameSet = staffService.getUsernameListByRegion(regionIdListByParentId);
Set<String> set = getAllPropertyIdByUsername(usernameSet);
int count = propertyCheckDeatilRepository.countByPropertyCheckId(propertyCheck.getId());
if (count >= set.size() ) {
propertyCheck.setCreator("java入门到放弃");
propertyCheck.setStatus(PropertyCheck.FINISHED); // 2
}
});
}
数据表:
未调用方法之前数据:
调用之后:
结论:
如果想返回一个对象,需要修改其中的内容,并且不修改数据表内容,可以新建一个对象来替代视图。
ps:
纯属个人记录。仅作参考。