-
自定义主键注解,使用在主键字段上
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 { }
-
自定义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; } }
- 已上传github地址: https://github.com/varyuan/awesome-java