Mybatiplus自定义sql注入器实现批量插入

需求:

读取存有10条数据的Excel文件,批量插入数据库

1.Excel文件写入10万条数据

 /**
     * Excel 写入10万条数据
     */
    @Test
    public void repeatedWrite() {
        String fileName = "/Users/chongfayi/Desktop/1676305898919.xlsx";
        try (ExcelWriter excelWriter = EasyExcel.write(fileName, DemoData.class).build()) {
            WriteSheet writeSheet = EasyExcel.writerSheet("模板").build();
            for (int i = 0; i < 100000; i++) {
                List data = new ArrayList<>();
                data.add(new DemoData(IdUtil.randomUUID(), "张三", "XXX大学"));
                excelWriter.write(data, writeSheet);
            }
        }
    }

2.使用Mybatiplus封装的saveBatch插入,用时3595ms,发现其实还是一条一条sql循环插入的,是伪批量

@Test
    public void simpleRead() {
        TimeInterval interval = new TimeInterval();
        List userList = new ArrayList<>();
        String fileName = "/Users/chongfayi/Desktop/1676305898919.xlsx";
        EasyExcel.read(fileName, DemoData.class, new ReadListener() {
            @Override
            public void invoke(DemoData demoData, AnalysisContext context) {
                userList.add(new User(null, demoData.getUuid(), 2, demoData.getSchool()));

            @Override
            public void doAfterAllAnalysed(AnalysisContext context) {
            }
        }).sheet().doRead();
        mybatisPlusUserService.saveBatch(userList);
        System.out.println("mybatisPlus自带saveBatch插入20万条用时:" + interval.interval() + "ms");

    }

3.使用自定义sql注入器实现批量插入

package com.example.demo.mybatisplus;

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.metadata.TableInfoHelper;
import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils;
import lombok.Setter;
import lombok.experimental.Accessors;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.executor.keygen.Jdbc3KeyGenerator;
import org.apache.ibatis.executor.keygen.KeyGenerator;
import org.apache.ibatis.executor.keygen.NoKeyGenerator;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlSource;

import java.util.List;
import java.util.function.Predicate;

/**
 * @author chongfayi
 * 自定义mybatisPlus批量插入方法
 */
public class InsertBatchMethod extends AbstractMethod {

    /**
     * 字段筛选条件
     */
    @Setter
    @Accessors(chain = true)
    private Predicate predicate;

    @Override
    public MappedStatement injectMappedStatement(Class mapperClass, Class modelClass, TableInfo tableInfo) {
        KeyGenerator keyGenerator = new NoKeyGenerator();
        SqlMethod sqlMethod = SqlMethod.INSERT_ONE;
        List fieldList = tableInfo.getFieldList();
        String insertSqlColumn = tableInfo.getKeyInsertSqlColumn(false) +
                this.filterTableFieldInfo(fieldList, predicate, TableFieldInfo::getInsertSqlColumn, EMPTY);
        String columnScript = LEFT_BRACKET + insertSqlColumn.substring(0, insertSqlColumn.length() - 1) + RIGHT_BRACKET;
        String insertSqlProperty = tableInfo.getKeyInsertSqlProperty(ENTITY_DOT, false) +
                this.filterTableFieldInfo(fieldList, predicate, i -> i.getInsertSqlProperty(ENTITY_DOT), EMPTY);
        insertSqlProperty = LEFT_BRACKET + insertSqlProperty.substring(0, insertSqlProperty.length() - 1) + RIGHT_BRACKET;
        String valuesScript = SqlScriptUtils.convertForeach(insertSqlProperty, "list", null, ENTITY, COMMA);
        String keyProperty = null;
        String keyColumn = null;
        // 表包含主键处理逻辑,如果不包含主键当普通字段处理
        if (StringUtils.isNotBlank(tableInfo.getKeyProperty())) {
            if (tableInfo.getIdType() == IdType.AUTO) {
                /* 自增主键 */
                keyGenerator = new Jdbc3KeyGenerator();
                keyProperty = tableInfo.getKeyProperty();
                keyColumn = tableInfo.getKeyColumn();
            } else {
                if (null != tableInfo.getKeySequence()) {
                    keyGenerator = TableInfoHelper.genKeyGenerator(getMethod(sqlMethod), tableInfo, builderAssistant);
                    keyProperty = tableInfo.getKeyProperty();
                    keyColumn = tableInfo.getKeyColumn();
                }
            }
        }
        String sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), columnScript, valuesScript);
        SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
        return this.addInsertMappedStatement(mapperClass, modelClass, getMethod(sqlMethod), sqlSource, keyGenerator, keyProperty, keyColumn);
    }

    @Override
    public String getMethod(SqlMethod sqlMethod) {
        // 自定义 mapper 方法名
        return "insertBatch";
    }

}
package com.example.demo.mybatisplus;

import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.injector.DefaultSqlInjector;

import java.util.List;

/**
 * @author chongfayi
 * mybatisPlus自定义sql注入器
 */
public class CustomizedSqlInjector extends DefaultSqlInjector {

    @Override
    public List getMethodList(Class mapperClass) {
        List methodList = super.getMethodList(mapperClass);
        methodList.add(new InsertBatchMethod());
        methodList.add(new UpdateBatchMethod());
        return methodList;
    }


}
package com.example.demo.mybatisplus;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author chongfayi
 * 自定义mybatisPlus配置类
 */
@Configuration
public class MybatisPlusConfig {

    @Bean
    public CustomizedSqlInjector customizedSqlInjector() {
        return new CustomizedSqlInjector();
    }
}
package com.example.demo.mybatisplus;

import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlSource;


public class UpdateBatchMethod extends AbstractMethod {
    @Override
    public MappedStatement injectMappedStatement(Class mapperClass, Class modelClass, TableInfo tableInfo) {
        String sql = "";
        String additional = tableInfo.isWithVersion() ? tableInfo.getVersionFieldInfo().getVersionOli("item", "item.") : "" + tableInfo.getLogicDeleteSql(true, true);
        String setSql = sqlSet(tableInfo.isWithLogicDelete(), false, tableInfo, false, "item", "item.");
        String sqlResult = String.format(sql, tableInfo.getTableName(), setSql, tableInfo.getKeyColumn(), "item." + tableInfo.getKeyProperty(), additional);
        SqlSource sqlSource = languageDriver.createSqlSource(configuration, sqlResult, modelClass);
        // 第三个参数必须和RootMapper的自定义方法名一致
        return this.addUpdateMappedStatement(mapperClass, modelClass, "updateBatch", sqlSource);
    }

}
package com.example.demo.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;

import java.util.List;

public interface RootMapper extends BaseMapper {


    /**
     * 自定义批量插入
     * 如果要自动填充,@Param(xx) xx参数名必须是 list/collection/array 3个的其中之一
     */
    int insertBatch(@Param("list") List list);

    /**
     * 自定义批量更新,条件为主键
     * 如果要自动填充,@Param(xx) xx参数名必须是 list/collection/array 3个的其中之一
     */
    int updateBatch(@Param("list") List list);

}
package com.example.demo.service;

import com.User;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.mapper.UserMapper;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MybatisPlusUserServiceImpl extends ServiceImpl implements MybatisPlusUserService {


    @Override
    public int saveAll(List userList) {
        return baseMapper.insertBatch(userList);
    }

    @Override
    public int updateAll(List userList) {
        return baseMapper.updateBatch(userList);
    }
}

进行测试,用时1773ms

@Test
    public void simpleRead() {
        TimeInterval interval = new TimeInterval();
        //List userList = new ArrayList<>();
        List userList1 = new ArrayList<>();
        String fileName = "/Users/chongfayi/Desktop/1676305898919.xlsx";
        EasyExcel.read(fileName, DemoData.class, new ReadListener() {
            @Override
            public void invoke(DemoData demoData, AnalysisContext context) {
                //userList.add(new User(null, demoData.getUuid(), 2, demoData.getSchool()));
                userList1.add(new User(null, demoData.getUuid(), 2, demoData.getSchool()));
            }

            @Override
            public void doAfterAllAnalysed(AnalysisContext context) {
            }
        }).sheet().doRead();
        //mybatisPlusUserService.saveBatch(userList);
        //System.out.println("mybatisPlus自带saveBatch插入20万条用时:" + interval.interval() + "ms");
        interval.restart();
        mybatisPlusUserService.saveAll(userList1);
        System.out.println("mybatisPlus-sql注入器插入20万条用时:" + interval.interval() + "ms");
    }

你可能感兴趣的:(Mybatis,sql,java,数据库)