使用updateById(entity)时,entity通常为查询出来的,entity包含了所有字段值,修改时所有字段会更新(除了值为空的)。
全字段修改更容易出现并发数据覆盖问题,如必要可事先forupdate加锁或通过redis枷锁 或乐观锁(需考虑失败影响) 或这种更新update xxx set a = a+1,最好就是加行锁(更利于字段状态检查)。
最好进行选择性更改字段(除了可减少更新时并发覆盖问题。还可防止参数攻击,比如拿模型类接收传参,但某些参数要忽略。):
方式0:不能改的字段置为null
方式1:Entity upcols = new Entity(),upcols.setId(),set需要改的字段,然后updateById(upcols)
方式2:update(updateWrapper)
方式3:update(upcols,updateWrapper),upcols是重新new的对象,仅set要修改的字段
====================
mybatisplus乐观锁:
仅updateById(entity)、update(entity,updateWrapper)支持
需实体类中用@version标记版本字段
updateById(entity),entity的version设置为null,可忽略乐观锁。
update(entity,updateWrapper),entity的version设置为null,可忽略乐观锁。