Module bus.mapper

Class GenIdKeyGenerator

java.lang.Object
org.miaixz.bus.mapper.support.keysql.GenIdKeyGenerator
All Implemented Interfaces:
org.apache.ibatis.executor.keygen.KeyGenerator

public class GenIdKeyGenerator extends Object implements org.apache.ibatis.executor.keygen.KeyGenerator
主键生成器,负责在插入操作前或后生成主键值。
Since:
Java 17+
Author:
Kimi Liu
  • Constructor Summary

    Constructors
    Constructor
    Description
    GenIdKeyGenerator(GenId<?> genId, TableMeta table, ColumnMeta column, org.apache.ibatis.session.Configuration configuration, boolean executeBefore)
    构造函数,初始化主键生成器。
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    genId(Object parameter)
    为参数对象生成主键值,支持单对象、Map、Iterator 和 Iterable 类型。
    void
    prepare(Object parameter)
    准备参数,当 executeBefore=true 时,确保在执行前生成主键。
    void
    processAfter(org.apache.ibatis.executor.Executor executor, org.apache.ibatis.mapping.MappedStatement ms, Statement stmt, Object parameter)
    在 SQL 执行后处理主键生成(若配置为插入后生成)。
    void
    processBefore(org.apache.ibatis.executor.Executor executor, org.apache.ibatis.mapping.MappedStatement ms, Statement stmt, Object parameter)
    在 SQL 执行前处理主键生成(若配置为插入前生成)。

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • GenIdKeyGenerator

      public GenIdKeyGenerator(GenId<?> genId, TableMeta table, ColumnMeta column, org.apache.ibatis.session.Configuration configuration, boolean executeBefore)
      构造函数,初始化主键生成器。
      Parameters:
      genId - 主键生成器接口实例
      table - 实体表信息
      column - 主键列信息
      configuration - MyBatis 配置对象
      executeBefore - 是否在插入前生成主键
  • Method Details

    • processBefore

      public void processBefore(org.apache.ibatis.executor.Executor executor, org.apache.ibatis.mapping.MappedStatement ms, Statement stmt, Object parameter)
      在 SQL 执行前处理主键生成(若配置为插入前生成)。
      Specified by:
      processBefore in interface org.apache.ibatis.executor.keygen.KeyGenerator
      Parameters:
      executor - MyBatis 执行器
      ms - MappedStatement 对象
      stmt - JDBC 语句对象
      parameter - 参数对象
    • processAfter

      public void processAfter(org.apache.ibatis.executor.Executor executor, org.apache.ibatis.mapping.MappedStatement ms, Statement stmt, Object parameter)
      在 SQL 执行后处理主键生成(若配置为插入后生成)。
      Specified by:
      processAfter in interface org.apache.ibatis.executor.keygen.KeyGenerator
      Parameters:
      executor - MyBatis 执行器
      ms - MappedStatement 对象
      stmt - JDBC 语句对象
      parameter - 参数对象
    • genId

      public void genId(Object parameter)
      为参数对象生成主键值,支持单对象、Map、Iterator 和 Iterable 类型。
      Parameters:
      parameter - 参数对象
    • prepare

      public void prepare(Object parameter)
      准备参数,当 executeBefore=true 时,确保在执行前生成主键。

      如果在 MappedStatement 初始化前调用此方法,可能未生成主键,此处补充生成。 初始化后,MyBatis 会通过 selectKey 自动调用,无需重复生成。

      使用并发度阈值 (CONCURRENCY) 限制重复生成,超过阈值后不再检查。 仅当首次并发插入超过并发度时,可能漏生成主键。

      Parameters:
      parameter - 参数对象