fix: insertSelectiveWithPk 方法 给主键赋值后会拼接两次id, close #I7L6DF

This commit is contained in:
开源海哥 2023-07-14 14:41:38 +08:00
parent 082b574bd0
commit 7c30bb217d
3 changed files with 55 additions and 35 deletions

View File

@ -479,7 +479,7 @@ public class TableInfo {
retColumns.add(insertColumn); retColumns.add(insertColumn);
} }
} }
return ArrayUtil.concat(insertPrimaryKeys, retColumns.toArray(new String[0])); return retColumns.toArray(new String[0]);
} }
} }
@ -797,7 +797,7 @@ public class TableInfo {
if (tableInfo != null) { if (tableInfo != null) {
QueryCondition joinQueryCondition = CPI.getJoinQueryCondition(join); QueryCondition joinQueryCondition = CPI.getJoinQueryCondition(join);
QueryWrapper newWrapper = QueryWrapper.create() QueryWrapper newWrapper = QueryWrapper.create()
.where(joinQueryCondition); .where(joinQueryCondition);
tableInfo.appendConditions(entity, newWrapper); tableInfo.appendConditions(entity, newWrapper);
CPI.setJoinQueryCondition(join, CPI.getWhereQueryCondition(newWrapper)); CPI.setJoinQueryCondition(join, CPI.getWhereQueryCondition(newWrapper));
} }
@ -857,8 +857,8 @@ public class TableInfo {
public List<QueryColumn> getDefaultQueryColumn() { public List<QueryColumn> getDefaultQueryColumn() {
return Arrays.stream(defaultColumns) return Arrays.stream(defaultColumns)
.map(name -> columnQueryMapping.get(name)) .map(name -> columnQueryMapping.get(name))
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
@ -904,9 +904,9 @@ public class TableInfo {
ResultMap nestedResultMap = tableInfo.doBuildResultMap(configuration, resultMapIds, existMappingColumns, true, nestedPrefix); ResultMap nestedResultMap = tableInfo.doBuildResultMap(configuration, resultMapIds, existMappingColumns, true, nestedPrefix);
if (nestedResultMap != null) { if (nestedResultMap != null) {
resultMappings.add(new ResultMapping.Builder(configuration, fieldName) resultMappings.add(new ResultMapping.Builder(configuration, fieldName)
.javaType(fieldType) .javaType(fieldType)
.nestedResultMapId(nestedResultMap.getId()) .nestedResultMapId(nestedResultMap.getId())
.build()); .build());
} }
}); });
} }
@ -920,16 +920,16 @@ public class TableInfo {
// 映射 <result column="..."/> // 映射 <result column="..."/>
String nestedResultMapId = entityClass.getName() + "." + field.getName(); String nestedResultMapId = entityClass.getName() + "." + field.getName();
ResultMapping resultMapping = new ResultMapping.Builder(configuration, null) ResultMapping resultMapping = new ResultMapping.Builder(configuration, null)
.column(columnName) .column(columnName)
.typeHandler(new UnknownTypeHandler(configuration)) .typeHandler(new UnknownTypeHandler(configuration))
.build(); .build();
ResultMap nestedResultMap = new ResultMap.Builder(configuration, nestedResultMapId, genericClass, Collections.singletonList(resultMapping)).build(); ResultMap nestedResultMap = new ResultMap.Builder(configuration, nestedResultMapId, genericClass, Collections.singletonList(resultMapping)).build();
configuration.addResultMap(nestedResultMap); configuration.addResultMap(nestedResultMap);
// 映射 <collection property="..." ofType="genericClass"> // 映射 <collection property="..." ofType="genericClass">
resultMappings.add(new ResultMapping.Builder(configuration, field.getName()) resultMappings.add(new ResultMapping.Builder(configuration, field.getName())
.javaType(field.getType()) .javaType(field.getType())
.nestedResultMapId(nestedResultMap.getId()) .nestedResultMapId(nestedResultMap.getId())
.build()); .build());
} else { } else {
// 获取集合泛型类型的信息也就是 ofType 属性 // 获取集合泛型类型的信息也就是 ofType 属性
TableInfo tableInfo = TableInfoFactory.ofEntityClass(genericClass); TableInfo tableInfo = TableInfoFactory.ofEntityClass(genericClass);
@ -937,9 +937,9 @@ public class TableInfo {
ResultMap nestedResultMap = tableInfo.doBuildResultMap(configuration, resultMapIds, existMappingColumns, true, nestedPrefix); ResultMap nestedResultMap = tableInfo.doBuildResultMap(configuration, resultMapIds, existMappingColumns, true, nestedPrefix);
if (nestedResultMap != null) { if (nestedResultMap != null) {
resultMappings.add(new ResultMapping.Builder(configuration, field.getName()) resultMappings.add(new ResultMapping.Builder(configuration, field.getName())
.javaType(field.getType()) .javaType(field.getType())
.nestedResultMapId(nestedResultMap.getId()) .nestedResultMapId(nestedResultMap.getId())
.build()); .build());
} }
} }
}); });
@ -953,18 +953,18 @@ public class TableInfo {
private void doBuildColumnResultMapping(Configuration configuration, Set<String> existMappingColumns, List<ResultMapping> resultMappings private void doBuildColumnResultMapping(Configuration configuration, Set<String> existMappingColumns, List<ResultMapping> resultMappings
, ColumnInfo columnInfo, List<ResultFlag> flags, boolean isNested) { , ColumnInfo columnInfo, List<ResultFlag> flags, boolean isNested) {
String[] columns = ArrayUtil.concat(new String[]{columnInfo.column, columnInfo.property}, columnInfo.alias); String[] columns = ArrayUtil.concat(new String[]{columnInfo.column, columnInfo.property}, columnInfo.alias);
for (String column : columns) { for (String column : columns) {
if (!existMappingColumns.contains(column)) { if (!existMappingColumns.contains(column)) {
ResultMapping mapping = new ResultMapping.Builder(configuration ResultMapping mapping = new ResultMapping.Builder(configuration
, columnInfo.property , columnInfo.property
, column , column
, columnInfo.propertyType) , columnInfo.propertyType)
.jdbcType(columnInfo.getJdbcType()) .jdbcType(columnInfo.getJdbcType())
.flags(flags) .flags(flags)
.typeHandler(columnInfo.buildTypeHandler()) .typeHandler(columnInfo.buildTypeHandler())
.build(); .build();
resultMappings.add(mapping); resultMappings.add(mapping);
existMappingColumns.add(mapping.getColumn()); existMappingColumns.add(mapping.getColumn());
} }
@ -975,13 +975,13 @@ public class TableInfo {
column = tableName + "$" + column; column = tableName + "$" + column;
if (!existMappingColumns.contains(column)) { if (!existMappingColumns.contains(column)) {
ResultMapping mapping = new ResultMapping.Builder(configuration ResultMapping mapping = new ResultMapping.Builder(configuration
, columnInfo.property , columnInfo.property
, column , column
, columnInfo.propertyType) , columnInfo.propertyType)
.jdbcType(columnInfo.getJdbcType()) .jdbcType(columnInfo.getJdbcType())
.flags(flags) .flags(flags)
.typeHandler(columnInfo.buildTypeHandler()) .typeHandler(columnInfo.buildTypeHandler())
.build(); .build();
resultMappings.add(mapping); resultMappings.add(mapping);
existMappingColumns.add(mapping.getColumn()); existMappingColumns.add(mapping.getColumn());
} }
@ -1091,7 +1091,7 @@ public class TableInfo {
private ResultSet getResultSet(Object value) { private ResultSet getResultSet(Object value) {
return (ResultSet) Proxy.newProxyInstance(TableInfo.class.getClassLoader(), return (ResultSet) Proxy.newProxyInstance(TableInfo.class.getClassLoader(),
new Class[]{ResultSet.class}, (proxy, method, args) -> value); new Class[]{ResultSet.class}, (proxy, method, args) -> value);
} }
@ -1165,7 +1165,7 @@ public class TableInfo {
public void invokeOnInsertListener(Object entity) { public void invokeOnInsertListener(Object entity) {
List<InsertListener> listeners = MapUtil.computeIfAbsent(insertListenerCache, entityClass, aClass -> { List<InsertListener> listeners = MapUtil.computeIfAbsent(insertListenerCache, entityClass, aClass -> {
List<InsertListener> globalListeners = FlexGlobalConfig.getDefaultConfig() List<InsertListener> globalListeners = FlexGlobalConfig.getDefaultConfig()
.getSupportedInsertListener(entityClass, CollectionUtil.isNotEmpty(onInsertListeners)); .getSupportedInsertListener(entityClass, CollectionUtil.isNotEmpty(onInsertListeners));
List<InsertListener> allListeners = CollectionUtil.merge(onInsertListeners, globalListeners); List<InsertListener> allListeners = CollectionUtil.merge(onInsertListeners, globalListeners);
Collections.sort(allListeners); Collections.sort(allListeners);
return allListeners; return allListeners;
@ -1179,7 +1179,7 @@ public class TableInfo {
public void invokeOnUpdateListener(Object entity) { public void invokeOnUpdateListener(Object entity) {
List<UpdateListener> listeners = MapUtil.computeIfAbsent(updateListenerCache, entityClass, aClass -> { List<UpdateListener> listeners = MapUtil.computeIfAbsent(updateListenerCache, entityClass, aClass -> {
List<UpdateListener> globalListeners = FlexGlobalConfig.getDefaultConfig() List<UpdateListener> globalListeners = FlexGlobalConfig.getDefaultConfig()
.getSupportedUpdateListener(entityClass, CollectionUtil.isNotEmpty(onUpdateListeners)); .getSupportedUpdateListener(entityClass, CollectionUtil.isNotEmpty(onUpdateListeners));
List<UpdateListener> allListeners = CollectionUtil.merge(onUpdateListeners, globalListeners); List<UpdateListener> allListeners = CollectionUtil.merge(onUpdateListeners, globalListeners);
Collections.sort(allListeners); Collections.sort(allListeners);
return allListeners; return allListeners;
@ -1193,7 +1193,7 @@ public class TableInfo {
public Object invokeOnSetListener(Object entity, String property, Object value) { public Object invokeOnSetListener(Object entity, String property, Object value) {
List<SetListener> listeners = MapUtil.computeIfAbsent(setListenerCache, entityClass, aClass -> { List<SetListener> listeners = MapUtil.computeIfAbsent(setListenerCache, entityClass, aClass -> {
List<SetListener> globalListeners = FlexGlobalConfig.getDefaultConfig() List<SetListener> globalListeners = FlexGlobalConfig.getDefaultConfig()
.getSupportedSetListener(entityClass, CollectionUtil.isNotEmpty(onSetListeners)); .getSupportedSetListener(entityClass, CollectionUtil.isNotEmpty(onSetListeners));
List<SetListener> allListeners = CollectionUtil.merge(onSetListeners, globalListeners); List<SetListener> allListeners = CollectionUtil.merge(onSetListeners, globalListeners);
Collections.sort(allListeners); Collections.sort(allListeners);
return allListeners; return allListeners;

View File

@ -32,6 +32,7 @@ public class Account extends BaseEntity implements Serializable, AgeAware {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
// @Id(keyType = KeyType.Generator,value = "uuid")
@Id(keyType = KeyType.Auto) @Id(keyType = KeyType.Auto)
@ColumnAlias("account_id") @ColumnAlias("account_id")
private Long id; private Long id;

View File

@ -107,5 +107,24 @@ public class AccountTester {
} }
/**
* https://gitee.com/mybatis-flex/mybatis-flex/issues/I7L6DF
*/
@Test
public void testInsertSelectiveWithPk() {
List<Account> accounts = accountMapper.selectAll();
System.out.println(accounts);
Account account = new Account();
account.setId(4L);
account.setUserName("test04");
accountMapper.insertSelectiveWithPk(account);
accounts = accountMapper.selectAll();
System.out.println(accounts);
}
} }