A background.

Recent updates to the Mybatis - Plus framework have made our base development even better. One of the basic add, delete, change check, code generator must be everyone with that call a cool. I am in use, also encountered some pits. Such as savebatch saveorupdatebatch, look at this is not a batch of new, a batch of new or updated, look at the API for development, I also feel very good. After developing a test, the speed is snail's pace, which is unbearable for large data volumes. Found on the console, how is the nominal batch insertion, or a one by one insertion, no wonder the speed is slow.

Ii. Solutions

There are generally two solutions for accessing online information:

(1). Write SQL statements by myself using MYbatis XML. One drawback of this method is that if the table has more fields, there are dozens of fields, write batch add, batch add modify SQL statement is a nightmare.

INSERT INTO t 
    (id, age) 
VALUES 
    (3.28),
    (4.29) 
ON DUPLICATE KEY UPDATE
    id = VALUES(id),
    age = VALUES(age);
Copy the code

(2) Mybatis - Plus added a new SQL injector, through the SQL injector can achieve batch add, batch add modify function. One injection, ready to use, extremely convenient to use. The disadvantage is that when the project is started, SQL injector registration will be performed, which slightly affects the startup speed.

Add dependencies

<! Mybatis Plus Extension includes mybatis Plus Core.
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-extension</artifactId>
    <version>3.4.3.4</version>
</dependency>
Copy the code

Inherit default method injection

import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.injector.DefaultSqlInjector;
import com.baomidou.mybatisplus.extension.injector.methods.InsertBatchSomeColumn;
import java.util.List;

public class EasySqlInjector extends DefaultSqlInjector {

    @Override
  
    public List<AbstractMethod> getMethodList(Class
        mapperClass, TableInfo tableInfo) {
        List<AbstractMethod> methodList = super.getMethodList(mapperClass, tableInfo);
        methodList.add(newInsertBatchSomeColumn(i -> i.getFieldFill() ! = FieldFill.UPDATE));returnmethodList; }}Copy the code

Inject beans into the MybatisPlusConfig configuration file

@Configuration
@EnableTransactionManagement
public class MybatisPlusConfig {

    /** ** paging plug-in */
// @Bean
// public PaginationInterceptor paginationInterceptor() {
// PaginationInterceptor page = new PaginationInterceptor();
// page.setDialectType("mysql");
// return new PaginationInterceptor();
/ /}

    / / the latest version
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor(a) {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }

    @Bean
    public MetaObjectHandler metaObjectHandler(a) {
        return new MyMetaObjectHandler();
    }

    @Bean
    public GlobalConfig globalConfiguration(a) {
        GlobalConfig conf = new GlobalConfig();
        // Custom injection needs to be configured here
        conf.setSqlInjector(easySqlInjector());
        return conf;
    }

    @Bean
    public EasySqlInjector easySqlInjector(a) {
        return new EasySqlInjector();
    }
Copy the code

The extension comes with BaseMapper

Mapper package under the new EasyBaseMapper interface, extension with BaseMapper

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import java.util.List;

public interface EasyBaseMapper<T> extends BaseMapper<T> {

    /** * Batch insert only applies to mysql *@paramEntityList Indicates the list of entities@returnAffects the number of rows */
    Integer insertBatchSomeColumn(List<T> entityList);
}
Copy the code

Vii. Business realization

Mapper inheritancepublic interface MonitorReportRecordMapper extends EasyBaseMapper<MonitorReportRecord> {}Copy the code

The service layer using

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinchen.admin.modules.monitoranalysis.mapper.MonitorReportRecordMapper;
import com.xinchen.api.monitoranalysis.entity.MonitorReportRecord;
import com.xinchen.api.utils.DateUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

@Service
public class MonitorReportRecordService extends ServiceImpl<MonitorReportRecordMapper.MonitorReportRecord> {

    @Transactional(rollbackFor = Exception.class)
    public void updateMonitorRecord(List<MonitorReportRecord> insertList) {
        this.baseMapper.insertBatchSomeColumn(insertList); }}Copy the code