From d20475480fd9294074c957650ffc420ccc0219ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=80=E6=BA=90=E6=B5=B7=E5=93=A5?= Date: Sun, 5 Mar 2023 10:57:09 +0800 Subject: [PATCH] =?UTF-8?q?add:=20=E6=B7=BB=E5=8A=A0=E4=B8=BB=E9=94=AE?= =?UTF-8?q?=E7=94=9F=E6=88=90=E7=9A=84=E5=85=A8=E5=B1=80=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=EF=BC=8C=E5=8F=AF=E4=BB=A5=E4=B8=8D=E7=94=A8=E4=B8=BA=E6=AF=8F?= =?UTF-8?q?=E4=B8=AA=20entity=20=E5=8D=95=E7=8B=AC=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/mybatisflex/annotation/Id.java | 2 +- .../mybatisflex/core/FlexGlobalConfig.java | 53 +++++++++++++++++++ .../core/key/CustomKeyGenerator.java | 5 +- .../core/key/MybatisKeyGeneratorUtil.java | 51 +++++++++++++++--- .../core/row/RowMapperInvoker.java | 2 +- .../com/mybatisflex/core/table/IdInfo.java | 8 +-- .../com/mybatisflex/core/table/TableInfo.java | 2 +- 7 files changed, 109 insertions(+), 14 deletions(-) diff --git a/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/Id.java b/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/Id.java index 1e55749b..99234d4a 100644 --- a/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/Id.java +++ b/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/Id.java @@ -29,7 +29,7 @@ public @interface Id { * * @return 生成策略 */ - KeyType keyType() default KeyType.Auto; + KeyType keyType() default KeyType.None; /** * 若 keyType 类型是 sequence, value 则代表的是 diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/FlexGlobalConfig.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/FlexGlobalConfig.java index a1801d09..181ae11d 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/FlexGlobalConfig.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/FlexGlobalConfig.java @@ -16,6 +16,8 @@ package com.mybatisflex.core; import com.mybatisflex.core.dialect.DbType; +import com.mybatisflex.core.enums.KeyType; +import org.apache.ibatis.session.Configuration; import org.apache.ibatis.session.SqlSessionFactory; import java.util.concurrent.ConcurrentHashMap; @@ -36,6 +38,12 @@ public class FlexGlobalConfig { private SqlSessionFactory sqlSessionFactory; + /** + * 全局的 ID 生成策略配置,当 @Id 未配置 或者 配置 KeyType 为 None 时 + * 使用当前全局配置 + */ + private KeyConfig keyConfig; + public DbType getDbType() { return dbType; @@ -53,6 +61,47 @@ public class FlexGlobalConfig { this.sqlSessionFactory = sqlSessionFactory; } + public KeyConfig getKeyConfig() { + return keyConfig; + } + + public void setKeyConfig(KeyConfig keyConfig) { + this.keyConfig = keyConfig; + } + + /** + * 对应的是 注解 {@link com.mybatisflex.annotation.Id} 的配置 + */ + public static class KeyConfig { + private KeyType keyType; + private String value; + private boolean before = true; + + public KeyType getKeyType() { + return keyType; + } + + public void setKeyType(KeyType keyType) { + this.keyType = keyType; + } + + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } + + public boolean isBefore() { + return before; + } + + public void setBefore(boolean before) { + this.before = before; + } + } + /////static factory methods///// private static ConcurrentHashMap globalConfigs = new ConcurrentHashMap(); @@ -66,6 +115,10 @@ public class FlexGlobalConfig { defaultConfig = config; } + public static FlexGlobalConfig getConfig(Configuration configuration) { + return globalConfigs.get(configuration.getEnvironment().getId()); + } + public static FlexGlobalConfig getConfig(String environmentId) { return globalConfigs.get(environmentId); } diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/key/CustomKeyGenerator.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/key/CustomKeyGenerator.java index f7d352aa..11014e55 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/key/CustomKeyGenerator.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/key/CustomKeyGenerator.java @@ -16,6 +16,7 @@ package com.mybatisflex.core.key; import com.mybatisflex.core.FlexConsts; +import com.mybatisflex.core.FlexGlobalConfig; import com.mybatisflex.core.exception.FlexExceptions; import com.mybatisflex.core.table.IdInfo; import com.mybatisflex.core.table.TableInfo; @@ -44,7 +45,9 @@ public class CustomKeyGenerator implements KeyGenerator { public CustomKeyGenerator(Configuration configuration, TableInfo tableInfo, IdInfo idInfo) { this.configuration = configuration; - this.keyGenerator = KeyGeneratorFactory.getKeyGenerator(idInfo.getValue()); + FlexGlobalConfig.KeyConfig globalKeyConfig = FlexGlobalConfig.getConfig(configuration).getKeyConfig(); + String keyValue = MybatisKeyGeneratorUtil.getKeyValue(idInfo, globalKeyConfig); + this.keyGenerator = KeyGeneratorFactory.getKeyGenerator(keyValue); this.tableInfo = tableInfo; this.idInfo = idInfo; diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/key/MybatisKeyGeneratorUtil.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/key/MybatisKeyGeneratorUtil.java index bbe86df2..0c8fcc07 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/key/MybatisKeyGeneratorUtil.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/key/MybatisKeyGeneratorUtil.java @@ -16,6 +16,7 @@ package com.mybatisflex.core.key; import com.mybatisflex.core.FlexConsts; +import com.mybatisflex.core.FlexGlobalConfig; import com.mybatisflex.core.enums.KeyType; import com.mybatisflex.core.exception.FlexExceptions; import com.mybatisflex.core.table.IdInfo; @@ -53,22 +54,25 @@ public class MybatisKeyGeneratorUtil { public static KeyGenerator createIdKeyGenerator(TableInfo tableInfo, MappedStatement ms, IdInfo idInfo) { - if (idInfo.getKeyType() == KeyType.None) { + FlexGlobalConfig.KeyConfig globalKeyConfig = FlexGlobalConfig.getConfig(ms.getConfiguration()).getKeyConfig(); + KeyType keyType = getKeyType(idInfo, globalKeyConfig); + + if (keyType == KeyType.None) { return NoKeyGenerator.INSTANCE; } //自增主键 - if (idInfo.getKeyType() == KeyType.Auto) { + if (keyType == KeyType.Auto) { return Jdbc3KeyGenerator.INSTANCE; } //通过 java 生成的主键 - if (idInfo.getKeyType() == KeyType.Generator) { + if (keyType == KeyType.Generator) { return new CustomKeyGenerator(ms.getConfiguration(), tableInfo, idInfo); } //通过序列生成的注解 - String sequence = idInfo.getValue(); + String sequence = getKeyValue(idInfo, globalKeyConfig); if (StringUtil.isBlank(sequence)) { throw FlexExceptions.wrap("please config @Id(value=\"...\") for field: %s in class: %s" , idInfo.getProperty() @@ -103,8 +107,7 @@ public class MybatisKeyGeneratorUtil { //因为只有在 xml 解析的时候,才可能存在多一个 MappedStatement 拥有同一个 keyGenerator 的情况 //当前每个方法都拥有一个自己的 keyGenerator 了,没必要添加 //this.addKeyGenerator(selectId, keyGenerator); - - return new SelectKeyGenerator(keyMappedStatement, idInfo.isBefore()); + return new SelectKeyGenerator(keyMappedStatement, isKeyBefore(idInfo, globalKeyConfig)); } @@ -115,4 +118,40 @@ public class MybatisKeyGeneratorUtil { return Arrays.asList(resultMap); } + + /** + * 获取主键的 keyType,优先通过 @id 获取,获取不到通过全局配置获取 + */ + public static KeyType getKeyType(IdInfo idInfo, FlexGlobalConfig.KeyConfig keyConfig) { + KeyType keyType = idInfo.getKeyType(); + if (keyType != KeyType.None) { + return keyType; + } + + if (keyConfig != null) { + return keyConfig.getKeyType(); + } + + return keyType; + } + + + public static String getKeyValue(IdInfo idInfo, FlexGlobalConfig.KeyConfig keyConfig) { + String value = idInfo.getValue(); + if (StringUtil.isBlank(value)) { + value = keyConfig.getValue(); + } + return value; + } + + + public static boolean isKeyBefore(IdInfo idInfo, FlexGlobalConfig.KeyConfig keyConfig) { + Boolean before = idInfo.getBefore(); + if (before == null) { + return keyConfig.isBefore(); + } else { + return before; + } + } + } diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/row/RowMapperInvoker.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/row/RowMapperInvoker.java index 00a013f4..7fed3f4c 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/row/RowMapperInvoker.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/row/RowMapperInvoker.java @@ -38,7 +38,7 @@ public class RowMapperInvoker { public RowMapperInvoker(SqlSessionFactory sqlSessionFactory) { this.sqlSessionFactory = sqlSessionFactory; - this.dbType = FlexGlobalConfig.getConfig(sqlSessionFactory.getConfiguration().getEnvironment().getId()).getDbType(); + this.dbType = FlexGlobalConfig.getConfig(sqlSessionFactory.getConfiguration()).getDbType(); } public RowSessionManager getRowSessionManager() { 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 c66e0aea..8232ce00 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 @@ -23,7 +23,7 @@ public class IdInfo extends ColumnInfo { /** * id 生成策略 */ - private KeyType keyType; + private KeyType keyType = KeyType.None; /** * 若 keyType 类型是 sequence, value 则代表的是 @@ -40,7 +40,7 @@ public class IdInfo extends ColumnInfo { * * @see org.apache.ibatis.executor.keygen.SelectKeyGenerator */ - private boolean before; + private Boolean before; public IdInfo(ColumnInfo columnInfo) { @@ -81,11 +81,11 @@ public class IdInfo extends ColumnInfo { this.value = value; } - public boolean isBefore() { + public Boolean getBefore() { return before; } - public void setBefore(boolean before) { + public void setBefore(Boolean before) { this.before = before; } } 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 7a4955ad..b69b2943 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 @@ -228,7 +228,7 @@ public class TableInfo { IdInfo idInfo = primaryKeyList.get(i); primaryKeys[i] = idInfo.getColumn(); - if (idInfo.getKeyType() != KeyType.Auto && idInfo.isBefore()) { + if (idInfo.getKeyType() != KeyType.Auto || (idInfo.getBefore() != null && idInfo.getBefore())) { insertIdFields.add(idInfo.getColumn()); }