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 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);
}