定义业务处理接口
import java.util.List; public interface PageService{ void business(List records); }
定义工具类,这里提供了两种实现方式
import com.baomidou.mybatisplus.core.conditions.Wrapper; import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.IService; import org.springframework.util.CollectionUtils; public class PageUtils { public staticvoid pageTurner(LambdaQueryChainWrapper lambdaQueryChainWrapper, PageService pageService) { // 当前页 long current = 1; // 分页大小,可以修改 long size = 200; for (; ; ) { // 分页 Page page = new Page<>(current, size); // 执行 lambdaQueryChainWrapper.page(page); // 结果为空,直接返回 if (CollectionUtils.isEmpty(page.getRecords())) { return; } // 业务处理 pageService.business(page.getRecords()); // 说明没有下一页,直接返回 if (current * size >= page.getTotal()) { return; } // 下一页 current++; } } public static void pageTurner(IService iService, Wrapper queryWrapper, PageService pageService) { // 当前页 long current = 1; // 分页大小,可以修改 long size = 200; for (; ; ) { // 分页 Page page = new Page<>(current, size); // 执行 iService.page(page, queryWrapper); // 结果为空,直接返回 if (CollectionUtils.isEmpty(page.getRecords())) { return; } // 业务处理 pageService.business(page.getRecords()); // 说明没有下一页,直接返回 if (current * size >= page.getTotal()) { return; } // 下一页 current++; } } }
使用方式
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper; import com.bzcst.bop.charge.Application; import com.bzcst.bop.oms.orm.model.po.UserOrder; import com.bzcst.bop.oms.orm.service.UserOrderService; import lombok.extern.slf4j.Slf4j; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import javax.annotation.Resource; import java.util.List; @Slf4j @SpringBootTest(classes = Application.class) @RunWith(SpringRunner.class) public class OrderTest { @Resource private UserOrderService userOrderService; @Test public void name1() { LambdaQueryChainWrapperlqcw = userOrderService.lambdaQuery().eq(UserOrder::getUserId, 29L); PageUtils.pageTurner(lqcw, new PageService () { @Override public void business(List records) { for (UserOrder record : records) { System.out.println(record); } } }); } @Test public void name2() { LambdaQueryWrapper lqw = new LambdaQueryWrapper<>(); lqw.eq(UserOrder::getUserId, 29L); // 如果不需要查询条件,lqw传null PageUtils.pageTurner(userOrderService, lqw, new PageService () { @Override public void business(List records) { for (UserOrder record : records) { System.out.println(record); } } }); } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)