Mybatis Plus 真正批量插入

、Mybatis Plus默认批量插入saveBatch方法在IService中,是使用同一个sqlSession,这相比遍历集合循环insert来说有一定的性能提升,但是这并不是sql层面真正的批量插入。
在这里插入图片描述
、jdbc添加rewriteBatchedStatements=true 无法改变本质

、真正批量插入

继承DefaultSqlInjector自定义sql注入器

public class MySqlInjector extends DefaultSqlInjector {

    @Override
    public List<AbstractMethod> getMethodList(Class<?> mapperClass, TableInfo tableInfo) {
        List<AbstractMethod> methodList = super.getMethodList(mapperClass, tableInfo);
        //更新时自动填充的字段,不用插入值
        methodList.add(new InsertBatchSomeColumn(i -> i.getFieldFill() != FieldFill.UPDATE));
        return methodList;
    }

}

将自定义的sql注入器注入到Mybatis容器中

@Configuration
public class MybatisPlusConfig {
	@Bean
    public MySqlInjector sqlInjector() {
        return new MySqlInjector();
    }
}

继承 BaseMapper 添加自定义方法

public interface CommonMapper<T> extends BaseMapper<T> {
    /**
     * 真正的批量插入
     * @param entityList
     * @return
     */
    int insertBatchSomeColumn(List<T> entityList);

}

Mapper继承CommonMapper接口,调用insertBatchSomeColumn方法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值