1.新建MybatisPlusConfig
/*** MybatisPlusConfig.*/
@Configuration
@MapperScan("com.test.mapper")
public class MybatisPlusConfig {/*** 自定义批量插入 SQL 注入器.*/@Beanpublic InsertBatchSqlInjector insertBatchSqlInjector() {return new InsertBatchSqlInjector();}}
2.新建sql注入器InsertBatchSqlInjector
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;/*** 批量插入 SQL 注入器.**/
public class InsertBatchSqlInjector extends DefaultSqlInjector {@Overridepublic List<AbstractMethod> getMethodList(Class<?> mapperClass, TableInfo tableInfo) {// super.getMethodList() 保留 Mybatis Plus 自带的方法List<AbstractMethod> methodList = super.getMethodList(mapperClass, tableInfo);// 添加自定义方法:批量插入,方法名为 insertBatchSomeColumnmethodList.add(new InsertBatchSomeColumn());return methodList;}
}
3.新建MyBaseMapper
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import java.util.List;
import org.springframework.data.repository.query.Param;/*** 新增批量插入.*/
public interface MyBaseMapper<T> extends BaseMapper<T> {// 批量插入int insertBatchSomeColumn(@Param("list") List<T> batchList);
}
4.继承MyBaseMapper
/*** TestMapper.*/
public interface TestMapper extends MyBaseMapper<User> {
}
5.service引入TestMapper就可以批量插入了