|
@@ -1,24 +1,11 @@
|
|
|
-/*
|
|
|
- * Copyright (c) 2011-2020, hubin (jobob@qq.com).
|
|
|
- * <p>
|
|
|
- * Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
|
|
- * use this file except in compliance with the License. You may obtain a copy of
|
|
|
- * the License at
|
|
|
- * <p>
|
|
|
- * http://www.apache.org/licenses/LICENSE-2.0
|
|
|
- * <p>
|
|
|
- * Unless required by applicable law or agreed to in writing, software
|
|
|
- * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
- * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
- * License for the specific language governing permissions and limitations under
|
|
|
- * the License.
|
|
|
- */
|
|
|
package com.baomidou.mybatisplus.extension.injector.methods.additional;
|
|
|
|
|
|
import com.baomidou.mybatisplus.annotation.IdType;
|
|
|
import com.baomidou.mybatisplus.core.enums.SqlMethod;
|
|
|
import com.baomidou.mybatisplus.core.injector.AbstractMethod;
|
|
|
+import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
|
|
|
import com.baomidou.mybatisplus.core.metadata.TableInfo;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.Assert;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.TableInfoHelper;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils;
|
|
@@ -28,37 +15,53 @@ import org.apache.ibatis.executor.keygen.NoKeyGenerator;
|
|
|
import org.apache.ibatis.mapping.MappedStatement;
|
|
|
import org.apache.ibatis.mapping.SqlSource;
|
|
|
|
|
|
+import java.util.function.Predicate;
|
|
|
+
|
|
|
/**
|
|
|
- * <p> 批量新增数据,全字段 insert </p>
|
|
|
+ * <p> 批量新增数据,自选字段 insert </p>
|
|
|
* <p> 不同的数据库支持度不一样!!! 只在 mysql 下测试过!!! 只在 mysql 下测试过!!! 只在 mysql 下测试过!!! </p>
|
|
|
- * <p> 除了主键是 <strong> 数据库自增的 </strong> 理论上都可以使用!!! </p>
|
|
|
+ * <p> 除了主键是 <strong> 数据库自增的未测试 </strong> 外理论上都可以使用!!! </p>
|
|
|
* <p> 如果你使用自增有报错或主键值无法回写到entity,就不要跑来问为什么了,因为我也不知道!!! </p>
|
|
|
* <p>
|
|
|
* 自己的通用 mapper 如下使用:
|
|
|
- * int insertBatchAllColumn(List<T> entityList);
|
|
|
+ * int insertBatchSomeColumn(List<T> entityList);
|
|
|
+ *
|
|
|
+ * <li> 注意1: 不要加任何注解 !! </li>
|
|
|
+ * <li> 注意2: 自选字段 insert !!,如果个别字段在 entity 里为 null 但是数据库中有配置默认值, insert 后数据库字段是为 null 而不是默认值 </li>
|
|
|
*
|
|
|
- * <li> 注意1: 不要加任何注解!!! </li>
|
|
|
- * <li> 注意2: 是所有字段insert,如果个别字段在entity里为null但是数据库中有配置默认值,insert后数据库字段是为null而不是默认值 </li>
|
|
|
+ * <p>
|
|
|
+ * 常用的构造入参:
|
|
|
* </p>
|
|
|
*
|
|
|
+ * <li> 例1: new InsertBatchSomeColumn(t -> true) , 表示用于全字段 </li>
|
|
|
+ * <li> 例2: new InsertBatchSomeColumn(t -> !t.isLogicDelete()) , 表示非逻辑删除字段外全字段 </li>
|
|
|
+ * <li> 例3: new InsertBatchSomeColumn(t -> t.getFieldFill() != FieldFill.UPDATE) , 表示填充策略为 UPDATE 外的全字段 </li>
|
|
|
+ *
|
|
|
* @author miemie
|
|
|
- * @since 2018-11-09
|
|
|
+ * @since 2018-11-29
|
|
|
*/
|
|
|
@SuppressWarnings("all")
|
|
|
-public class InsertBatchAllColumn extends AbstractMethod {
|
|
|
+public class InsertBatchSomeColumn extends AbstractMethod {
|
|
|
|
|
|
/**
|
|
|
* mapper 对应的方法名
|
|
|
*/
|
|
|
- private static final String MAPPER_METHOD = "insertBatchAllColumn";
|
|
|
+ private static final String MAPPER_METHOD = "insertBatchSomeColumn";
|
|
|
+
|
|
|
+ private Predicate<TableFieldInfo> predicate;
|
|
|
+
|
|
|
+ public InsertBatchSomeColumn(Predicate<TableFieldInfo> predicate) {
|
|
|
+ Assert.notNull(predicate, "this predicate can not be null !");
|
|
|
+ this.predicate = predicate;
|
|
|
+ }
|
|
|
|
|
|
@Override
|
|
|
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
|
|
|
KeyGenerator keyGenerator = new NoKeyGenerator();
|
|
|
SqlMethod sqlMethod = SqlMethod.INSERT_ONE;
|
|
|
- String insertSqlColumn = tableInfo.getAllInsertSqlColumn(true);
|
|
|
+ String insertSqlColumn = tableInfo.getSomeInsertSqlColumn(predicate);
|
|
|
String columnScript = LEFT_BRACKET + insertSqlColumn.substring(0, insertSqlColumn.length() - 1) + RIGHT_BRACKET;
|
|
|
- String insertSqlProperty = tableInfo.getAllInsertSqlProperty(true, ENTITY_DOT);
|
|
|
+ String insertSqlProperty = tableInfo.getSomeInsertSqlProperty(ENTITY_DOT, predicate);
|
|
|
insertSqlProperty = LEFT_BRACKET + insertSqlProperty.substring(0, insertSqlProperty.length() - 1) + RIGHT_BRACKET;
|
|
|
String valuesScript = SqlScriptUtils.convertForeach(insertSqlProperty, "list", null, ENTITY, COMMA);
|
|
|
String keyProperty = null;
|