mybatisPlus底层的新增方法是一条一条的新增的,今天自定义批量新增方法。
创建自定义数据方法注入类

/**
 * @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;
  }
}

在mybatisplus配置文件MybatisPlusConfig加入自定义

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

创建EasyBaseMapper 扩展通用 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 天开易想
 */
@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);
  }
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。