我是靠谱客的博主 高挑长颈鹿,这篇文章主要介绍MybatisPlus 使用 saveOrUpdate 详解(如果有某某一个主要字段的值重复,则更新,否则插入!)一、遇见的问题二、saveOrUpdate,现在分享给大家,希望可以做个参考。

MybatisPlus 使用 saveOrUpdate 详解

  • 一、遇见的问题
  • 二、saveOrUpdate

一、遇见的问题

报错信息:

com.baomidou.mybatisplus.core.exceptions.MybatisPlusException: error: can not execute. because can not find column for id from entity!

就是这个mybatisPlus不能找到哪个是主键字段,因为这个saveOrUpdate默认是根据主键执行操作的!
解决方法:需要在原本的实体类的主键头上,打个@TableId,已经主键自动递增。

  @TableId(value = "subject_Code", type = IdType.AUTO)
  private long id;

二、saveOrUpdate

// 根据updateWrapper尝试更新,否继续执行saveOrUpdate(T)方法
boolean saveOrUpdate(T entity, Wrapper<T> updateWrapper);
   public Response addWareHouse(@RequestBody List<GosInventoryDeviceRelationDTO> deviceRelationDTOList)  {
        if (CollectionUtils.isEmpty(deviceRelationDTOList)) {
            throw new BizServiceException(ServiceErrorEnum.PARA_ERROR);
        }
        for (GosInventoryDeviceRelationDTO gosInventoryDeviceRelationDTO : deviceRelationDTOList) {
                GosInventoryDeviceRelation gosInventoryDeviceRelation = new GosInventoryDeviceRelation();
                BeanUtils.copyProperties(gosInventoryDeviceRelationDTO, gosInventoryDeviceRelation);
                deviceRelationService.saveOrUpdate(gosInventoryDeviceRelation,new LambdaQueryWrapper<GosInventoryDeviceRelation>().eq(GosInventoryDeviceRelation::getDeviceOnlyCode, gosInventoryDeviceRelation.getDeviceOnlyCode()));     
        }
        return Response.ok(InventoryConstants.SUCCESS);
    }

最后

以上就是高挑长颈鹿最近收集整理的关于MybatisPlus 使用 saveOrUpdate 详解(如果有某某一个主要字段的值重复,则更新,否则插入!)一、遇见的问题二、saveOrUpdate的全部内容,更多相关MybatisPlus内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(129)

评论列表共有 0 条评论

立即
投稿
返回
顶部