注意点:
BeanUtil(cn.hutool.core.bean;)
mybatisPlus设置了更新字段为空时不更新数据库
//复制属性操作实体类
SysPost sysPost = BeanUtil.copyProperties(postDTO, SysPost.class);//更新时数据库不更新字段为空的属性
Boolean result = false;
//根据用户名查询列表
List<SysPost> postList = list(new QueryWrapper<SysPost>().eq( ObjectUtil.isNotNull( sysPost.getPostName()) ,"post_name" ,sysPost.getPostName() ) );
//id不为空且!=0 ,更新
if (ObjectUtil.isNotNull(sysPost.getId()) && sysPost.getId() != 0) {
//更新
result = saveOrUpdate(sysPost);
}else {
if (ObjectUtil.isNotNull(postList) && postList.size() != 0 && !postList.get(0).getPostName().equals(sysPost.getPostName())) {
throw new BusinessException(CommonErrCode.APP_NAME_REPEAT);
}
//新增
sysPost.setId(IdGenerator.getId());
result = save(sysPost);
}
return result ? Result.succeed(sysPost, StringPool.SUCCESS_MSG) : Result.failed(StringPool.FAIL_MSG);