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
ConstructorsConstructorDescriptionGenIdKeyGenerator(GenId<?> genId, TableMeta table, ColumnMeta column, org.apache.ibatis.session.Configuration configuration, boolean executeBefore) 构造函数,初始化主键生成器。 -
Method Summary
Modifier and TypeMethodDescriptionvoid为参数对象生成主键值,支持单对象、Map、Iterator 和 Iterable 类型。void准备参数,当 executeBefore=true 时,确保在执行前生成主键。voidprocessAfter(org.apache.ibatis.executor.Executor executor, org.apache.ibatis.mapping.MappedStatement ms, Statement stmt, Object parameter) 在 SQL 执行后处理主键生成(若配置为插入后生成)。voidprocessBefore(org.apache.ibatis.executor.Executor executor, org.apache.ibatis.mapping.MappedStatement ms, Statement stmt, Object parameter) 在 SQL 执行前处理主键生成(若配置为插入前生成)。
-
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:
processBeforein interfaceorg.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:
processAfterin interfaceorg.apache.ibatis.executor.keygen.KeyGenerator- Parameters:
executor- MyBatis 执行器ms- MappedStatement 对象stmt- JDBC 语句对象parameter- 参数对象
-
genId
为参数对象生成主键值,支持单对象、Map、Iterator 和 Iterable 类型。- Parameters:
parameter- 参数对象
-
prepare
准备参数,当 executeBefore=true 时,确保在执行前生成主键。如果在 MappedStatement 初始化前调用此方法,可能未生成主键,此处补充生成。 初始化后,MyBatis 会通过 selectKey 自动调用,无需重复生成。
使用并发度阈值 (
CONCURRENCY) 限制重复生成,超过阈值后不再检查。 仅当首次并发插入超过并发度时,可能漏生成主键。- Parameters:
parameter- 参数对象
-