对数据库 insert 或 update 后会返回受影响的行数,那么这个行数需要返回给前端吗?
public int insert(OrgDTO orgDTO) {
OrgEntity orgEntity = new OrgEntity();
BeanUtils.copyProperties(orgDTO, orgEntity);
String curUsername = userLocalService.getUsername();
orgEntity.setId(idWorkerUtil.nextId())
.setCreateUsername(curUsername);
return orgMapper.insert(orgEntity);
}
第一种方式如上使用 int 返回给前端,这需要 return ,那么若遇到这种情况:
- 需要在 insert 后同步到缓存,那么代码就会不太美观
int count = orgMapper.insert(orgEntity);
// 缓存
...
return count;
即使返回返回了 int 给前端,但针对新增更新这种接口,前端一般是直接判断后端状态码是否等于 200 ,不会进一步去看返回行数是否大于一吧?另外出错的话也是被全局异常拦截,所以此时返回 int 无意义?
那么请教下各位,你们一般针对这种简单的保存和更新接口返回值用的是啥的,用 void ?
public void insert(OrgDTO orgDTO) {
OrgEntity orgEntity = new OrgEntity();
BeanUtils.copyProperties(orgDTO, orgEntity);
String curUsername = userLocalService.getUsername();
orgEntity.setId(idWorkerUtil.nextId())
.setCreateUsername(curUsername);
orgMapper.insert(orgEntity);
}