• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Mybatis-plus – 分页 – RowBounds

mybatis 海叔叔 4年前 (2021-11-04) 45次浏览 已收录 0个评论

Mybatis-plus – 分页 – RowBounds
Server 类

@Slf4j
@Transactional
@Service
public class AvatarServiceImpl implements AvatarService {

	@Autowired
	private AvatarDao avatarDao ;
	
	@Autowired
	private PaginationProperties paginationProperties ; 
	
//	do 。。。

	@Override
	public Page<Avatar> doFondPageObject(Integer pageCurrent, String name) {
		log.debug("doFondPageObject pageCurrent={} name={}",pageCurrent,name);
		Assert.isArgumentValid(pageCurrent==null||pageCurrent<1, "页码异常");
		/**查询:[分页前]总数据量*/
		Wrapper<Avatar> wrapper = //条件
				new EntityWrapper<Avatar>()
				.like(name!=null&&!"".equals(name),"name", name);
		Integer totalDataCount = //[分页前]总数据量
				avatarDao.selectCount(wrapper);
		Assert.isNoFound(totalDataCount==null||totalDataCount==0, "没有数据");
		/**查询分页*/
		Integer pageSize = 		//以后再定制化
				paginationProperties.getPageSize();
		int offset = 			//开始位置
				paginationProperties.getStartIndex(pageCurrent); 
		RowBounds rowBounds = 	//分页查询条件
				new RowBounds(offset, pageSize);
		List<Avatar> list = avatarDao.selectPage(rowBounds, wrapper); 
		Assert.isNoFound(list==null||list.size()==0, "没有数据!");
		return new Page<Avatar>(list, totalDataCount, pageCurrent, pageSize);
	}
}

其中 paginationProperties 、 page 为自定义类

paginationProperties :

@Data
@Configuration
@ConfigurationProperties(prefix = "page.config")
public class PaginationProperties {

	/**
	 * 默认 每页 数据量
	 */
	final static private Integer DEFAULT_PAGESIZE = 10 ;  
	
	/**
	 * 每页多少条数据
	 */
	private Integer pageSize = DEFAULT_PAGESIZE ; 
	
	/**
	 * 工具:计算分页的startIndex
	 * @param pageCurrent 当前页码 1~n
	 * @return startIndex
	 */
	public Integer getStartIndex(Integer pageCurrent) {
		return (pageCurrent-1)*pageSize ; 
	}
}

page

@Data
public class Page<T> implements Serializable{
	private static final long serialVersionUID = 7844855365725887051L;
	/**数据s*/
	private List<T> datas ;
	/**(分页前)总数据数*/
	private Integer totalDataCount = 0 ; 
	/**总页数*/
	private Integer pageTotal = 0 ; 
	/**当前页码*/
	private Integer pageCurrent = 1 ; 
	/**当前页数据量*/
	private Integer pageSize = 3;
	public Page(List<T> datas, Integer totalDataCount, Integer pageCurrent, Integer pageSize) {
		this.datas = datas;
		this.totalDataCount = totalDataCount;
		this.pageCurrent = pageCurrent;
		this.pageSize = pageSize;
		this.pageTotal = (totalDataCount-1)/pageSize + 1;
	}
	
}

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Mybatis-plus – 分页 – RowBounds
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址