mybatisPlus custom batch addition

The new methods at the bottom of mybatisPlus are added one by one. Today, customize the batch adding methods.
Create a custom data method injection class

/**
 * @Description: EasySqlInjector 自定义数据方法注入
 * @Author WangYejian
 * @Date: 2020/11/4 14:34
 */
public class EasySqlInjector extends DefaultSqlInjector {
    
    

    @Override
    public List<AbstractMethod> getMethodList(Class<?> mapperClass) {
    
    
        //防止父类方法不可用
        List<AbstractMethod> methods= super.getMethodList(mapperClass);
        methods.add(new InsertBatchSomeColumn());
        return methods;
    }
}

Add customization in mybatisplus configuration file MybatisPlusConfig

@Bean
    public EasySqlInjector easySqlInjector() {
    
    
        return new EasySqlInjector();
    }

Create EasyBaseMapper to extend Universal Mapper

package com.cgmcomm.mallplus.basic.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;

import java.util.Collection;

/**
 * @Description: EasyBaseMapper 扩展通用 Mapper,支持数据批量插入
 * @Author WangYejian
 * @Date: 2020/10/15 18:57
 */
public interface EasyBaseMapper<T> extends BaseMapper<T> {
    
    

    /**
     * 批量插入 仅适用于mysql
     * @param entityList 实体列表
     * @return 影响行数
     */
    Integer insertBatchSomeColumn(Collection<T> entityList);
}
**
 * 定义业务mapper接口,继承刚刚扩展的EasyBaseMapper
 *
 * @Author WangYejian
 * @Date: 2020/10/15 19:02
 */
@Mapper
public interface TestMapper extends EasyBaseMapper<Test> {
    
    
}

/**
 * 业务实现类接口,即可引用
 */
@Service
public class TestServiceImpl extends ServiceImpl<TestMapper, Test> implements TestService {
    
    
    @Override
    public Integer testBatch(Collection<Test> testList) {
    
    
        return baseMapper.insertBatchSomeColumn(testList);
    }

Guess you like

Origin blog.csdn.net/weixin_45528650/article/details/109497757