mybatis sql拦截器(可用于自定义主键生成规则等情况)

  1. 自定义主键注解,使用在主键字段上

    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    
    @Target({ElementType.FIELD})
    @Retention(RetentionPolicy.RUNTIME)
    public @interface PrimaryKey {
    }
    
  2. 自定义mybatis拦截器, 拦截mybatis的insert操作, 使用生成的主键设置到@PrimaryKey注解的字段上

    import com.varyuan.awesome.annotation.PrimaryKey;
    import lombok.extern.slf4j.Slf4j;
    import org.apache.ibatis.executor.Executor;
    import org.apache.ibatis.mapping.MappedStatement;
    import org.apache.ibatis.mapping.SqlCommandType;
    import org.apache.ibatis.plugin.Interceptor;
    import org.apache.ibatis.plugin.Intercepts;
    import org.apache.ibatis.plugin.Invocation;
    import org.apache.ibatis.plugin.Signature;
    import org.springframework.stereotype.Component;
    
    import java.lang.reflect.Field;
    
    // 拦截mybatis的insert语句, 使用自定义策略生成主键
    @Component
    @Intercepts(@Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class}))
    @Slf4j
    public class PrimaryKeyInterceptor implements Interceptor {
    
        @Override
        public Object intercept(Invocation invocation) throws Throwable {
            Object[] args = invocation.getArgs();
            for (Object o : args) {
                if (o instanceof MappedStatement) {
                    MappedStatement mappedStatement = (MappedStatement) o;
                    if (!SqlCommandType.INSERT.equals(mappedStatement.getSqlCommandType())) {
                        return invocation.proceed();
                    }
                } else {
                    Field[] fields = o.getClass().getDeclaredFields();
                    for (Field field : fields) {
                        if (field.getAnnotation(PrimaryKey.class) != null) {
                            // 主键已设置不再生成
                            field.setAccessible(true);
                            if (field.get(o) != null) {
                                return invocation.proceed();
                            }
                            // 生成并设置主键
                            String name = field.getName();
                            String setName = "set" + name.substring(0, 1).toUpperCase() + name.substring(1);
                            Integer primaryKey = generatePrimaryKey();
                            o.getClass().getMethod(setName, Integer.class).invoke(o, primaryKey);
                            log.info("set primary key: {}, value: {}", name, generatePrimaryKey());
                            return invocation.proceed();
                        }
                    }
                }
            }
            return invocation.proceed();
        }
    
        // 生成主键, 可使用snowflake分布式ID生成算法, 可参考shardingsphere的实现:org.apache.shardingsphere.core.strategy.keygen.SnowflakeShardingKeyGenerator
        private Integer generatePrimaryKey() {
            // todo
            return 1;
        }
    }
    
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值