|
@@ -17,13 +17,16 @@ package com.baomidou.mybatisplus.extension.injector.methods.additional;
|
|
|
|
|
|
import com.baomidou.mybatisplus.annotation.IdType;
|
|
import com.baomidou.mybatisplus.annotation.IdType;
|
|
import com.baomidou.mybatisplus.core.enums.SqlMethod;
|
|
import com.baomidou.mybatisplus.core.enums.SqlMethod;
|
|
-import com.baomidou.mybatisplus.core.injector.ChooseTableField;
|
|
|
|
|
|
+import com.baomidou.mybatisplus.core.injector.AbstractMethod;
|
|
import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
|
|
import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
|
|
import com.baomidou.mybatisplus.core.metadata.TableInfo;
|
|
import com.baomidou.mybatisplus.core.metadata.TableInfo;
|
|
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
|
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
|
import com.baomidou.mybatisplus.core.toolkit.TableInfoHelper;
|
|
import com.baomidou.mybatisplus.core.toolkit.TableInfoHelper;
|
|
import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils;
|
|
import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils;
|
|
|
|
+import lombok.AllArgsConstructor;
|
|
import lombok.NoArgsConstructor;
|
|
import lombok.NoArgsConstructor;
|
|
|
|
+import lombok.Setter;
|
|
|
|
+import lombok.experimental.Accessors;
|
|
import org.apache.ibatis.executor.keygen.Jdbc3KeyGenerator;
|
|
import org.apache.ibatis.executor.keygen.Jdbc3KeyGenerator;
|
|
import org.apache.ibatis.executor.keygen.KeyGenerator;
|
|
import org.apache.ibatis.executor.keygen.KeyGenerator;
|
|
import org.apache.ibatis.executor.keygen.NoKeyGenerator;
|
|
import org.apache.ibatis.executor.keygen.NoKeyGenerator;
|
|
@@ -33,8 +36,6 @@ import org.apache.ibatis.mapping.SqlSource;
|
|
import java.util.List;
|
|
import java.util.List;
|
|
import java.util.function.Predicate;
|
|
import java.util.function.Predicate;
|
|
|
|
|
|
-import static java.util.stream.Collectors.joining;
|
|
|
|
-
|
|
|
|
/**
|
|
/**
|
|
* 批量新增数据,自选字段 insert
|
|
* 批量新增数据,自选字段 insert
|
|
* <p> 不同的数据库支持度不一样!!! 只在 mysql 下测试过!!! 只在 mysql 下测试过!!! 只在 mysql 下测试过!!! </p>
|
|
* <p> 不同的数据库支持度不一样!!! 只在 mysql 下测试过!!! 只在 mysql 下测试过!!! 只在 mysql 下测试过!!! </p>
|
|
@@ -61,7 +62,8 @@ import static java.util.stream.Collectors.joining;
|
|
* @since 2018-11-29
|
|
* @since 2018-11-29
|
|
*/
|
|
*/
|
|
@NoArgsConstructor
|
|
@NoArgsConstructor
|
|
-public class InsertBatchSomeColumn extends ChooseTableField<InsertBatchSomeColumn> {
|
|
|
|
|
|
+@AllArgsConstructor
|
|
|
|
+public class InsertBatchSomeColumn extends AbstractMethod {
|
|
|
|
|
|
/**
|
|
/**
|
|
* mapper 对应的方法名
|
|
* mapper 对应的方法名
|
|
@@ -69,12 +71,11 @@ public class InsertBatchSomeColumn extends ChooseTableField<InsertBatchSomeColum
|
|
private static final String MAPPER_METHOD = "insertBatchSomeColumn";
|
|
private static final String MAPPER_METHOD = "insertBatchSomeColumn";
|
|
|
|
|
|
/**
|
|
/**
|
|
- * @deprecated please to use {@link #addPredicate(Predicate)}
|
|
|
|
|
|
+ * 字段筛选条件
|
|
*/
|
|
*/
|
|
- @Deprecated
|
|
|
|
- public InsertBatchSomeColumn(Predicate<TableFieldInfo> predicate) {
|
|
|
|
- this.addPredicate(predicate);
|
|
|
|
- }
|
|
|
|
|
|
+ @Setter
|
|
|
|
+ @Accessors(chain = true)
|
|
|
|
+ private Predicate<TableFieldInfo> predicate;
|
|
|
|
|
|
@SuppressWarnings("Duplicates")
|
|
@SuppressWarnings("Duplicates")
|
|
@Override
|
|
@Override
|
|
@@ -82,11 +83,11 @@ public class InsertBatchSomeColumn extends ChooseTableField<InsertBatchSomeColum
|
|
KeyGenerator keyGenerator = new NoKeyGenerator();
|
|
KeyGenerator keyGenerator = new NoKeyGenerator();
|
|
SqlMethod sqlMethod = SqlMethod.INSERT_ONE;
|
|
SqlMethod sqlMethod = SqlMethod.INSERT_ONE;
|
|
List<TableFieldInfo> fieldList = tableInfo.getFieldList();
|
|
List<TableFieldInfo> fieldList = tableInfo.getFieldList();
|
|
- String insertSqlColumn = tableInfo.getKeyInsertSqlColumn(false) + this.filters(fieldList)
|
|
|
|
- .map(TableFieldInfo::getInsertSqlColumn).collect(joining(EMPTY));
|
|
|
|
|
|
+ String insertSqlColumn = tableInfo.getKeyInsertSqlColumn(false) +
|
|
|
|
+ this.filterTableFieldInfo(fieldList, predicate, TableFieldInfo::getInsertSqlColumn, EMPTY);
|
|
String columnScript = LEFT_BRACKET + insertSqlColumn.substring(0, insertSqlColumn.length() - 1) + RIGHT_BRACKET;
|
|
String columnScript = LEFT_BRACKET + insertSqlColumn.substring(0, insertSqlColumn.length() - 1) + RIGHT_BRACKET;
|
|
- String insertSqlProperty = tableInfo.getKeyInsertSqlProperty(ENTITY_DOT, false) + this.filters(fieldList)
|
|
|
|
- .map(i -> i.getInsertSqlProperty(ENTITY_DOT)).collect(joining(EMPTY));
|
|
|
|
|
|
+ String insertSqlProperty = tableInfo.getKeyInsertSqlProperty(ENTITY_DOT, false) +
|
|
|
|
+ this.filterTableFieldInfo(fieldList, predicate, i -> i.getInsertSqlProperty(ENTITY_DOT), EMPTY);
|
|
insertSqlProperty = LEFT_BRACKET + insertSqlProperty.substring(0, insertSqlProperty.length() - 1) + RIGHT_BRACKET;
|
|
insertSqlProperty = LEFT_BRACKET + insertSqlProperty.substring(0, insertSqlProperty.length() - 1) + RIGHT_BRACKET;
|
|
String valuesScript = SqlScriptUtils.convertForeach(insertSqlProperty, "list", null, ENTITY, COMMA);
|
|
String valuesScript = SqlScriptUtils.convertForeach(insertSqlProperty, "list", null, ENTITY, COMMA);
|
|
String keyProperty = null;
|
|
String keyProperty = null;
|
|
@@ -94,7 +95,7 @@ public class InsertBatchSomeColumn extends ChooseTableField<InsertBatchSomeColum
|
|
// 表包含主键处理逻辑,如果不包含主键当普通字段处理
|
|
// 表包含主键处理逻辑,如果不包含主键当普通字段处理
|
|
if (StringUtils.isNotEmpty(tableInfo.getKeyProperty())) {
|
|
if (StringUtils.isNotEmpty(tableInfo.getKeyProperty())) {
|
|
if (tableInfo.getIdType() == IdType.AUTO) {
|
|
if (tableInfo.getIdType() == IdType.AUTO) {
|
|
- /** 自增主键 */
|
|
|
|
|
|
+ /* 自增主键 */
|
|
keyGenerator = new Jdbc3KeyGenerator();
|
|
keyGenerator = new Jdbc3KeyGenerator();
|
|
keyProperty = tableInfo.getKeyProperty();
|
|
keyProperty = tableInfo.getKeyProperty();
|
|
keyColumn = tableInfo.getKeyColumn();
|
|
keyColumn = tableInfo.getKeyColumn();
|