fix: 修复配置全局配置默认的 id 为 auto 时,不生效的问题,close #I7QOD3

This commit is contained in:
开源海哥 2023-08-08 09:38:29 +08:00
parent f43bb61d92
commit 22ca55f25f
2 changed files with 29 additions and 2 deletions

View File

@ -17,6 +17,8 @@ package com.mybatisflex.core.table;
import com.mybatisflex.annotation.Id; import com.mybatisflex.annotation.Id;
import com.mybatisflex.annotation.KeyType; import com.mybatisflex.annotation.KeyType;
import com.mybatisflex.core.FlexGlobalConfig;
import com.mybatisflex.core.util.StringUtil;
public class IdInfo extends ColumnInfo { public class IdInfo extends ColumnInfo {
@ -53,16 +55,38 @@ public class IdInfo extends ColumnInfo {
if (Number.class.isAssignableFrom(columnInfo.getPropertyType())) { if (Number.class.isAssignableFrom(columnInfo.getPropertyType())) {
keyType = KeyType.Auto; keyType = KeyType.Auto;
} else { } else {
keyType = KeyType.None; initDefaultKeyType();
} }
} }
public IdInfo(Id id) { public IdInfo(Id id) {
this.keyType = id.keyType(); this.keyType = id.keyType();
this.value = id.value(); this.value = id.value();
this.before = id.before(); 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() { public KeyType getKeyType() {
return keyType; return keyType;
} }

View File

@ -370,11 +370,14 @@ public class TableInfo {
this.primaryColumns = new String[primaryKeyList.size()]; this.primaryColumns = new String[primaryKeyList.size()];
List<String> insertIdFields = new ArrayList<>(); List<String> insertIdFields = new ArrayList<>();
for (int i = 0; i < primaryKeyList.size(); i++) { for (int i = 0; i < primaryKeyList.size(); i++) {
IdInfo idInfo = primaryKeyList.get(i); IdInfo idInfo = primaryKeyList.get(i);
primaryColumns[i] = idInfo.getColumn(); 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()); insertIdFields.add(idInfo.getColumn());
} }