public class EasySqlInjector extends DefaultSqlInjector {
@Override
public List<AbstractMethod> getMethodList(Class<?> mapperClass) {
List<AbstractMethod> methodList=super.getMethodList(mapperClass);
methodList.add(new InsertBatchSomeColumn());
return methodList;
}
}
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.1</version>
</dependency>
在MybatisPlusConfig文件中注入该Bean,代码如下:
@Configuration
public class MybatisPlusConfig {
/**
* 分页插件
*
* @return PaginationInterceptor
*/
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
@Bean
public EasySqlInjector easySqlInjector() {
return new EasySqlInjector();
}
}
还要扩展一下自带的BaseMapper,代码如下:
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import java.util.Collection;
/**
* 扩展通用 Mapper,支持数据批量插入
*
* @author 天开易想
*/
public interface EasyBaseMapper<T> extends BaseMapper<T> {
/**
* 批量插入 仅适用于mysql
*
* @param entityList 实体列表
* @return 影响行数
*/
Integer insertBatchSomeColumn(Collection<T> entityList);
}
我们即可在业务类中,实现下面的引用了,代码如下:
/**
* 定义业务mapper接口,继承刚刚扩展的EasyBaseMapper
*
* @author 天开易想
*/
@Mapper
public interface TestMapper extends EasyBaseMapper<Test> {
}
/**
* 业务实现类接口,即可引用
*
* @author 天开易想
*/
@Service
public class TestServiceImpl extends ServiceImpl<TestMapper, Test> implements TestService {
@Override
public Integer testBatch(Collection<Test> testList) {
return baseMapper.insertBatchSomeColumn(testList);
}
因为在BaseMapper中是不能拿来直接引用的,为什么不能直接引用,据说是只支持MySql数据库,所以作者没有内置的原因吧!