From 22ca55f25fadda11ed25685a8b0ddb95de55dd55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=80=E6=BA=90=E6=B5=B7=E5=93=A5?= Date: Tue, 8 Aug 2023 09:38:29 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E5=85=A8=E5=B1=80=E9=85=8D=E7=BD=AE=E9=BB=98=E8=AE=A4=E7=9A=84?= =?UTF-8?q?=20id=20=E4=B8=BA=20auto=20=E6=97=B6=EF=BC=8C=E4=B8=8D=E7=94=9F?= =?UTF-8?q?=E6=95=88=E7=9A=84=E9=97=AE=E9=A2=98=EF=BC=8Cclose=20#I7QOD3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/mybatisflex/core/table/IdInfo.java | 26 ++++++++++++++++++- .../com/mybatisflex/core/table/TableInfo.java | 5 +++- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/IdInfo.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/IdInfo.java index 0561fa4a..390b9cf0 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/IdInfo.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/IdInfo.java @@ -17,6 +17,8 @@ package com.mybatisflex.core.table; import com.mybatisflex.annotation.Id; import com.mybatisflex.annotation.KeyType; +import com.mybatisflex.core.FlexGlobalConfig; +import com.mybatisflex.core.util.StringUtil; public class IdInfo extends ColumnInfo { @@ -53,16 +55,38 @@ public class IdInfo extends ColumnInfo { if (Number.class.isAssignableFrom(columnInfo.getPropertyType())) { keyType = KeyType.Auto; } else { - keyType = KeyType.None; + initDefaultKeyType(); } } + public IdInfo(Id id) { this.keyType = id.keyType(); this.value = id.value(); this.before = id.before(); + + initDefaultKeyType(); } + /** + * 用户未配置 keyType 是,配置默认的 key Type + */ + private void initDefaultKeyType() { + if (this.keyType == null || this.keyType == KeyType.None) { + FlexGlobalConfig.KeyConfig defaultKeyConfig = FlexGlobalConfig.getDefaultConfig().getKeyConfig(); + if (defaultKeyConfig != null) { + if (defaultKeyConfig.getKeyType() != null) { + this.keyType = defaultKeyConfig.getKeyType(); + this.before = defaultKeyConfig.isBefore(); + } + if (StringUtil.isBlank(this.value) && StringUtil.isNotBlank(defaultKeyConfig.getValue())) { + this.value = defaultKeyConfig.getValue(); + } + } + } + } + + public KeyType getKeyType() { return keyType; } 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 8ff6cf40..650fd308 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 @@ -370,11 +370,14 @@ public class TableInfo { this.primaryColumns = new String[primaryKeyList.size()]; List insertIdFields = new ArrayList<>(); + for (int i = 0; i < primaryKeyList.size(); i++) { IdInfo idInfo = primaryKeyList.get(i); primaryColumns[i] = idInfo.getColumn(); - if (idInfo.getKeyType() != KeyType.Auto && (idInfo.getBefore() != null && idInfo.getBefore())) { + if (idInfo.getKeyType() != KeyType.Auto + || (idInfo.getBefore() != null && idInfo.getBefore()) + ) { insertIdFields.add(idInfo.getColumn()); }