// 基于 springboot环境
@Bean
public BatchSaveSqlInjector batchSaveSqlInjector() {
return new BatchSaveSqlInjector();
}
// 批量插入mapper
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import java.util.Collection;
public interface BatchSaveMapper<T> extends BaseMapper<T> {
/**
* 批量插入
*
* @param entityList 实体列表
* @return 影响行数
*/
Integer insertBatchSomeColumn(Collection<T> entityList);
}
// 批量插入注入方法
import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.injector.DefaultSqlInjector;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.extension.injector.methods.InsertBatchSomeColumn;
import java.util.List;
/**
* @author zsq
* @Description: 支持自定义SQL注入方法
* @date 2023-08-22
*/
public class BatchSaveSqlInjector extends DefaultSqlInjector {
@Override
public List<AbstractMethod> getMethodList(Class<?> mapperClass, TableInfo tableInfo) {
// 获取父类SQL注入方法列表
List<AbstractMethod> methodList = super.getMethodList(mapperClass, tableInfo);
// 将批量插入方法添加进去
methodList.add(new InsertBatchSomeColumn());
return methodList;
}
}
MybatisPlus批量插入Mapper实现
本文介绍了在SpringBoot环境中如何使用BatchSaveSqlInjector进行批量插入操作,包括Mapper接口的定义、自定义的insertBatchSomeColumn方法以及如何扩展DefaultSqlInjector以支持自定义SQL注入。
摘要由CSDN通过智能技术生成