创建sql注入器
package com.shinow.qiaoyun.cloud.portal.config.database.mybatis; import com.baomidou.mybatisplus.annotation.FieldFill; 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; public class EasySqlInjector extends DefaultSqlInjector { @Override public List<AbstractMethod> getMethodList(Class<?> mapperClass, TableInfo tableInfo) { // 注意:此SQL注入器继承了DefaultSqlInjector(默认注入器),调用了DefaultSqlInjector的getMethodList方法,保留了mybatis-plus的自带方法 List<AbstractMethod> methodList = super.getMethodList(mapperClass, tableInfo); methodList.add(new InsertBatchSomeColumn(i -> i.getFieldFill() != FieldFill.UPDATE)); return methodList; } }
注入配置
package com.shinow.qiaoyun.cloud.portal.config.database.mybatis; 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 org.mybatis.spring.annotation.MapperScan; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class MyBatisPlusConfig { @Bean public MybatisPlusInterceptor mybatisPlusInterceptor() { MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.POSTGRE_SQL)); interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor()); interceptor.addInnerInterceptor(new BlockAttackInnerInterceptor()); return interceptor; } @Bean public EasySqlInjector sqlInjector() { return new EasySqlInjector(); } }
创建mapper(方法名必须为 insertBatchSomeColumn)
import com.baomidou.mybatisplus.core.mapper.BaseMapper; import java.util.Collection; public interface EasyBaseMapper<T> extends BaseMapper<T> { /** * 批量插入 仅适用于mysql * * @param entityList 实体列表 * @return 影响行数 */ Integer insertBatchSomeColumn(Collection<T> entityList); }
完成以上步骤即可使用
使用方式 集成 EasyBaseMapper
import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.shinow.qiaoyun.cloud.portal.system.entity.system.AuthorizationRelationship; import org.apache.ibatis.annotations.Mapper; import java.util.Collection; @Mapper public interface AuthorizationRelationshipMapper extends EasyBaseMapper<AuthorizationRelationship> { @Override Integer insertBatchSomeColumn(Collection<AuthorizationRelationship> entityList); }