add: 添加主键生成的全局配置,可以不用为每个 entity 单独配置

This commit is contained in:
开源海哥 2023-03-05 10:57:09 +08:00
parent 19bf420627
commit d20475480f
7 changed files with 109 additions and 14 deletions

View File

@ -29,7 +29,7 @@ public @interface Id {
* *
* @return 生成策略 * @return 生成策略
*/ */
KeyType keyType() default KeyType.Auto; KeyType keyType() default KeyType.None;
/** /**
* keyType 类型是 sequence value 则代表的是 * keyType 类型是 sequence value 则代表的是

View File

@ -16,6 +16,8 @@
package com.mybatisflex.core; package com.mybatisflex.core;
import com.mybatisflex.core.dialect.DbType; 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 org.apache.ibatis.session.SqlSessionFactory;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
@ -36,6 +38,12 @@ public class FlexGlobalConfig {
private SqlSessionFactory sqlSessionFactory; private SqlSessionFactory sqlSessionFactory;
/**
* 全局的 ID 生成策略配置 @Id 未配置 或者 配置 KeyType None
* 使用当前全局配置
*/
private KeyConfig keyConfig;
public DbType getDbType() { public DbType getDbType() {
return dbType; return dbType;
@ -53,6 +61,47 @@ public class FlexGlobalConfig {
this.sqlSessionFactory = sqlSessionFactory; 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///// /////static factory methods/////
private static ConcurrentHashMap<String, FlexGlobalConfig> globalConfigs = new ConcurrentHashMap(); private static ConcurrentHashMap<String, FlexGlobalConfig> globalConfigs = new ConcurrentHashMap();
@ -66,6 +115,10 @@ public class FlexGlobalConfig {
defaultConfig = config; defaultConfig = config;
} }
public static FlexGlobalConfig getConfig(Configuration configuration) {
return globalConfigs.get(configuration.getEnvironment().getId());
}
public static FlexGlobalConfig getConfig(String environmentId) { public static FlexGlobalConfig getConfig(String environmentId) {
return globalConfigs.get(environmentId); return globalConfigs.get(environmentId);
} }

View File

@ -16,6 +16,7 @@
package com.mybatisflex.core.key; package com.mybatisflex.core.key;
import com.mybatisflex.core.FlexConsts; import com.mybatisflex.core.FlexConsts;
import com.mybatisflex.core.FlexGlobalConfig;
import com.mybatisflex.core.exception.FlexExceptions; import com.mybatisflex.core.exception.FlexExceptions;
import com.mybatisflex.core.table.IdInfo; import com.mybatisflex.core.table.IdInfo;
import com.mybatisflex.core.table.TableInfo; import com.mybatisflex.core.table.TableInfo;
@ -44,7 +45,9 @@ public class CustomKeyGenerator implements KeyGenerator {
public CustomKeyGenerator(Configuration configuration, TableInfo tableInfo, IdInfo idInfo) { public CustomKeyGenerator(Configuration configuration, TableInfo tableInfo, IdInfo idInfo) {
this.configuration = configuration; 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.tableInfo = tableInfo;
this.idInfo = idInfo; this.idInfo = idInfo;

View File

@ -16,6 +16,7 @@
package com.mybatisflex.core.key; package com.mybatisflex.core.key;
import com.mybatisflex.core.FlexConsts; import com.mybatisflex.core.FlexConsts;
import com.mybatisflex.core.FlexGlobalConfig;
import com.mybatisflex.core.enums.KeyType; import com.mybatisflex.core.enums.KeyType;
import com.mybatisflex.core.exception.FlexExceptions; import com.mybatisflex.core.exception.FlexExceptions;
import com.mybatisflex.core.table.IdInfo; import com.mybatisflex.core.table.IdInfo;
@ -53,22 +54,25 @@ public class MybatisKeyGeneratorUtil {
public static KeyGenerator createIdKeyGenerator(TableInfo tableInfo, MappedStatement ms, IdInfo idInfo) { 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; return NoKeyGenerator.INSTANCE;
} }
//自增主键 //自增主键
if (idInfo.getKeyType() == KeyType.Auto) { if (keyType == KeyType.Auto) {
return Jdbc3KeyGenerator.INSTANCE; return Jdbc3KeyGenerator.INSTANCE;
} }
//通过 java 生成的主键 //通过 java 生成的主键
if (idInfo.getKeyType() == KeyType.Generator) { if (keyType == KeyType.Generator) {
return new CustomKeyGenerator(ms.getConfiguration(), tableInfo, idInfo); return new CustomKeyGenerator(ms.getConfiguration(), tableInfo, idInfo);
} }
//通过序列生成的注解 //通过序列生成的注解
String sequence = idInfo.getValue(); String sequence = getKeyValue(idInfo, globalKeyConfig);
if (StringUtil.isBlank(sequence)) { if (StringUtil.isBlank(sequence)) {
throw FlexExceptions.wrap("please config @Id(value=\"...\") for field: %s in class: %s" throw FlexExceptions.wrap("please config @Id(value=\"...\") for field: %s in class: %s"
, idInfo.getProperty() , idInfo.getProperty()
@ -103,8 +107,7 @@ public class MybatisKeyGeneratorUtil {
//因为只有在 xml 解析的时候才可能存在多一个 MappedStatement 拥有同一个 keyGenerator 的情况 //因为只有在 xml 解析的时候才可能存在多一个 MappedStatement 拥有同一个 keyGenerator 的情况
//当前每个方法都拥有一个自己的 keyGenerator 没必要添加 //当前每个方法都拥有一个自己的 keyGenerator 没必要添加
//this.addKeyGenerator(selectId, keyGenerator); //this.addKeyGenerator(selectId, keyGenerator);
return new SelectKeyGenerator(keyMappedStatement, isKeyBefore(idInfo, globalKeyConfig));
return new SelectKeyGenerator(keyMappedStatement, idInfo.isBefore());
} }
@ -115,4 +118,40 @@ public class MybatisKeyGeneratorUtil {
return Arrays.asList(resultMap); 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;
}
}
} }

View File

@ -38,7 +38,7 @@ public class RowMapperInvoker {
public RowMapperInvoker(SqlSessionFactory sqlSessionFactory) { public RowMapperInvoker(SqlSessionFactory sqlSessionFactory) {
this.sqlSessionFactory = sqlSessionFactory; this.sqlSessionFactory = sqlSessionFactory;
this.dbType = FlexGlobalConfig.getConfig(sqlSessionFactory.getConfiguration().getEnvironment().getId()).getDbType(); this.dbType = FlexGlobalConfig.getConfig(sqlSessionFactory.getConfiguration()).getDbType();
} }
public RowSessionManager getRowSessionManager() { public RowSessionManager getRowSessionManager() {

View File

@ -23,7 +23,7 @@ public class IdInfo extends ColumnInfo {
/** /**
* id 生成策略 * id 生成策略
*/ */
private KeyType keyType; private KeyType keyType = KeyType.None;
/** /**
* keyType 类型是 sequence value 则代表的是 * keyType 类型是 sequence value 则代表的是
@ -40,7 +40,7 @@ public class IdInfo extends ColumnInfo {
* *
* @see org.apache.ibatis.executor.keygen.SelectKeyGenerator * @see org.apache.ibatis.executor.keygen.SelectKeyGenerator
*/ */
private boolean before; private Boolean before;
public IdInfo(ColumnInfo columnInfo) { public IdInfo(ColumnInfo columnInfo) {
@ -81,11 +81,11 @@ public class IdInfo extends ColumnInfo {
this.value = value; this.value = value;
} }
public boolean isBefore() { public Boolean getBefore() {
return before; return before;
} }
public void setBefore(boolean before) { public void setBefore(Boolean before) {
this.before = before; this.before = before;
} }
} }

View File

@ -228,7 +228,7 @@ public class TableInfo {
IdInfo idInfo = primaryKeyList.get(i); IdInfo idInfo = primaryKeyList.get(i);
primaryKeys[i] = idInfo.getColumn(); 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()); insertIdFields.add(idInfo.getColumn());
} }