mybatisplus中update--更新使用注意事项

<一> update(pojo,Wrapper)方法:封装一个对象mcTemplate,使用update(pojo,Wrapper) ,该方法仅仅修改mcTemplate中不为空的字段,别的字段不更新,在数据库中保持不变,如下:

mcTemplate.setStatus(TemplateStatusEnum.PASSED.getStatus());//待更新的字段
LambdaUpdateWrapper lambdaUpdateWrapper = new LambdaUpdateWrapper();
lambdaUpdateWrapper.eq(McTemplate::getId,mcTemplate.getId() ); //限定条件
Integer result = mcTemplateMapper.update(mcTemplate, lambdaUpdateWrapper); //更新mcTemplate中不为空的字段


<二>更新的字段比较少,不想封装成一个对象的时候,可以采取便捷方式,该方式同上只会更新设定的字段,对于其他字段不更新。

LambdaUpdateWrapper lambdaUpdateWrapper = new LambdaUpdateWrapper();
lambdaUpdateWrapper.eq(McTemplate::getId,mcTemplate.getId() )
.set(McTemplate::getStatus,1); //更新的值
Integer result = mcTemplateMapper.update(null, lambdaUpdateWrapper);


<三>updateById(mcTemplate)方法。该方法会将所有的字段都更新,在对象mcTemplate中没有的字段,会字段赋值null

int result = mcTemplateMapper.updateById(mcTemplate);

你可能感兴趣的:(myBatisPlus,java,mysql)