mybatis-plus乐观锁

1. 乐观锁mybatis插件 配置

@Configuration
public class DataSourceConfig {

    /**
     * 乐观锁mybatis插件
     */
    @Bean
    public OptimisticLockerInterceptor optimisticLockerInterceptor() {
        return new OptimisticLockerInterceptor();
    }

2. 实体类中注解 @Version 库里也需要有version字段

@Data
@EqualsAndHashCode(callSuper = false)
@TableName("t_monitor_point_info")
public class MonitorPointInfo implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * UUID
     */
    @TableId(value = "id", type = IdType.ASSIGN_UUID)
    private String id;

    /**
     * 乐观锁 
     */
    @Version
    private Integer version;

3.直接调用更新方法自动更新version 字段

/**
     * 更新
     *
     * @param param 修改参数
     * @return 返回结果
     */
    @Override
    public boolean update(MonitorPointParam param) {
        MonitorPoint bean = super.getById(param.getId());
       
        bean.setMonitorPointNum(param.getMonitorPointNum());
        return super.updateById(tempUpdate);
    }

4. 查看乐观锁拦截

注意: 会先获取实体中version的值, 判断是否为空, 所以 version 默认需要给个值, 不能为 ‘null’
public Object intercept(Invocation invocation) throws Throwable {
        Object[] args = invocation.getArgs();
        MappedStatement ms = (MappedStatement)args[0];
        if (SqlCommandType.UPDATE != ms.getSqlCommandType()) {
            return invocation.proceed();
        } else {
            Object param = args[1];
            if (param instanceof Map) {
                Map map = (Map)param;
                Object et = map.getOrDefault("et", (Object)null);
                if (et != null) {
                    String methodId = ms.getId();
                    String methodName = methodId.substring(methodId.lastIndexOf(".") + 1);
                    TableInfo tableInfo = TableInfoHelper.getTableInfo(et.getClass());
                    if (tableInfo != null && tableInfo.isWithVersion()) {
                        TableFieldInfo fieldInfo = tableInfo.getVersionFieldInfo();
                        Field versionField = fieldInfo.getField();
       -----------------/ 注意这边 获取乐观锁值, 判断是否有值 /-----------------
                        Object originalVersionVal = versionField.get(et);
                        if (originalVersionVal == null) {
                            return invocation.proceed();
                        }

                        String versionColumn = fieldInfo.getColumn();
                        Object updatedVersionVal = this.getUpdatedVersionVal(fieldInfo.getPropertyType(), originalVersionVal);
                        if ("update".equals(methodName)) {
                            AbstractWrapper<?, ?, ?> aw = (AbstractWrapper)map.getOrDefault("ew", (Object)null);
                            if (aw == null) {
                                UpdateWrapper<?> uw = new UpdateWrapper();
                                uw.eq(versionColumn, originalVersionVal);
                                map.put("ew", uw);
                            } else {
                                aw.apply(versionColumn + " = {0}", new Object[]{originalVersionVal});
                            }
                        } else {
                            map.put("MP_OPTLOCK_VERSION_ORIGINAL", originalVersionVal);
                        }

                        versionField.set(et, updatedVersionVal);
                        return invocation.proceed();
                    }

                    return invocation.proceed();
                }
            }

            return invocation.proceed();
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值