mybatis/mp批量插入非自增主键数据

2023-09-21 14:30:46


前言

mybatis/mp 在实际开发中是常用的优秀持久层框架,但是在非自增主键的时候,单条数据插入式可以的,当批量插入的时候,如何做到填充主键呢?

这里的批量插入是指执行真正的批量插入!


一、mp的批量插入是假的

mp中细心的小伙伴会发现,批量插入是假的,以下是mybatis-plus的源码

    public boolean saveBatch(Collection<T> entityList, int batchSize) {
        String sqlStatement = this.getSqlStatement(SqlMethod.INSERT_ONE);
        return this.executeBatch(entityList, batchSize, (sqlSession, entity) -> {
            sqlSession.insert(sqlStatement, entity);
        });
    }

问题就出在这里: 循环调用的insert方法,不是insert into values

我想做的是做一个真正的批量执行,然后插入,但是问题是,当插入的时候,非自增主键没有被填充!!!

二、真正的批量插入

1.利用sql注入器处理

可以参考我的另一篇文章实现mybatis-plus 批量插入修改

2.采用自编码,编写xml批量执行

  1. 可以采用easycode生成器,选择最新修复版本,会自动生成xml批量插入
    easycode
  2. 直接手写

生成内容如下:

  1. mapper/dao
    /**
     * 批量新增数据(MyBatis原生foreach方法)
     *
     * @param entities List<Goods> 实例对象列表
     * @return 影响行数
     */
    int insertBatch(@Param("entities") List<Goods> entities);
  1. xml
    <!-- 批量插入 -->
    <insert id="insertBatch" keyProperty="id">
        insert into dbo.goods(id,name, code, price)
        values
        <foreach collection="entities" item="entity" separator=",">
        (#{entity.id},#{entity.name}, #{entity.code}, #{entity.price})
        </foreach>
    </insert>

这就能直接调用生成的 insertBatch 去执行真正的批量执行了!


三 问题

问题描述

虽然如上两种实现了批量插入,但是都有问题, 批量插入无法生成id,导致插入失败,因为主键不能为空

问题原因

经过不断 断点 跟踪mybatis执行,发现在 MybatisParameterHandler 中,如下代码有问题:

    private void process(Object parameter) {
        if (parameter != null) {
            TableInfo tableInfo = null;
            Object entity = parameter;
            if (parameter instanceof Map) {
                Map<?, ?> map = (Map)parameter;
                if (map.containsKey("et")) {
                    Object et = map.get("et");
                    if (et != null) {
                        entity = et;
                        tableInfo = TableInfoHelper.getTableInfo(et.getClass());
                    }
                }
            } else {
                tableInfo = TableInfoHelper.getTableInfo(parameter.getClass());
            }

            if (tableInfo != null) {
                MetaObject metaObject = this.configuration.newMetaObject(entity);
                if (SqlCommandType.INSERT == this.sqlCommandType) {
                    this.populateKeys(tableInfo, metaObject, entity);
                    this.insertFill(metaObject, tableInfo);
                } else {
                    this.updateFill(metaObject, tableInfo);
                }
            }
        }

    }

其中问题为:

	if (parameter instanceof Map) {
	    Map<?, ?> map = (Map)parameter;
	          if (map.containsKey("et")) {
	              Object et = map.get("et");
	              if (et != null) {
	                  entity = et;
	                  tableInfo = TableInfoHelper.getTableInfo(et.getClass());
	              }
	          }
      } else {
          tableInfo = TableInfoHelper.getTableInfo(parameter.getClass());
      }

此处 map.containsKey("et") 不对,因为我这里是批量插入,传入的不是et,就算我把参数名称改为et也不行
因为我是批量执行,这里获取到的应该是一个集合,list
但是这里是按照一个对象处理的 TableInfoHelper.getTableInfo(et.getClass())
所以一直获取不到tableInfo ,导致后续无法执行填充主键逻辑

问题解决

思路:

  1. 将MybatisParameterHandler 重新粘贴一份,然后修改上述的问题,增加判断逻辑,处理集合;
  2. 将当前的文件替换原有文件,使得mybatis执行的时候,走我这份文件即可(覆盖之前不兼容集合的文件)

粘贴一份,兼容集合

package com.baomidou.mybatisplus.core;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import com.baomidou.mybatisplus.core.toolkit.*;
import org.apache.ibatis.executor.ErrorContext;
import org.apache.ibatis.executor.parameter.ParameterHandler;
import org.apache.ibatis.mapping.*;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeException;
import org.apache.ibatis.type.TypeHandler;
import org.apache.ibatis.type.TypeHandlerRegistry;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.*;

/**
 * @author fulin
 * @since 2023/9/20 17:38
 */
public class MybatisParameterHandler implements ParameterHandler {

    private final TypeHandlerRegistry typeHandlerRegistry;
    private final MappedStatement mappedStatement;
    private final Object parameterObject;
    private final BoundSql boundSql;
    private final Configuration configuration;
    private final SqlCommandType sqlCommandType;

    public MybatisParameterHandler(MappedStatement mappedStatement, Object parameter, BoundSql boundSql) {
        this.typeHandlerRegistry = mappedStatement.getConfiguration().getTypeHandlerRegistry();
        this.mappedStatement = mappedStatement;
        this.boundSql = boundSql;
        this.configuration = mappedStatement.getConfiguration();
        this.sqlCommandType = mappedStatement.getSqlCommandType();
        this.parameterObject = processParameter(parameter);
    }

    public Object processParameter(Object parameter) {
        /* 只处理插入或更新操作 */
        if (parameter != null
                && (SqlCommandType.INSERT == this.sqlCommandType || SqlCommandType.UPDATE == this.sqlCommandType)) {
            //检查 parameterObject
            if (ReflectionKit.isPrimitiveOrWrapper(parameter.getClass())
                    || parameter.getClass() == String.class) {
                return parameter;
            }
            Collection<Object> parameters = getParameters(parameter);
            if (null != parameters) {
                // 感觉这里可以稍微优化一下,理论上都是同一个.
                parameters.forEach(this::process);
            } else {
                process(parameter);
            }
        }
        return parameter;
    }

    @Override
    public Object getParameterObject() {
        return this.parameterObject;
    }

    private void process(Object parameter) {
        if (parameter != null) {
            TableInfo tableInfo = null;
            Object entity = parameter;
            if (parameter instanceof Map) {
                Map<?, ?> map = (Map<?, ?>) parameter;
                if (map.containsKey(Constants.ENTITY)) {
                    Object et = map.get(Constants.ENTITY);
                    if (et != null) {
                        entity = et;
                        tableInfo = TableInfoHelper.getTableInfo(entity.getClass());
                    }
                }
                if (map.containsKey("entities")) {
                    List list = (List<Object>) map.get("entities");
                    if (CollectionUtils.isEmpty(list)) {
                        return;
                    }
                    Optional first = list.stream().findFirst();
                    if (!first.isPresent()) {
                        return;
                    }
                    entity = first.get();
                    tableInfo = TableInfoHelper.getTableInfo(entity.getClass());
                }
            } else {
                tableInfo = TableInfoHelper.getTableInfo(parameter.getClass());
            }
            if (tableInfo != null) {
                //到这里就应该转换到实体参数对象了,因为填充和ID处理都是争对实体对象处理的,不用传递原参数对象下去.
                MetaObject metaObject = this.configuration.newMetaObject(entity);
                if (SqlCommandType.INSERT == this.sqlCommandType) {
                    populateKeys(tableInfo, metaObject, entity);
                    insertFill(metaObject, tableInfo);
                } else {
                    updateFill(metaObject, tableInfo);
                }
            }
        }
    }


    protected void populateKeys(TableInfo tableInfo, MetaObject metaObject, Object entity) {
        final IdType idType = tableInfo.getIdType();
        final String keyProperty = tableInfo.getKeyProperty();
        if (StringUtils.isNotBlank(keyProperty) && null != idType && idType.getKey() >= 3) {
            final IdentifierGenerator identifierGenerator = GlobalConfigUtils.getGlobalConfig(this.configuration).getIdentifierGenerator();
            Object idValue = metaObject.getValue(keyProperty);
            if (StringUtils.checkValNull(idValue)) {
                if (idType.getKey() == IdType.ASSIGN_ID.getKey()) {
                    if (Number.class.isAssignableFrom(tableInfo.getKeyType())) {
                        metaObject.setValue(keyProperty, identifierGenerator.nextId(entity));
                    } else {
                        metaObject.setValue(keyProperty, identifierGenerator.nextId(entity).toString());
                    }
                } else if (idType.getKey() == IdType.ASSIGN_UUID.getKey()) {
                    metaObject.setValue(keyProperty, identifierGenerator.nextUUID(entity));
                }
            }
        }
    }


    protected void insertFill(MetaObject metaObject, TableInfo tableInfo) {
        GlobalConfigUtils.getMetaObjectHandler(this.configuration).ifPresent(metaObjectHandler -> {
            if (metaObjectHandler.openInsertFill()) {
                if (tableInfo.isWithInsertFill()) {
                    metaObjectHandler.insertFill(metaObject);
                } else {
                    // 兼容旧操作 id类型为input或none的要用填充器处理一下
                    if (metaObjectHandler.compatibleFillId()) {
                        String keyProperty = tableInfo.getKeyProperty();
                        if (StringUtils.isNotBlank(keyProperty)) {
                            Object value = metaObject.getValue(keyProperty);
                            if (value == null && (IdType.NONE == tableInfo.getIdType() || IdType.INPUT == tableInfo.getIdType())) {
                                metaObjectHandler.insertFill(metaObject);
                            }
                        }
                    }
                }
            }
        });
    }

    protected void updateFill(MetaObject metaObject, TableInfo tableInfo) {
        GlobalConfigUtils.getMetaObjectHandler(this.configuration).ifPresent(metaObjectHandler -> {
            if (metaObjectHandler.openUpdateFill() && tableInfo.isWithUpdateFill()) {
                metaObjectHandler.updateFill(metaObject);
            }
        });
    }

    /**
     * 处理正常批量插入逻辑
     * <p>
     * org.apache.ibatis.session.defaults.DefaultSqlSession$StrictMap 该类方法
     * wrapCollection 实现 StrictMap 封装逻辑
     * </p>
     *
     * @return 集合参数
     */
    @SuppressWarnings({"rawtypes", "unchecked"})
    protected Collection<Object> getParameters(Object parameterObject) {
        Collection<Object> parameters = null;
        if (parameterObject instanceof Collection) {
            parameters = (Collection) parameterObject;
        } else if (parameterObject instanceof Map) {
            Map parameterMap = (Map) parameterObject;
            if (parameterMap.containsKey("collection")) {
                parameters = (Collection) parameterMap.get("collection");
            } else if (parameterMap.containsKey("list")) {
                parameters = (List) parameterMap.get("list");
            } else if (parameterMap.containsKey("array")) {
                parameters = Arrays.asList((Object[]) parameterMap.get("array"));
            }
        }
        return parameters;
    }

    @Override
    @SuppressWarnings("unchecked")
    public void setParameters(PreparedStatement ps) {
        ErrorContext.instance().activity("setting parameters").object(this.mappedStatement.getParameterMap().getId());
        List<ParameterMapping> parameterMappings = this.boundSql.getParameterMappings();
        if (parameterMappings != null) {
            for (int i = 0; i < parameterMappings.size(); i++) {
                ParameterMapping parameterMapping = parameterMappings.get(i);
                if (parameterMapping.getMode() != ParameterMode.OUT) {
                    Object value;
                    String propertyName = parameterMapping.getProperty();
                    if (this.boundSql.hasAdditionalParameter(propertyName)) { // issue #448 ask first for additional params
                        value = this.boundSql.getAdditionalParameter(propertyName);
                    } else if (this.parameterObject == null) {
                        value = null;
                    } else if (this.typeHandlerRegistry.hasTypeHandler(this.parameterObject.getClass())) {
                        value = parameterObject;
                    } else {
                        MetaObject metaObject = this.configuration.newMetaObject(this.parameterObject);
                        value = metaObject.getValue(propertyName);
                    }
                    TypeHandler typeHandler = parameterMapping.getTypeHandler();
                    JdbcType jdbcType = parameterMapping.getJdbcType();
                    if (value == null && jdbcType == null) {
                        jdbcType = this.configuration.getJdbcTypeForNull();
                    }
                    try {
                        typeHandler.setParameter(ps, i + 1, value, jdbcType);
                    } catch (TypeException | SQLException e) {
                        throw new TypeException("Could not set parameters for mapping: " + parameterMapping + ". Cause: " + e, e);
                    }
                }
            }
        }
    }
}

这里面仅仅加了 如下这一段,其他完全复制

 if (map.containsKey("entities")) {
                    List list = (List<Object>) map.get("entities");
                    if (CollectionUtils.isEmpty(list)) {
                        return;
                    }
                    Optional first = list.stream().findFirst();
                    if (!first.isPresent()) {
                        return;
                    }
                    entity = first.get();
                    tableInfo = TableInfoHelper.getTableInfo(entity.getClass());
            }

替换原有文件

在本地项目中创建于mybatis中MybatisParameterHandler 同包目录,再将同名文件放入即可
同包同名放入

这样就可以通过本地文件,覆盖jar包中的文件了

总结

对于此次的问题,是因为之前一直用的是自增主键,今天改为非自增主键,导致该问题;

自增与非自增区别:
  1. 自增: 数据库层面的ID填充
  2. 非自增: 代码层面的数据ID填充,需要在插入之前就获取到ID,并填充到实体中

此次问题解决,对于mybatis执行流程也有了更加清晰的认知,与君共勉~
文中涉及所有代码: 代码地址

更多推荐

开心档之JavaScript 异步编程

JavaScript异步编程目录JavaScript异步编程异步的概念什么时候用异步编程回调函数实例实例实例异步AJAX实例实例异步的概念异步(Asynchronous,async)是与同步(Synchronous,sync)相对的概念。在我们学习的传统单线程编程中,程序的运行是同步的(同步不意味着所有步骤同时运行,而

云原生微服务治理 第五章 Spring Cloud Netflix 之 Ribbon

系列文章目录第一章Java线程池技术应用第二章CountDownLatch和Semaphone的应用第三章SpringCloud简介第四章SpringCloudNetflix之Eureka第四章SpringCloudNetflix之Ribbon文章目录系列文章目录@[TOC](文章目录)前言1、负载均衡1.1、服务端负

Python in Visual Studio Code 2023年9月更新

作者:CourtneyWebster-ProgramManager,PythonExtensioninVisualStudioCode排版:AlanWang我们很高兴地宣布VisualStudioCode的Python和Jupyter扩展将于2023年9月发布!此版本包括以下内容:•将Python的“Recreate”

uniapp----微信小程序 日历组件(周日历&& 月日历)【Vue3+ts+uView】

uniapp----微信小程序日历组件(周日历&&月日历)【Vue3+ts+uView】用Vue3+ts+uView来编写日历组件;存在周日历和月日历两种显示方式;高亮显示当天日期,红点渲染有数据的日期,点击显示数据1.calendar-week-mouth组件代码<template><viewclass="calen

虹科分享 | 谷歌Vertex AI平台使用Redis搭建大语言模型

文章来源:虹科云科技点此阅读原文基础模型和高性能数据层这两个基本组件始终是创建高效、可扩展语言模型应用的关键,利用Redis搭建大语言模型,能够实现高效可扩展的语义搜索、检索增强生成、LLM缓存机制、LLM记忆和持久化。有Redis加持的大语言模型可应用于文档检索、虚拟购物助手、客户服务助理等,为企业带来益处。一、语言

服务器的架构有哪些

服务器的架构有哪些1、单体架构软件设计经典的3层模型是表现层,业务逻辑层,数据访问层。典型的单体架构就是将所有的业务场景的表现层,业务逻辑层,数据访问层放在一个工程中最终经过编译,打包,部署在一台服务器上。2、垂直架构垂直架构是将一个大项目,按照业务场景纵向拆分为互不相干的单体架构的项目。3、前后端分离前后端分离是横向

近年来国内室内定位领域硕士论文选题的现状与趋势

目录一、前言二、选题的目的和意义三、选题现状分析四、选题趋势分析一、前言本博文采用了图表统计法分析了近5年来100余篇高被引室内定位领域硕士论文选题的现状,并从选题现状中得出了该领域选题的大致趋势。本文还通过分析该领域硕士毕业论文选题的现状和趋势,对未来该领域选题提出了自己的见解和展望。二、选题的目的和意义无论是大学生

成为威胁:网络安全中的动手威胁模拟案例

不断变化的网络威胁形势要求组织为其网络安全团队配备必要的技能来检测、响应和防御恶意攻击。然而,在研究中发现并继续探索的最令人惊讶的事情是,欺骗当前的网络安全防御是多么容易。防病毒程序建立在庞大的签名数据库之上,只需更改程序内的文本这样简单的操作就很容易崩溃。这同样适用于网络签名以及端点检测和响应。防御技术主要关注某些行

区块链安全,哈希函数暴露的攻击向量与对策

区块链安全,哈希函数暴露的攻击向量与对策简介LengthExtensionAttack是一种与某些特定类型的哈希函数(如MD5,SHA-1和SHA-2)的特性有关的攻击。简单来说,这种攻击利用了一个事实,即知道H(message)和message的长度,我们可以轻松计算出H(message||padding||exte

QTday3

#include"widget.h"Widget::Widget(QWidget*parent):QWidget(parent){this->setFixedSize(600,450);//将窗口固定大小this->setWindowIcon(QIcon(":/wodepeizhenshi.png"));//设置窗口图

驱动开发---基于gpio子系统编写LED灯的驱动

一、GPIO子系统相关API1.解析GPIO相关的设备树节点structdevice_node*of_find_node_by_path(constchar*path)功能:根据设备树节点路径解析设备树节点信息参数:path:设备树所在的节点路径/mynode@0X12345678返回值:成功返回目标节点首地址,失败返

热文推荐