MyBatis-Plus通用中等、大量数据分批查询和处理
函数式接口
获取分页数据接口
主要用于获取数据
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;@FunctionalInterface
public interface MyBatisPlusPageFunctionInterface<T> {Page<T> selectPage(Page<T> page);}
数据处理接口
import java.util.List;@FunctionalInterface
public interface MyBatisPlusFunctionInterface<T> {void execute(List<T> tList);}
通用逻辑工具类
用于执行分页数据接口实现和数据处理接口实现逻辑
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;import java.util.List;/*** MyBatis-Plus 工具类*/
public final class MyBatisPlusUtils {public static final int DEFAULT_PAGE_SIZE_INT = 1000;public static final long DEFAULT_PAGE_SIZE = 1000L;private MyBatisPlusUtils() {}/*** 无条件分页查询N条(N由调用方决定)** @param extendsBaseMapperClass 继承了BaseMapper的Mapper接口* @param myBatisPlusFunctionInterface 业务逻辑*/public static <T> void findByPage(BaseMapper<T> extendsBaseMapperClass, long pageSize, MyBatisPlusFunctionInterface<T> myBatisPlusFunctionInterface) {findByPage(extendsBaseMapperClass, null, pageSize, myBatisPlusFunctionInterface);}/*** 无条件分页查询(默认每次取1000条,直到取完为止)** @param extendsBaseMapperClass 继承了BaseMapper的Mapper接口* @param myBatisPlusFunctionInterface 业务逻辑*/public static <T> void findByPage(BaseMapper<T> extendsBaseMapperClass, MyBatisPlusFunctionInterface<T> myBatisPlusFunctionInterface) {findByPage(extendsBaseMapperClass, null, DEFAULT_PAGE_SIZE, myBatisPlusFunctionInterface);}/*** 有条件分页查询(默认每次取1000条,直到取完为止)** @param extendsBaseMapperClass 继承了BaseMapper的Mapper接口* @param myBatisPlusFunctionInterface 业务逻辑*/public static <T> void findByPage(BaseMapper<T> extendsBaseMapperClass, LambdaQueryWrapper<T> lambdaQueryWrapper, MyBatisPlusFunctionInterface<T> myBatisPlusFunctionInterface) {findByPage(extendsBaseMapperClass, lambdaQueryWrapper, DEFAULT_PAGE_SIZE, myBatisPlusFunctionInterface);}/*** 后端分页查询所有数据** @param extendsBaseMapperClass 继承了BaseMapper的Mapper接口* @param myBatisPlusFunctionInterface 业务逻辑*/public static <T> void findByPage(BaseMapper<T> extendsBaseMapperClass, LambdaQueryWrapper<T> lambdaQueryWrapper, long pageSize, MyBatisPlusFunctionInterface<T> myBatisPlusFunctionInterface) {findByPage((Page<T> page) -> extendsBaseMapperClass.selectPage(page, lambdaQueryWrapper), pageSize, myBatisPlusFunctionInterface);}/*** 通用分页查询方法** @param functionInterface 获取分页数据的方法* @param pageSize 每次获取的数据量* @param myBatisPlusFunctionInterface 对获取的分页数据进行处理*/public static <T> void findByPage(MyBatisPlusPageFunctionInterface<T> functionInterface, long pageSize, MyBatisPlusFunctionInterface<T> myBatisPlusFunctionInterface) {// 参数校验Assert.isTrue(pageSize > 0, "每页大小必须大于 0");Assert.notNull(myBatisPlusFunctionInterface, "数据处理逻辑不能为 null");int currentPage = 1;while (true) {Page<T> tPage = functionInterface.selectPage(Page.of(currentPage, pageSize));long pages = tPage.getPages();List<T> records = tPage.getRecords();//数据为空if (CollectionUtils.isEmpty(records)) {return;}// 业务逻辑myBatisPlusFunctionInterface.execute(records);//最后一页if (pages <= currentPage) {return;}currentPage++;}}}
使用方法
简单查询
//批量获取全部数据,没设置一次取几条数据,这里默认取1000条。MyBatisPlusUtils.findByPage(XXXMapper,//简单查询继承了BaseMapper的Mappernew LambdaQueryWrapper<DataBaseEntity>().isNull(DataBaseEntity::getName),//简单查询mapper的条件records -> {//处理分页查询出来的数据,如果有多次分页,则进行多次处理。比如有2页,则下面的方法会执行2次。List<DataBaseEntity> l = new ArrayList<>(MyBatisPlusUtils.DEFAULT_PAGE_SIZE_INT);//遍历处理这一批次的数据for (DataBaseEntity record : records) {//处理查询到的数据}}});
自定义查询方法
//自定义查询方法MyBatisPlusUtils.findByPage((Page<HiBdSrvtmpMedOrd> page) -> {//对传入的数据进行处理String param2 = dto.getParam2() + dto.getParam3();return hiBdSrvtmpMedOrdMapper.findPage(page, param2);//使用自定义的分页方法,并且分页参数可以不固定,需要多少个都行。},//使用自定义的分页方法2000L,//一次取2000条数据records -> {//遍历处理这一批次的数据for (HiBdSrvtmpMedOrd record : records) {//处理查询到的数据}});
总结
上面提供的方式对于初学者或者没有系统了解过JAVA8的函数式接口、lambda表达式、匿名函数等新特性的人,理解起来是非常抽象、不好理解的,但是正因为代码抽象,所以功能才更强大!
还有其它组合使用方式没有一一列举出来,大家可以自行尝试使用和扩展。
有些功能没有考虑在内,比如:分批提交的事务问题、没有使用批处理模式(ExecutorType.BATCH)、使用 Cursor进行流式查询(适用于大数据量)、多线程处理(代码会更复杂,而且我也没有想过通用这方面的代码,这方面单独的代码会更合适)
有更好的办法,欢迎留言,谢谢。