mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 00:58:24 +08:00
fix: close #I8IEPE
This commit is contained in:
parent
35e2991ed8
commit
41aaeb69cb
@ -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);
|
||||
}
|
||||
|
||||
@ -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);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user