|
@@ -16,10 +16,13 @@
|
|
|
package com.baomidou.mybatisplus.generator.index;
|
|
|
|
|
|
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper;
|
|
|
import com.baomidou.mybatisplus.extension.kotlin.KtUpdateWrapper;
|
|
|
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
|
|
|
+import com.baomidou.mybatisplus.generator.config.StrategyConfig;
|
|
|
+import com.baomidou.mybatisplus.generator.config.builder.Entity;
|
|
|
import com.baomidou.mybatisplus.generator.config.po.TableField;
|
|
|
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
|
|
|
import com.baomidou.mybatisplus.generator.jdbc.DatabaseMetaDataWrapper;
|
|
@@ -35,28 +38,47 @@ import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
* 使用Lambda方式生成索引方法
|
|
|
+ * <p>复合索引下,第一个字段不会判空,后续字段会进行判空处理,也就是只能保证第一个字段不传递空,无法解决掉索引中间项传递为空的情况</p>
|
|
|
+ * <p>由于需求不一样,默认只处理单字段索引,如果默认复合索引的方案符合你的要求,你可以考虑{@link #singleIndex}设置成false</p>
|
|
|
*
|
|
|
* @author nieqiurong
|
|
|
* @since 3.5.10
|
|
|
*/
|
|
|
public class DefaultGenerateMapperLambdaMethodHandler extends AbstractMapperMethodHandler {
|
|
|
|
|
|
+ /**
|
|
|
+ * 只生成单索引字段方法(默认true)
|
|
|
+ * <p>当设置为true时,代表会过滤掉复合索引</p>
|
|
|
+ */
|
|
|
+ private final boolean singleIndex;
|
|
|
+
|
|
|
+ public DefaultGenerateMapperLambdaMethodHandler() {
|
|
|
+ this(true);
|
|
|
+ }
|
|
|
+
|
|
|
+ public DefaultGenerateMapperLambdaMethodHandler(boolean singleIndex) {
|
|
|
+ this.singleIndex = singleIndex;
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public List<MapperMethod> getMethodList(TableInfo tableInfo) {
|
|
|
Map<String, List<DatabaseMetaDataWrapper.Index>> indexlistMap = tableInfo.getIndexList().stream()
|
|
|
.collect(Collectors.groupingBy(DatabaseMetaDataWrapper.Index::getName));
|
|
|
String entityName = tableInfo.getEntityName();
|
|
|
GlobalConfig globalConfig = tableInfo.getGlobalConfig();
|
|
|
+ StrategyConfig strategyConfig = tableInfo.getStrategyConfig();
|
|
|
+ Entity entity = strategyConfig.entity();
|
|
|
Set<Map.Entry<String, List<DatabaseMetaDataWrapper.Index>>> entrySet = indexlistMap.entrySet();
|
|
|
List<MapperMethod> methodList = new ArrayList<>();
|
|
|
for (Map.Entry<String, List<DatabaseMetaDataWrapper.Index>> entry : entrySet) {
|
|
|
String indexName = entry.getKey();
|
|
|
List<DatabaseMetaDataWrapper.Index> indexList = entry.getValue();
|
|
|
int indexSize = indexList.size();
|
|
|
- if ("PRIMARY".equals(indexName)) {
|
|
|
- if (indexSize == 1) {
|
|
|
- continue;
|
|
|
- }
|
|
|
+ if (this.singleIndex && indexSize > 1) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if ("PRIMARY".equals(indexName) && indexSize == 1) {
|
|
|
+ continue;
|
|
|
}
|
|
|
Map<String, TableField> tableFieldMap = tableInfo.getTableFieldMap();
|
|
|
StringBuilder baseMethodNameBuilder = new StringBuilder();
|
|
@@ -70,6 +92,10 @@ public class DefaultGenerateMapperLambdaMethodHandler extends AbstractMapperMeth
|
|
|
uniqueKey = true;
|
|
|
}
|
|
|
TableField tableField = tableFieldMap.get(index.getColumnName());
|
|
|
+ if (index.getColumnName().equals(entity.getLogicDeleteColumnName())
|
|
|
+ || tableField.getPropertyName().equals(entity.getLogicDeletePropertyName())) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
tableFieldList.add(tableField);
|
|
|
baseMethodNameBuilder.append(tableField.getCapitalName());
|
|
|
if (indexSize > 1) {
|
|
@@ -96,8 +122,11 @@ public class DefaultGenerateMapperLambdaMethodHandler extends AbstractMapperMeth
|
|
|
argsBuilder.append(", ");
|
|
|
}
|
|
|
}
|
|
|
- boolean returnList = (indexSize > 1 || !uniqueKey);
|
|
|
String baseMethodName = baseMethodNameBuilder.toString();
|
|
|
+ if (StringUtils.isBlank(baseMethodName)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ boolean returnList = (indexSize > 1 || !uniqueKey);
|
|
|
String args = argsBuilder.toString();
|
|
|
String baseWrapper = baseWrapperBuilder.toString();
|
|
|
if (globalConfig.isKotlin()) {
|
|
@@ -142,8 +171,12 @@ public class DefaultGenerateMapperLambdaMethodHandler extends AbstractMapperMeth
|
|
|
public Set<String> getImportPackages(TableInfo tableInfo) {
|
|
|
GlobalConfig globalConfig = tableInfo.getGlobalConfig();
|
|
|
Set<String> imports = new HashSet<>();
|
|
|
- imports.add(ObjectUtils.class.getName());
|
|
|
- imports.add(List.class.getName());
|
|
|
+ if (!singleIndex) {
|
|
|
+ imports.add(ObjectUtils.class.getName());
|
|
|
+ }
|
|
|
+ if (!globalConfig.isKotlin()) {
|
|
|
+ imports.add(List.class.getName());
|
|
|
+ }
|
|
|
if (globalConfig.isKotlin()) {
|
|
|
imports.add(KtQueryWrapper.class.getName());
|
|
|
imports.add(KtUpdateWrapper.class.getName());
|