通用mapper乐观锁

乐观锁

乐观锁实现中,要求一个实体类中只能有一个乐观锁字段。

配置 @Version

想要使用乐观锁,只需要在实体中,给乐观锁字段增加 @tk.mybatis.mapper.annotation.Version 注解。

例如:

public class User {
 	private Long id;
  	private String name;
    //...
    @Version
  	private Integer version;
  	//setter and getter
}

@Version 注解有一个 nextVersion 属性,默认值为默认的实现,默认实现如下:

package tk.mybatis.mapper.version;

import java.sql.Timestamp;

/**
 * @author liuzh
 * @since 3.5.0
 */
public class DefaultNextVersion implements NextVersion {

    @Override
    public Object nextVersion(Object current) throws VersionException {
        if (current == null) {
            throw new VersionException("当前版本号为空!");
        }
        if (current instanceof Integer) {
            return (Integer) current + 1;
        } else if (current instanceof Long) {
            return (Long) current + 1L;
        } else if (current instanceof Timestamp) {
            return new Timestamp(System.currentTimeMillis());
        } else {
            throw new VersionException("默认的 NextVersion 只支持 Integer, Long" +
                    " 和 java.sql.Timestamp 类型的版本号,如果有需要请自行扩展!");
        }
    }

}

默认实现支持 Integer, Longjava.sql.Timestamp ,如果默认实现不能满足自己的需要,可以实现自己的方法,在配置注解时指定自己的实现即可。

支持的方法

  • delete
  • deleteByPrimaryKey
  • updateByPrimaryKey
  • updateByPrimaryKeySelective
  • updateByExample
  • updateByExampleSelective

这些方法在执行时会更新乐观锁字段的值或者使用乐观锁的值作为查询条件。

需要注意的地方

在使用乐观锁时,由于通用 Mapper 是内置的实现,不是通过 拦截器 方式实现的,因此当执行上面支持的方法时,如果版本不一致,那么执行结果影响的行数可能就是 0。这种情况下也不会报错!

所以在 Java6,7中使用时,你需要自己在调用方法后进行判断是否执行成功。

在 Java8+ 中,可以通过默认方法来增加能够自动报错(抛异常)的方法,例如:

public interface MyMapper<T> extends Mapper<T> {
  
  default int deleteWithVersion(T t){
    int result = delete(t);
    if(result == 0){
      throw new RuntimeException("删除失败!");
    }
    return result;
  }
  
  default int updateByPrimaryKeyWithVersion(Object t){
    int result = updateByPrimaryKey(t);
    if(result == 0){
      throw new RuntimeException("更新失败!");
    }
    return result;
  }
  
  //...
  
}

通过增加上面这种默认方法就能实现。

猜你喜欢

转载自blog.csdn.net/lyf_ldh/article/details/81041329