diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/keygen/CustomKeyGenerator.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/keygen/CustomKeyGenerator.java index 1cdd00ab..da46f386 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/keygen/CustomKeyGenerator.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/keygen/CustomKeyGenerator.java @@ -22,6 +22,7 @@ import com.mybatisflex.core.exception.FlexExceptions; import com.mybatisflex.core.table.IdInfo; import com.mybatisflex.core.table.TableInfo; import com.mybatisflex.core.util.ConvertUtil; +import com.mybatisflex.core.util.StringUtil; import org.apache.ibatis.executor.Executor; import org.apache.ibatis.executor.ExecutorException; import org.apache.ibatis.executor.keygen.KeyGenerator; @@ -71,17 +72,18 @@ public class CustomKeyGenerator implements KeyGenerator { Object entity = ((Map) parameter).get(FlexConsts.ENTITY); try { Object existId = tableInfo.getValue(entity, idInfo.getProperty()); + // 若用户主动设置了主键,则使用用户自己设置的主键,不再生成主键 - if (existId != null){ - return; + // 只有主键为 null 或者 空字符串时,对主键进行设置 + if (existId == null || (existId instanceof String && StringUtil.isBlank((String) existId))) { + Configuration configuration = ms.getConfiguration(); + MetaObject metaParam = configuration.newMetaObject(parameter); + Object generateId = keyGenerator.generate(entity, idInfo.getColumn()); + MetaObject metaObjectForProperty = metaParam.metaObjectForProperty(FlexConsts.ENTITY); + Invoker setInvoker = tableInfo.getReflector().getSetInvoker(idInfo.getProperty()); + Object id = ConvertUtil.convert(generateId, setInvoker.getType()); + this.setValue(metaObjectForProperty, this.idInfo.getProperty(), id); } - Configuration configuration = ms.getConfiguration(); - MetaObject metaParam = configuration.newMetaObject(parameter); - Object generateId = keyGenerator.generate(entity, idInfo.getColumn()); - MetaObject metaObjectForProperty = metaParam.metaObjectForProperty(FlexConsts.ENTITY); - Invoker setInvoker = tableInfo.getReflector().getSetInvoker(idInfo.getProperty()); - Object id = ConvertUtil.convert(generateId, setInvoker.getType()); - this.setValue(metaObjectForProperty, this.idInfo.getProperty(), id); } catch (Exception e) { throw FlexExceptions.wrap(e); } diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/keygen/RowCustomKeyGenerator.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/keygen/RowCustomKeyGenerator.java index 125ae042..7ba218a7 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/keygen/RowCustomKeyGenerator.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/keygen/RowCustomKeyGenerator.java @@ -20,6 +20,7 @@ import com.mybatisflex.core.FlexConsts; import com.mybatisflex.core.exception.FlexExceptions; import com.mybatisflex.core.row.Row; import com.mybatisflex.core.row.RowKey; +import com.mybatisflex.core.util.StringUtil; import org.apache.ibatis.executor.Executor; import org.apache.ibatis.executor.keygen.KeyGenerator; import org.apache.ibatis.mapping.MappedStatement; @@ -58,11 +59,11 @@ public class RowCustomKeyGenerator implements KeyGenerator { try { Object existId = row.get(rowKey.getKeyColumn()); // 若用户主动设置了主键,则使用用户自己设置的主键,不再生成主键 - if (existId != null) { - return; + // 只有主键为 null 或者 空字符串时,对主键进行设置 + if (existId == null || (existId instanceof String && StringUtil.isBlank((String) existId))) { + Object generateId = keyGenerator.generate(row, rowKey.getKeyColumn()); + row.put(rowKey.getKeyColumn(), generateId); } - Object generateId = keyGenerator.generate(row, rowKey.getKeyColumn()); - row.put(rowKey.getKeyColumn(), generateId); } catch (Exception e) { throw FlexExceptions.wrap(e); }