From 7c30bb217d91aae871e553ac99914166d3588991 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=80=E6=BA=90=E6=B5=B7=E5=93=A5?= Date: Fri, 14 Jul 2023 14:41:38 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20insertSelectiveWithPk=20=E6=96=B9?= =?UTF-8?q?=E6=B3=95=20=E7=BB=99=E4=B8=BB=E9=94=AE=E8=B5=8B=E5=80=BC?= =?UTF-8?q?=E5=90=8E=E4=BC=9A=E6=8B=BC=E6=8E=A5=E4=B8=A4=E6=AC=A1id,=20clo?= =?UTF-8?q?se=20#I7L6DF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/mybatisflex/core/table/TableInfo.java | 70 +++++++++---------- .../java/com/mybatisflex/test/Account.java | 1 + .../com/mybatisflex/test/AccountTester.java | 19 +++++ 3 files changed, 55 insertions(+), 35 deletions(-) diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/TableInfo.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/TableInfo.java index 261884f5..ae46eec5 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/TableInfo.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/TableInfo.java @@ -479,7 +479,7 @@ public class TableInfo { 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) { QueryCondition joinQueryCondition = CPI.getJoinQueryCondition(join); QueryWrapper newWrapper = QueryWrapper.create() - .where(joinQueryCondition); + .where(joinQueryCondition); tableInfo.appendConditions(entity, newWrapper); CPI.setJoinQueryCondition(join, CPI.getWhereQueryCondition(newWrapper)); } @@ -857,8 +857,8 @@ public class TableInfo { public List getDefaultQueryColumn() { return Arrays.stream(defaultColumns) - .map(name -> columnQueryMapping.get(name)) - .collect(Collectors.toList()); + .map(name -> columnQueryMapping.get(name)) + .collect(Collectors.toList()); } @@ -904,9 +904,9 @@ public class TableInfo { ResultMap nestedResultMap = tableInfo.doBuildResultMap(configuration, resultMapIds, existMappingColumns, true, nestedPrefix); if (nestedResultMap != null) { resultMappings.add(new ResultMapping.Builder(configuration, fieldName) - .javaType(fieldType) - .nestedResultMapId(nestedResultMap.getId()) - .build()); + .javaType(fieldType) + .nestedResultMapId(nestedResultMap.getId()) + .build()); } }); } @@ -920,16 +920,16 @@ public class TableInfo { // 映射 String nestedResultMapId = entityClass.getName() + "." + field.getName(); ResultMapping resultMapping = new ResultMapping.Builder(configuration, null) - .column(columnName) - .typeHandler(new UnknownTypeHandler(configuration)) - .build(); + .column(columnName) + .typeHandler(new UnknownTypeHandler(configuration)) + .build(); ResultMap nestedResultMap = new ResultMap.Builder(configuration, nestedResultMapId, genericClass, Collections.singletonList(resultMapping)).build(); configuration.addResultMap(nestedResultMap); // 映射 resultMappings.add(new ResultMapping.Builder(configuration, field.getName()) - .javaType(field.getType()) - .nestedResultMapId(nestedResultMap.getId()) - .build()); + .javaType(field.getType()) + .nestedResultMapId(nestedResultMap.getId()) + .build()); } else { // 获取集合泛型类型的信息,也就是 ofType 属性 TableInfo tableInfo = TableInfoFactory.ofEntityClass(genericClass); @@ -937,9 +937,9 @@ public class TableInfo { ResultMap nestedResultMap = tableInfo.doBuildResultMap(configuration, resultMapIds, existMappingColumns, true, nestedPrefix); if (nestedResultMap != null) { resultMappings.add(new ResultMapping.Builder(configuration, field.getName()) - .javaType(field.getType()) - .nestedResultMapId(nestedResultMap.getId()) - .build()); + .javaType(field.getType()) + .nestedResultMapId(nestedResultMap.getId()) + .build()); } } }); @@ -953,18 +953,18 @@ public class TableInfo { private void doBuildColumnResultMapping(Configuration configuration, Set existMappingColumns, List resultMappings - , ColumnInfo columnInfo, List flags, boolean isNested) { + , ColumnInfo columnInfo, List flags, boolean isNested) { String[] columns = ArrayUtil.concat(new String[]{columnInfo.column, columnInfo.property}, columnInfo.alias); for (String column : columns) { if (!existMappingColumns.contains(column)) { ResultMapping mapping = new ResultMapping.Builder(configuration - , columnInfo.property - , column - , columnInfo.propertyType) - .jdbcType(columnInfo.getJdbcType()) - .flags(flags) - .typeHandler(columnInfo.buildTypeHandler()) - .build(); + , columnInfo.property + , column + , columnInfo.propertyType) + .jdbcType(columnInfo.getJdbcType()) + .flags(flags) + .typeHandler(columnInfo.buildTypeHandler()) + .build(); resultMappings.add(mapping); existMappingColumns.add(mapping.getColumn()); } @@ -975,13 +975,13 @@ public class TableInfo { column = tableName + "$" + column; if (!existMappingColumns.contains(column)) { ResultMapping mapping = new ResultMapping.Builder(configuration - , columnInfo.property - , column - , columnInfo.propertyType) - .jdbcType(columnInfo.getJdbcType()) - .flags(flags) - .typeHandler(columnInfo.buildTypeHandler()) - .build(); + , columnInfo.property + , column + , columnInfo.propertyType) + .jdbcType(columnInfo.getJdbcType()) + .flags(flags) + .typeHandler(columnInfo.buildTypeHandler()) + .build(); resultMappings.add(mapping); existMappingColumns.add(mapping.getColumn()); } @@ -1091,7 +1091,7 @@ public class TableInfo { private ResultSet getResultSet(Object value) { 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) { List listeners = MapUtil.computeIfAbsent(insertListenerCache, entityClass, aClass -> { List globalListeners = FlexGlobalConfig.getDefaultConfig() - .getSupportedInsertListener(entityClass, CollectionUtil.isNotEmpty(onInsertListeners)); + .getSupportedInsertListener(entityClass, CollectionUtil.isNotEmpty(onInsertListeners)); List allListeners = CollectionUtil.merge(onInsertListeners, globalListeners); Collections.sort(allListeners); return allListeners; @@ -1179,7 +1179,7 @@ public class TableInfo { public void invokeOnUpdateListener(Object entity) { List listeners = MapUtil.computeIfAbsent(updateListenerCache, entityClass, aClass -> { List globalListeners = FlexGlobalConfig.getDefaultConfig() - .getSupportedUpdateListener(entityClass, CollectionUtil.isNotEmpty(onUpdateListeners)); + .getSupportedUpdateListener(entityClass, CollectionUtil.isNotEmpty(onUpdateListeners)); List allListeners = CollectionUtil.merge(onUpdateListeners, globalListeners); Collections.sort(allListeners); return allListeners; @@ -1193,7 +1193,7 @@ public class TableInfo { public Object invokeOnSetListener(Object entity, String property, Object value) { List listeners = MapUtil.computeIfAbsent(setListenerCache, entityClass, aClass -> { List globalListeners = FlexGlobalConfig.getDefaultConfig() - .getSupportedSetListener(entityClass, CollectionUtil.isNotEmpty(onSetListeners)); + .getSupportedSetListener(entityClass, CollectionUtil.isNotEmpty(onSetListeners)); List allListeners = CollectionUtil.merge(onSetListeners, globalListeners); Collections.sort(allListeners); return allListeners; diff --git a/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/Account.java b/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/Account.java index c47fef27..c473a3b0 100644 --- a/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/Account.java +++ b/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/Account.java @@ -32,6 +32,7 @@ public class Account extends BaseEntity implements Serializable, AgeAware { private static final long serialVersionUID = 1L; +// @Id(keyType = KeyType.Generator,value = "uuid") @Id(keyType = KeyType.Auto) @ColumnAlias("account_id") private Long id; diff --git a/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/AccountTester.java b/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/AccountTester.java index 48c592c4..e132db96 100644 --- a/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/AccountTester.java +++ b/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/AccountTester.java @@ -107,5 +107,24 @@ public class AccountTester { } + /** + * https://gitee.com/mybatis-flex/mybatis-flex/issues/I7L6DF + */ + @Test + public void testInsertSelectiveWithPk() { + List 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); + + } + }