mybatis-plus 实现分页查询及批量插入
分页查询
/*
* 分页查询
*
* @param pageNum 页数
* @param pageSize 条数
*/
private void getPage(Integer pageNum, Integer pageSize) {
Page page = new Page(pageNum, pageSize);
// 分页查询
Page<BaseEntity> dataPage = baseMapper.getDataPage(page);
// ... 具体的业务代码
// 当最大也等于当前页时退出递归
if (dataPage.getPages() != pageNum) {
// 递归查询下一页
this.getPage(pageNum + 1, pageSize);
}
}
- 批量查询默认情况下size为500,超过需要进行设置
package com.zl.config;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
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
@MapperScan("com.baomidou.cloud.service.* .mapper*r")
public class MybatisPlusConfig {
//新版本
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor(){
MybatisPlusInterceptor mybatisPlusInterceptor=new MybatisPlusInterceptor();
mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return mybatisPlusInterceptor;
}
//旧版本
@Bean
public PaginationInterceptor paginationInterceptor() {
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
//设置最大单页限制数量,默认 500 条,-1 不受限制
paginationInterceptor.setLimit(-1);
return paginationInterceptor;
}
}
批量插入
- 伪插入
baseService.saveBatch(list);
其内部还是使用的for循环插入
- 批量插入
自定义SQL注入器
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;
/**
* 批量插入SQL注入器
*/
public class InsertBatchSqlInjector extends DefaultSqlInjector {
@Override
public List<AbstractMethod> getMethodList(Class<?> mapperClass) {
// 注意:此SQL注入器继承了DefaultSqlInjector(默认注入器),调用了DefaultSqlInjector的getMethodList方法,保留了mybatis-plus的自带方法
List<AbstractMethod> methodList = super.getMethodList(mapperClass);
// i -> i.getFieldFill() != FieldFill.UPDATE
methodList.add(new InsertBatchSomeColumn()); //添加InsertBatchSomeColumn方法
return methodList;
}
}
把SQL注入器交给Spring
@Bean
public InsertBatchSqlInjector easySqlInjector() {
return new InsertBatchSqlInjector();
}
到此定义完毕,在Mapper中生成insertBatchSomeColumn
(必须是这个方法名)方法,你就可以撒手不管了,直接调用就行,或者直接在ServiceImpl通过Mapper调用insertBatchSomeColumn,然后ALT+回车生成此方法
package com.zl;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zl.model.entity.UserEntity;
import java.util.Collection;
import org.apache.ibatis.annotations.Mapper;
/**
* 用户基本信息
*/
@Mapper
public interface UserMapper extends BaseMapper<UserEntity> {
Integer insertBatchSomeColumn(Collection<UserEntity> entityList);
}
或者
public interface BatchBaseMapper extends BaseMapper<T> {
Integer insertBatchSomeColumn(Collection<T> entityList);
}
package com.zl;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zl.model.entity.UserEntity;
import java.util.Collection;
import org.apache.ibatis.annotations.Mapper;
/**
* 用户基本信息
*/
@Mapper
public interface UserMapper extends BatchBaseMapper<UserEntity> {
}