解决Mybatis Plus使用insertBatchSomeColumn批量插入Null字段非空问题[终版]

一、场景

  1. Mybatis Plus默认提供了insertBatchSomeColumn选装件
  2. 当批量插入的PO对象是NULL值,且数据库字段是NotNull且有默认值时就会报Value Not Null异常

二、解决思路

  1. 在代码生成器时对PO对象赋予默认值
  2. 在BaseServiceImpl实现类中对PO对象值为Null,数据库字段NotNull且有默认的值字段自动设置默认值

三、实现

3.1 代码生成器解决方法

  1. 重写AutoGenerator
/*

package com.taco.springcloud.generate.config;

import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableLogic;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.annotation.Version;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.builder.ConfigBuilder;
import com.baomidou.mybatisplus.generator.config.po.TableField;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine;
import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine;
import lombok.AccessLevel;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 生成文件
 *
 * @author YangHu, tangguo, hubin
 * @since 2016-08-30
 */
@Data
@Accessors(chain = true)
public class AutoGeneratorHelper {
    private static final Logger logger = LoggerFactory.getLogger(AutoGeneratorHelper.class);

    /**
     * 配置信息
     */
    protected ConfigBuilder config;
    /**
     * 注入配置
     */
    @Getter(AccessLevel.NONE)
    @Setter(AccessLevel.NONE)
    protected InjectionConfig injectionConfig;
    /**
     * 数据源配置
     */
    private DataSourceConfig dataSource;
    /**
     * 数据库表配置
     */
    private StrategyConfig strategy;
    /**
     * 包 相关配置
     */
    private PackageConfig packageInfo;
    /**
     * 模板 相关配置
     */
    private TemplateConfig template;
    /**
     * 全局 相关配置
     */
    private GlobalConfig globalConfig;
    /**
     * 模板引擎
     */
    private AbstractTemplateEngine templateEngine;

    /**
     * 生成代码
     */
    public void execute() {
        logger.debug("==========================准备生成文件...==========================");
        // 初始化配置
        if (null == config) {
            config = new ConfigBuilder(packageInfo, dataSource, strategy, template, globalConfig);
            if (null != injectionConfig) {
                injectionConfig.setConfig(config);
            }
        }

        processTableFieldDefaultValue();
		
        if (null == templateEngine) {
            // 为了兼容之前逻辑,采用 Velocity 引擎 【 默认 】
            templateEngine = new VelocityTemplateEngine();
        }
        // 模板引擎初始化执行文件输出
        templateEngine.init(this.pretreatmentConfigBuilder(config)).mkdirs().batchOutput().open();
        logger.debug("==========================文件生成完成!!!==========================");
    }

    private void processTableFieldDefaultValue() {
        for (TableInfo tableInfo : config.getTableInfoList()) {
            for (TableField field : tableInfo.getFields()) {
                Map<String, Object> customMap = field.getCustomMap();
                DbColumnType columnType = (DbColumnType) field.getColumnType();

                String defaultValue = (String) customMap.get("Default");
                if (Objects.isNull(defaultValue)) {
                    continue;
                }
                Object val = null;
                switch (columnType) {
                    case BYTE:
                    case SHORT:
                    case CHARACTER:
                    case INTEGER:
                        val = defaultValue;
                        break;
                    case LONG:
                        val = defaultValue + "L";
                        break;
                    case FLOAT:
                        val = defaultValue + "F";
                        break;
                    case DOUBLE:
                        val = defaultValue + "D";
                        break;
                    case BOOLEAN:
                        val = "Boolean.valueOf(\"" + defaultValue + "\")";
                        break;
                    case STRING:
                        val = "\"" + defaultValue + "\"";
                        break;
                    case LOCAL_DATE:
                        if ("CURRENT_TIMESTAMP".equals(defaultValue)) {
                            val = "LocalDate.now()";
                            break;
                        }
                        val = "LocalDateTime.parse(\""+ defaultValue + "\", DateTimeFormatter.ofPattern("yyyy-MM-dd")).toLocalDate()";
                        break;
                    case LOCAL_TIME:
                        if ("CURRENT_TIMESTAMP".equals(defaultValue)) {
                            val = "LocalTime.now()";
                            break;
                        }
                        val = "LocalDateTime.parse(\""+ defaultValue + "\", DateTimeFormatter.ofPattern("HH:mm:ss")).toLocalTime()";
                        break;
                    case LOCAL_DATE_TIME:
                        if ("CURRENT_TIMESTAMP".equals(defaultValue)) {
                            val = "LocalDateTime.now()";
                            break;
                        }
                        val = "LocalDateTime.parse(\""+ defaultValue + "\", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))";
                        break;
                    case DATE:
                        val = "new Date()";
                        break;
                    case BIG_INTEGER:
                        val = "new BigInteger(\"" + defaultValue + "\")";
                        break;
                    case BIG_DECIMAL:
                        val = "new BigDecimal(\"" + defaultValue + "\")";
                        break;
                }

                customMap.put("Default", val);
                customMap.put("DefaultValueFieldName", "DEFAULT_" + CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, field.getPropertyName()));
            }

        }
    }

    /**
     * 开放表信息、预留子类重写
     *
     * @param config 配置信息
     * @return ignore
     */
    protected List<TableInfo> getAllTableInfoList(ConfigBuilder config) {
        return config.getTableInfoList();
    }

    /**
     * 预处理配置
     *
     * @param config 总配置信息
     * @return 解析数据结果集
     */
    protected ConfigBuilder pretreatmentConfigBuilder(ConfigBuilder config) {
        /*
         * 注入自定义配置
         */
        if (null != injectionConfig) {
            injectionConfig.initMap();
            config.setInjectionConfig(injectionConfig);
        }
        /*
         * 表信息列表
         */
        List<TableInfo> tableList = this.getAllTableInfoList(config);
        for (TableInfo tableInfo : tableList) {
            /* ---------- 添加导入包 ---------- */
            if (config.getGlobalConfig().isActiveRecord()) {
                // 开启 ActiveRecord 模式
                tableInfo.setImportPackages(Model.class.getCanonicalName());
            }
            if (tableInfo.isConvert()) {
                // 表注解
                tableInfo.setImportPackages(TableName.class.getCanonicalName());
            }
            if (config.getStrategyConfig().getLogicDeleteFieldName() != null && tableInfo.isLogicDelete(config.getStrategyConfig().getLogicDeleteFieldName())) {
                // 逻辑删除注解
                tableInfo.setImportPackages(TableLogic.class.getCanonicalName());
            }
            if (StringUtils.isNotBlank(config.getStrategyConfig().getVersionFieldName())) {
                // 乐观锁注解
                tableInfo.setImportPackages(Version.class.getCanonicalName());
            }
            boolean importSerializable = true;
            if (StringUtils.isNotBlank(config.getSuperEntityClass())) {
                // 父实体
                tableInfo.setImportPackages(config.getSuperEntityClass());
                importSerializable = false;
            }
            if (config.getGlobalConfig().isActiveRecord()) {
                importSerializable = true;
            }
            if (importSerializable) {
                tableInfo.setImportPackages(Serializable.class.getCanonicalName());
            }
            // Boolean类型is前缀处理
            if (config.getStrategyConfig().isEntityBooleanColumnRemoveIsPrefix()
                && CollectionUtils.isNotEmpty(tableInfo.getFields())) {
                List<TableField> tableFields = tableInfo.getFields().stream().filter(field -> "boolean".equalsIgnoreCase(field.getPropertyType()))
                    .filter(field -> field.getPropertyName().startsWith("is")).collect(Collectors.toList());
                tableFields.forEach(field -> {
                    //主键为is的情况基本上是不存在的.
                    if (field.isKeyFlag()) {
                        tableInfo.setImportPackages(TableId.class.getCanonicalName());
                    } else {
                        tableInfo.setImportPackages(com.baomidou.mybatisplus.annotation.TableField.class.getCanonicalName());
                    }
                    field.setConvert(true);
                    field.setPropertyName(StringUtils.removePrefixAfterPrefixToLower(field.getPropertyName(), 2));
                });
            }
        }
        return config.setTableInfoList(tableList);
    }

    public InjectionConfig getCfg() {
        return injectionConfig;
    }

    public AutoGeneratorHelper setCfg(InjectionConfig injectionConfig) {
        this.injectionConfig = injectionConfig;
        return this;
    }
}

  1. 修改配置

	AutoGeneratorHelper autoGenerator = new AutoGeneratorHelper();
	//...
        DataSourceConfig dataSourceConfig = getDataSourceConfig(generateDTO);
        dataSourceConfig.setDbQuery(new MySqlQuery() {
            @Override
            public String[] fieldCustom() {
                return new String[]{"Default"};
            }
        });
	//...
	autoGenerator.setDataSource(dataSourceConfig);
  1. 编写Entity模板
    #if(${field.customMap.Default})
        #if(!${field.fill})
    @TableField(fill = FieldFill.INSERT)
        #end
    private ${field.propertyType} ${field.propertyName};
	public static final ${field.propertyType} ${field.customMap.DefaultValueFieldName} = ${field.customMap.Default};
    #end
    #if(!${field.customMap.Default})
    private ${field.propertyType} ${field.propertyName};
    #end
  1. DAO层
public interface CommonMapper<T> extends BaseMapper<T> {

    int insertBatchSomeColumn(List<T> entityList);
}

  1. BaseServiceImpl对Null值处理
public class CommonServiceImpl<M extends CommonMapper<T>, T> extends ServiceImpl<M, T> {

    private static ConcurrentMap<String, List<Field>> defaultValueFields = new ConcurrentHashMap<>();
    private static ConcurrentMap<Field, Object> defaultFieldValue = new ConcurrentHashMap<>();
    private static final int BATCH_SIZE = 1000;
	private static final String DEFAULT_VALUE_PREFIX = "DEFAULT_";

    @Transactional(rollbackFor = Exception.class)
    public boolean fastSaveBatch(List<T> list, int batchSize) {
        if(CollectionUtils.isEmpty(list)) {
            return true;
        }

        processDefaultValue(list);
        batchSize = batchSize < 1 ? BATCH_SIZE : batchSize;

        if(list.size() <= batchSize) {
            return retBool(baseMapper.insertBatchSomeColumn(list));
        }

        for (int fromIdx = 0 , endIdx = batchSize ; ; fromIdx += batchSize, endIdx += batchSize) {
            if(endIdx > list.size()) {
                endIdx = list.size();
            }
            baseMapper.insertBatchSomeColumn(list.subList(fromIdx, endIdx));
            if(endIdx == list.size()) {
                return true;
            }
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public boolean fastSaveBatch(List<T> list) {
        return fastSaveBatch(list, BATCH_SIZE);
    }

    private void processDefaultValue(List<T> list) {
        try {
             Class<T> clz = (Class<T>) list.get(0).getClass();
            List<Field> fields = getFilDefaultFields(clz);

            for (T t1 : list) {
                for (Field field : fields) {
                    ReflectionUtils.makeAccessible(field);
                    Object value = ReflectionUtils.getField(field, t1);
                    if (Objects.isNull(value)) {
                        Object fieldDefaultValue = getFieldDefaultValue(clz, field);
                        if (Objects.nonNull(fieldDefaultValue)) {
                            ReflectionUtils.setField(field, t1, fieldDefaultValue);
                        }
                    }
                }
            }
        }catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new RuntimeException(e);
        }
    }

    private List<Field> getFilDefaultFields(Class<T> clz) {
        List<Field> fields = defaultValueFields.get(clz.getName());
        if (Objects.isNull(fields)) {
            fields = Stream.of(clz.getDeclaredFields())
                    .filter(o -> {
                        TableField annotation = o.getAnnotation(TableField.class);
                        return Objects.nonNull(annotation) && FieldFill.INSERT == annotation.fill();
                    }).collect(Collectors.toList());
            defaultValueFields.putIfAbsent(clz.getName(), fields);
        }
        return fields;
    }

    private Object getFieldDefaultValue(Class<T> clz, Field field) throws IllegalAccessException, InstantiationException {
        Object value = defaultFieldValue.get(field);

        if (Objects.isNull(value)) {
            T t = clz.newInstance();
            String name = DEFAULT_VALUE_PREFIX + CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, field.getName());
            Field defaultValueField = ReflectionUtils.findField(clz, name);
            if (Objects.isNull(defaultValueField)) {
                throw new RuntimeException("can not find field:" + field.getName() + "default value");
            }
            value = ReflectionUtils.getField(defaultValueField, t);
            if (Objects.nonNull(value)) {
                defaultFieldValue.putIfAbsent(field, value);
            }
        }
        return value;
    }
}

四、优化

  1. 针对反射的赋值取值操作可以用并行流、多线程等方式提升性能、或者可以参考Mapstruct生成检测空值和赋值操作
  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值