Skip to content

Commit

Permalink
删除老的 InsertBatchAllColumn.java 选装件,提供新的 InsertBatchSomeColumn.java 选装件
Browse files Browse the repository at this point in the history
  • Loading branch information
miemieYaho committed Nov 29, 2018
1 parent b64bb3c commit efb67a7
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ public class Insert extends AbstractMethod {
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
KeyGenerator keyGenerator = new NoKeyGenerator();
SqlMethod sqlMethod = SqlMethod.INSERT_ONE;
String columnScript = SqlScriptUtils.convertTrim(tableInfo.getAllInsertSqlColumn(false),
String columnScript = SqlScriptUtils.convertTrim(tableInfo.getAllInsertSqlColumnMaybeIf(),
LEFT_BRACKET, RIGHT_BRACKET, null, COMMA);
String valuesScript = SqlScriptUtils.convertTrim(tableInfo.getAllInsertSqlProperty(false, null),
String valuesScript = SqlScriptUtils.convertTrim(tableInfo.getAllInsertSqlPropertyMaybeIf(null),
LEFT_BRACKET, RIGHT_BRACKET, null, COMMA);
String keyProperty = null;
String keyColumn = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,18 @@
*/
package com.baomidou.mybatisplus.core.metadata;

import java.lang.reflect.Field;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.FieldStrategy;
import com.baomidou.mybatisplus.annotation.SqlCondition;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableLogic;
import com.baomidou.mybatisplus.annotation.*;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.TableInfoHelper;
import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils;
import com.baomidou.mybatisplus.core.toolkit.sql.SqlUtils;

import lombok.AccessLevel;
import lombok.Getter;

import java.lang.reflect.Field;

/**
* <p>
* 数据库表字段反射信息
Expand All @@ -49,11 +43,6 @@ public class TableFieldInfo implements Constants {
* true: 表示要进行 as
*/
private final boolean related;
/**
* 是否进行 select 查询
* 大字段可设置为 false 不加入 select 查询范围
*/
private boolean select = true;
/**
* 字段名
*/
Expand All @@ -78,6 +67,15 @@ public class TableFieldInfo implements Constants {
* 字段策略【 默认,自判断 null 】
*/
private final FieldStrategy fieldStrategy;
/**
* 标记该字段属于哪个类
*/
private final Class<?> clazz;
/**
* 是否进行 select 查询
* 大字段可设置为 false 不加入 select 查询范围
*/
private boolean select = true;
/**
* 逻辑删除值
*/
Expand All @@ -98,10 +96,6 @@ public class TableFieldInfo implements Constants {
* 字段填充策略
*/
private FieldFill fieldFill = FieldFill.DEFAULT;
/**
* 标记该字段属于哪个类
*/
private final Class<?> clazz;
/**
* 缓存 sql select
*/
Expand Down Expand Up @@ -249,12 +243,27 @@ public String getSqlSelect(DbType dbType) {
* insert into table (字段) values (值)
* 位于 "值" 部位
*
* <li> 不生成 if 标签 </li>
*
* @return sql 脚本片段
*/
public String getInsertSqlProperty(boolean isAll, final String prefix) {
public String getInsertSqlProperty(final String prefix) {
final String newPrefix = prefix == null ? EMPTY : prefix;
String sqlScript = SqlScriptUtils.safeParam(newPrefix + el) + COMMA;
if (isAll || fieldFill == FieldFill.INSERT || fieldFill == FieldFill.INSERT_UPDATE) {
return SqlScriptUtils.safeParam(newPrefix + el) + COMMA;
}

/**
* 获取 insert 时候插入值 sql 脚本片段
* insert into table (字段) values (值)
* 位于 "值" 部位
*
* <li> 根据规则会生成 if 标签 </li>
*
* @return sql 脚本片段
*/
public String getInsertSqlPropertyMaybeIf(final String prefix) {
String sqlScript = getInsertSqlProperty(prefix);
if (fieldFill == FieldFill.INSERT || fieldFill == FieldFill.INSERT_UPDATE) {
return sqlScript;
}
return convertIf(sqlScript, property);
Expand All @@ -265,11 +274,26 @@ public String getInsertSqlProperty(boolean isAll, final String prefix) {
* insert into table (字段) values (值)
* 位于 "字段" 部位
*
* <li> 不生成 if 标签 </li>
*
* @return sql 脚本片段
*/
public String getInsertSqlColumn() {
return column + COMMA;
}

/**
* 获取 insert 时候字段 sql 脚本片段
* insert into table (字段) values (值)
* 位于 "字段" 部位
*
* <li> 根据规则会生成 if 标签 </li>
*
* @return sql 脚本片段
*/
public String getInsertSqlColumn(boolean isAll) {
final String sqlScript = column + COMMA;
if (isAll || fieldFill == FieldFill.INSERT || fieldFill == FieldFill.INSERT_UPDATE) {
public String getInsertSqlColumnMaybeIf() {
final String sqlScript = getInsertSqlColumn();
if (fieldFill == FieldFill.INSERT || fieldFill == FieldFill.INSERT_UPDATE) {
return sqlScript;
}
return convertIf(sqlScript, property);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,13 +198,13 @@ public String chooseSelect(Predicate<TableFieldInfo> predicate) {
*
* @return sql 脚本片段
*/
public String getKeyInsertSqlProperty(final String prefix) {
public String getKeyInsertSqlProperty(final String prefix, final boolean newLine) {
final String newPrefix = prefix == null ? EMPTY : prefix;
if (StringUtils.isNotEmpty(keyProperty)) {
if (idType == IdType.AUTO) {
return EMPTY;
}
return SqlScriptUtils.safeParam(newPrefix + keyProperty) + COMMA + NEWLINE;
return SqlScriptUtils.safeParam(newPrefix + keyProperty) + COMMA + (newLine ? NEWLINE : EMPTY);
}
return EMPTY;
}
Expand All @@ -216,39 +216,73 @@ public String getKeyInsertSqlProperty(final String prefix) {
*
* @return sql 脚本片段
*/
public String getKeyInsertSqlColumn() {
public String getKeyInsertSqlColumn(final boolean newLine) {
if (StringUtils.isNotEmpty(keyColumn)) {
if (idType == IdType.AUTO) {
return EMPTY;
}
return keyColumn + COMMA + NEWLINE;
return keyColumn + COMMA + (newLine ? NEWLINE : EMPTY);
}
return EMPTY;
}


/**
* 根据 predicate 过滤后获取 insert 时候插入值 sql 脚本片段
* insert into table (字段) values (值)
* 位于 "值" 部位
*
* <li> 自选部位,不生成 if 标签 </li>
*
* @return sql 脚本片段
*/
public String getSomeInsertSqlProperty(final String prefix, Predicate<TableFieldInfo> predicate) {
final String newPrefix = prefix == null ? EMPTY : prefix;
return getKeyInsertSqlProperty(newPrefix, false) + fieldList.stream()
.filter(predicate).map(i -> i.getInsertSqlProperty(newPrefix)).collect(joining(EMPTY));
}

/**
* 根据 predicate 过滤后获取 insert 时候字段 sql 脚本片段
* insert into table (字段) values (值)
* 位于 "字段" 部位
*
* <li> 自选部位,不生成 if 标签 </li>
*
* @return sql 脚本片段
*/
public String getSomeInsertSqlColumn(Predicate<TableFieldInfo> predicate) {
return getKeyInsertSqlColumn(false) + fieldList.stream().filter(predicate)
.map(TableFieldInfo::getInsertSqlColumn).collect(joining(EMPTY));
}


/**
* 获取所有 insert 时候插入值 sql 脚本片段
* insert into table (字段) values (值)
* 位于 "值" 部位
*
* <li> 自动选部位,根据规则会生成 if 标签 </li>
*
* @return sql 脚本片段
*/
public String getAllInsertSqlProperty(boolean isAll, final String prefix) {
public String getAllInsertSqlPropertyMaybeIf(final String prefix) {
final String newPrefix = prefix == null ? EMPTY : prefix;
return getKeyInsertSqlProperty(newPrefix) + fieldList.stream()
.map(i -> i.getInsertSqlProperty(isAll, newPrefix)).collect(joining(NEWLINE));
return getKeyInsertSqlProperty(newPrefix, true) + fieldList.stream()
.map(i -> i.getInsertSqlPropertyMaybeIf(newPrefix)).collect(joining(NEWLINE));
}

/**
* 获取 insert 时候字段 sql 脚本片段
* insert into table (字段) values (值)
* 位于 "字段" 部位
*
* <li> 自动选部位,根据规则会生成 if 标签 </li>
*
* @return sql 脚本片段
*/
public String getAllInsertSqlColumn(boolean isAll) {
return getKeyInsertSqlColumn() + fieldList.stream().map(i -> i.getInsertSqlColumn(isAll))
public String getAllInsertSqlColumnMaybeIf() {
return getKeyInsertSqlColumn(true) + fieldList.stream().map(TableFieldInfo::getInsertSqlColumnMaybeIf)
.collect(joining(NEWLINE));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ public void other() {
public void testTableInfoHelper() {
TableInfo info = TableInfoHelper.initTableInfo(null, Xx.class);
System.out.println("----------- AllInsertSqlColumn -----------");
System.out.println(info.getAllInsertSqlColumn(false));
System.out.println(info.getAllInsertSqlColumnMaybeIf());
System.out.println("----------- AllInsertSqlProperty -----------");
System.out.println(info.getAllInsertSqlProperty(false, null));
System.out.println(info.getAllInsertSqlPropertyMaybeIf(null));
System.out.println("----------- AllSqlSet -----------");
System.out.println(info.getAllSqlSet(true, "ew.entity."));
System.out.println("----------- AllSqlWhere -----------");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,11 @@
/*
* Copyright (c) 2011-2020, hubin ([email protected]).
* <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;
Expand All @@ -28,37 +15,53 @@
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ public interface MyBaseMapper<T> extends BaseMapper<T> {

int deleteByIdWithFill(T entity);

int insertBatchAllColumn(List<T> entityList);
int insertBatchSomeColumn(List<T> entityList);
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ public void a2_insertBatch() {
commonDataList.add(new CommonData().setTestInt(i).setTestEnum(TestEnum.TWO).setTestStr(i + "条"));
commonLogicDataList.add(new CommonLogicData().setTestInt(i).setTestStr(i + "条"));
}
Assert.assertEquals(9, mysqlMapper.insertBatchAllColumn(mysqlDataList));
Assert.assertEquals(9, commonMapper.insertBatchAllColumn(commonDataList));
Assert.assertEquals(9, commonLogicMapper.insertBatchAllColumn(commonLogicDataList));
Assert.assertEquals(9, mysqlMapper.insertBatchSomeColumn(mysqlDataList));
Assert.assertEquals(9, commonMapper.insertBatchSomeColumn(commonDataList));
Assert.assertEquals(9, commonLogicMapper.insertBatchSomeColumn(commonLogicDataList));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.baomidou.mybatisplus.test.mysql.config;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.parser.ISqlParser;
import com.baomidou.mybatisplus.extension.injector.LogicSqlInjector;
import com.baomidou.mybatisplus.extension.injector.methods.additional.InsertBatchAllColumn;
import com.baomidou.mybatisplus.extension.injector.methods.additional.InsertBatchSomeColumn;
import com.baomidou.mybatisplus.extension.injector.methods.additional.LogicDeleteByIdWithFill;
import com.baomidou.mybatisplus.extension.plugins.OptimisticLockerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
Expand Down Expand Up @@ -74,7 +75,8 @@ public GlobalConfig globalConfig() {
public List<AbstractMethod> getMethodList() {
List<AbstractMethod> methodList = super.getMethodList();
methodList.add(new LogicDeleteByIdWithFill());
methodList.add(new InsertBatchAllColumn());
methodList.add(new InsertBatchSomeColumn(t -> !(t.getFieldFill() == FieldFill.UPDATE
|| t.isLogicDelete() || t.getProperty().equals("version"))));
return methodList;
}
};
Expand Down

0 comments on commit efb67a7

Please sign in to comment.