注入器
package com.ruoyi.framework.interceptor; import com.baomidou.mybatisplus.core.injector.AbstractMethod; import com.baomidou.mybatisplus.core.injector.DefaultSqlInjector; import com.baomidou.mybatisplus.extension.injector.methods.InsertBatchSomeColumn; import java.util.List; /** * @description: sql注入器 * @create: 2024-07-12 17:25 **/ public class InsertBatchSqlInjector extends DefaultSqlInjector { @Override public List<AbstractMethod> getMethodList(Class<?> mapperClass) { List<AbstractMethod> methodList = super.getMethodList(mapperClass); methodList.add(new InsertBatchSomeColumn()); return methodList; } }
添加配置
package com.ruoyi.framework.config; import com.baomidou.mybatisplus.annotation.DbType; import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; import com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor; import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor; import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor; import com.ruoyi.common.utils.StringUtils; import com.ruoyi.framework.interceptor.InsertBatchSqlInjector; import org.apache.ibatis.io.VFS; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.boot.autoconfigure.SpringBootVFS; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.env.Environment; import org.springframework.core.io.DefaultResourceLoader; import org.springframework.core.io.Resource; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.core.io.support.ResourcePatternResolver; import org.springframework.core.type.classreading.CachingMetadataReaderFactory; import org.springframework.core.type.classreading.MetadataReader; import org.springframework.core.type.classreading.MetadataReaderFactory; import org.springframework.transaction.annotation.EnableTransactionManagement; import org.springframework.util.ClassUtils; import javax.sql.DataSource; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; import java.util.List; /** * Mybatis Plus 配置 * * @author ruoyi */ @EnableTransactionManagement(proxyTargetClass = true) @Configuration public class MybatisPlusConfig { /** * sql注入器 */ @Bean public InsertBatchSqlInjector insertBatchSqlInjector() { return new InsertBatchSqlInjector(); } }
新增一个MyBaseMapper
package com.ruoyi.system.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import java.util.List; import org.apache.ibatis.annotations.Param; /** * @description: * @create: 2024-07-12 17:32 **/ public interface MyBaseMapper<T> extends BaseMapper<T> { // 批量插入 int insertBatchSomeColumn(@Param("list") List<T> batchList); }
然后继续MyBaseMapper就可以用了
@Mapper
public interface CouponEntityMapper extends MyBaseMapper<CouponEntity>
使用泛型做成公共接口
@Transactional(rollbackFor = Exception.class)
public <T> void batchInsert(List<T> entityList, MyBaseMapper<T> baseMapper) {
int batchSize = 1000; // 每批次插入的记录数 for (int i = 0; i < entityList.size(); i += batchSize) { List<T> batchList = entityList.subList(i, Math.min(i + batchSize, entityList.size())); baseMapper.insertBatchSomeColumn(batchList); } }
标签:core,mybatisPlus,批量,sql,springframework,org,import,com,mybatisplus From: https://www.cnblogs.com/jiutang001/p/18303247