Java数据列表分页查询算法

一个程序员一生中可能会邂逅各种各样的算法,但总有那么几种,是作为一个程序员一定会遇见且大概率需要掌握的算法。今天就来聊聊这些十分重要的“必抓!”算法吧~其中数据库查询分页算法是每个后端开发者必备的算法技能。

 一、前言

昨天复用了以前的分页代码,发现分页居然失效了。原因是我之前用的是pg数据库,现在用的是mysql数据库,我也不知道是不是数据库的原因引起的。但是我直觉感觉不是。pg可以分页,mysql就变成了全量查询。有点奇怪。所以在网上查找方法解决。

二、我使用的分页工具

 1、mapper

@Repository
public interface MerchantMapper extends BaseMapper<Merchant>{
	 
    
 

}

2、service

	/**
	 * 查询商户列表
	 * @param param
	 * @return
	 */
	public PageUtils queryPage(Map<String, Object> param) {
		QueryWrapper<Merchant> qw = new QueryWrapper<Merchant>();
		
		Page<Merchant> page = new Page<>(Integer.parseInt(param.get("pageNo").toString()), Integer.parseInt(param.get("pageSize").toString()));
		if (param.containsKey("orgOuName")) {
			qw.like("org_ou_name", param.get("orgOuName").toString());
		}
		qw.orderByAsc("create_Time");
 
		IPage<Merchant> merchantIPage = merchantMapper.selectPage(page, qw);
		return new PageUtils(merchantIPage);
	}

3、controller

  
	@ApiOperation(value = "查询所有商户列表", notes = "查询所有商户列表")
    @PostMapping("pageList")
    public ResponseData<PageUtils> queryPage(@RequestBody Map<String, Object> param) {
        PageUtils pageUtils = merchantService.queryPage(param);
        return ResponseData.success(pageUtils);
    }

4、实体

@Data
@EqualsAndHashCode(callSuper=false)
@TableName(value = "base_etp_merchant", schema = "库名")
public class Merchant implements Serializable{

}

5、分页工具:PageUtils

public class PageUtils implements Serializable {
	private static final long serialVersionUID = 1L;
	/**
	 * 总记录数
	 */
	private int totalCount;
	/**
	 * 每页记录数
	 */
	private int pageSize;
	/**
	 * 总页数
	 */
	private int totalPage;
	/**
	 * 当前页数
	 */
	private int currPage;
	/**
	 * 列表数据
	 */
	private List<?> list;
	
	/**
	 * 分页
	 * @param list        列表数据
	 * @param totalCount  总记录数
	 * @param pageSize    每页记录数
	 * @param currPage    当前页数
	 */
	public PageUtils(List<?> list, int totalCount, int pageSize, int currPage) {
		this.list = list;
		this.totalCount = totalCount;
		this.pageSize = pageSize;
		this.currPage = currPage;
		this.totalPage = (int)Math.ceil((double)totalCount/pageSize);
	}

	/**
	 * 分页
	 */
	public PageUtils(IPage<?> page) {
		this.list = page.getRecords();
		this.totalCount = (int)page.getTotal();
		this.pageSize = (int)page.getSize();
		this.currPage = (int)page.getCurrent();
		this.totalPage = (int)page.getPages();
	}

	public int getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public int getCurrPage() {
		return currPage;
	}

	public void setCurrPage(int currPage) {
		this.currPage = currPage;
	}

	public List<?> getList() {
		return list;
	}

	public void setList(List<?> list) {
		this.list = list;
	}
	
}

6、分页设置MybatisPlusConfig 

@Configuration
@MapperScan("cn.xxx.blockchain.dao")
public class MybatisPlusConfig {
	/**
     * 分页插件
     * @return
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor(){
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
        return interceptor;
    }

}

 改造前后的区别就是

改造前的page

 改造后的page

还有就是增加了 MybatisPlusConfig 这个类。居然就好了,虽然不知道为什么,但是在这里记录下来。

猜你喜欢

转载自blog.csdn.net/dongjing991/article/details/131911789
今日推荐