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

时间:2025-04-27 15:28:56

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

(());//待更新的字段
LambdaUpdateWrapper lambdaUpdateWrapper = new LambdaUpdateWrapper();
(McTemplate::getId,() ); //限定条件
Integer result = (mcTemplate, lambdaUpdateWrapper); //更新mcTemplate中不为空的字段


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

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


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

int result = (mcTemplate);