Java自定义分页

  1. 业务场景
    遇到一个比较奇葩的需求,前端调用接口,传入一个id,想查询每个id对应的任务status(状态),查得到的要返回,查不到的也要返回传入的id(是不是很奇葩,正常来讲我只需要把查得到的给他就行),这样就没办法直接使用mybatis功能对sql进行分页查询,只能自己手动分页,对数据进行切割,然后每次批量查询。
  2. 代码处理
 		int pageSize = 100;
        int size = req.getId().size(); // 这里是入参的id数组长度
        int totalPage = PageUtil.totalPage(size, pageSize);

		ArrayList<Result> result = new ArrayList<>();// 结果
        for (int page = 0; page < totalPage; page++) {
        	// 手动分页,截取需要查询的数组
            int startIndex = page * pageSize;
            int endIndex = Math.min(startIndex + pageSize, size);
            ArrayList<String> wantSearchIds = new ArrayList<>(req.getIds().subList(startIndex, endIndex));

           // 执行查询,我这里写伪代码
           List<Respxxx> hasInSql= saerach(wantSearchIds)
           // 定义一个存在数据库的数组
           ArrayList<String> existIds = new ArrayList<>();
            if (ObjectUtil.isNotEmpty(hasInSql)) {
                hasInSql.forEach(item->{
                    // 写入存在的
                    result.add(item);
                    existIds.add(item.getId());
                });
            }

            // 获取不存在的Id
            ArrayList<String> noExistIds = new ArrayList<>(wantSearchIds );
            noExistIds .removeAll(existIds);
            noExistIds .forEach(item->{
                result.add(item);
            });

        }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
MyBatis Plus提供了丰富的分页功能,但如果需要自定义分页逻辑,你可以按照以下步骤进行操作: 1. 创建一个自定义分页类,该类需要继承`com.baomidou.mybatisplus.extension.plugins.pagination.Page`,并重写其中的方法,以实现自定义分页逻辑。例如,可以根据特定条件进行数据筛选或排序。 ```java import com.baomidou.mybatisplus.extension.plugins.pagination.Page; public class CustomPage<T> extends Page<T> { // 重写构造方法或其他方法 @Override public List<T> getRecords() { // 自定义获取数据的逻辑 // 在这里可以根据条件进行数据筛选或排序 // 返回自定义的数据列表 } // 其他自定义方法 } ``` 2. 在Mapper接口中定义一个方法,用于执行自定义分页查询。该方法需要接受自定义分页类作为参数,并返回自定义分页类的对象。 ```java import com.baomidou.mybatisplus.core.mapper.BaseMapper; public interface YourMapper extends BaseMapper<YourEntity> { CustomPage<YourEntity> customSelectPage(CustomPage<YourEntity> page); // 其他方法 } ``` 3. 在对应的Mapper XML文件中编写SQL语句,实现自定义分页查询逻辑。可以使用MyBatis的动态SQL语句来根据传入的参数进行不同的处理。 ```xml <select id="customSelectPage" parameterType="com.yourpackage.CustomPage" resultMap="YourResultMap"> SELECT * FROM your_table WHERE ... ORDER BY ... LIMIT #{offset}, #{size} </select> ``` 4. 在Service层中调用自定义分页查询方法,并传入自定义分页类对象作为参数。 ```java import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; @Service public class YourServiceImpl extends ServiceImpl<YourMapper, YourEntity> implements YourService { @Override public CustomPage<YourEntity> customSelectPage(CustomPage<YourEntity> page) { return baseMapper.customSelectPage(page); } // 其他方法 } ``` 5. 最后,在Controller层中调用Service层的自定义分页查询方法,并将结果返回给前端。 ```java @RestController @RequestMapping("/your") public class YourController { @Autowired private YourService yourService; @GetMapping("/customPage") public CustomPage<YourEntity> customPage(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize) { CustomPage<YourEntity> page = new CustomPage<>(pageNum, pageSize); return yourService.customSelectPage(page); } // 其他接口 } ``` 以上是使用MyBatis Plus实现自定义分页的简单示例,你可以根据实际需求进行相应的修改和扩展。希望对你有帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值