fix: close #I8IEPE

This commit is contained in:
开源海哥 2023-11-23 10:07:33 +08:00
parent 35e2991ed8
commit 41aaeb69cb
2 changed files with 16 additions and 13 deletions

View File

@ -22,6 +22,7 @@ import com.mybatisflex.core.exception.FlexExceptions;
import com.mybatisflex.core.table.IdInfo; import com.mybatisflex.core.table.IdInfo;
import com.mybatisflex.core.table.TableInfo; import com.mybatisflex.core.table.TableInfo;
import com.mybatisflex.core.util.ConvertUtil; import com.mybatisflex.core.util.ConvertUtil;
import com.mybatisflex.core.util.StringUtil;
import org.apache.ibatis.executor.Executor; import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.executor.ExecutorException; import org.apache.ibatis.executor.ExecutorException;
import org.apache.ibatis.executor.keygen.KeyGenerator; import org.apache.ibatis.executor.keygen.KeyGenerator;
@ -71,17 +72,18 @@ public class CustomKeyGenerator implements KeyGenerator {
Object entity = ((Map) parameter).get(FlexConsts.ENTITY); Object entity = ((Map) parameter).get(FlexConsts.ENTITY);
try { try {
Object existId = tableInfo.getValue(entity, idInfo.getProperty()); Object existId = tableInfo.getValue(entity, idInfo.getProperty());
// 若用户主动设置了主键则使用用户自己设置的主键不再生成主键 // 若用户主动设置了主键则使用用户自己设置的主键不再生成主键
if (existId != null){ // 只有主键为 null 或者 空字符串时对主键进行设置
return; 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) { } catch (Exception e) {
throw FlexExceptions.wrap(e); throw FlexExceptions.wrap(e);
} }

View File

@ -20,6 +20,7 @@ import com.mybatisflex.core.FlexConsts;
import com.mybatisflex.core.exception.FlexExceptions; import com.mybatisflex.core.exception.FlexExceptions;
import com.mybatisflex.core.row.Row; import com.mybatisflex.core.row.Row;
import com.mybatisflex.core.row.RowKey; import com.mybatisflex.core.row.RowKey;
import com.mybatisflex.core.util.StringUtil;
import org.apache.ibatis.executor.Executor; import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.executor.keygen.KeyGenerator; import org.apache.ibatis.executor.keygen.KeyGenerator;
import org.apache.ibatis.mapping.MappedStatement; import org.apache.ibatis.mapping.MappedStatement;
@ -58,11 +59,11 @@ public class RowCustomKeyGenerator implements KeyGenerator {
try { try {
Object existId = row.get(rowKey.getKeyColumn()); Object existId = row.get(rowKey.getKeyColumn());
// 若用户主动设置了主键则使用用户自己设置的主键不再生成主键 // 若用户主动设置了主键则使用用户自己设置的主键不再生成主键
if (existId != null) { // 只有主键为 null 或者 空字符串时对主键进行设置
return; 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) { } catch (Exception e) {
throw FlexExceptions.wrap(e); throw FlexExceptions.wrap(e);
} }