自定义分页查询 参考Bootstrap分页插件

参考Bootstrap 分页组件:
Bootstrap 分页组件

大致实现效果:
在这里插入图片描述

一、分页实体类

/**
 * 分页Model类
 * @author 
 *
 */
public class PageBean {
	private int page; // 第几页
	private int pageSize; // 每页记录数
	private int start;  // 起始页
		
	public PageBean(int page, int pageSize) {
		super();
		this.page = page;
		this.pageSize = pageSize;
	}
	
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}	
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getStart() {
		return (page-1)*pageSize;
	}	
}

二、Controller

/**
	 * 请求主页
	 * @return
	 */
	@RequestMapping("/index")
	public ModelAndView index(@RequestParam(value="page",required=false)String page,
			@RequestParam(value="typeId",required=false)String typeId,
			@RequestParam(value="releaseDateStr",required=false)String releaseDateStr,
			HttpServletRequest request) throws Exception{
		ModelAndView mav=new ModelAndView();
		if(StringUtil.isEmpty(page)) {
			page="1";
		}
		PageBean pageBean=new PageBean(Integer.parseInt(page),8);//每页8条数据
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("start", pageBean.getStart());   //起始下标
		map.put("size", pageBean.getPageSize());  
		
		map.put("typeId", typeId);
		map.put("releaseDateStr", releaseDateStr);
		
		List<Blog> blogList=blogService.list(map);
		
		mav.addObject("blogList",blogList);
		
		StringBuffer param =new StringBuffer();
		if(!StringUtil.isEmpty(typeId)) {
			param.append("typeId="+typeId+"&");
		}
		if(!StringUtil.isEmpty(releaseDateStr)) {
			param.append("releaseDateStr="+releaseDateStr+"&");
		}
				
		mav.addObject("pageCode", PageUtil.genPagination(request.getContextPath()+"/index.html",
				blogService.getTotal(map), Integer.parseInt(page), 8, param.toString()));
		mav.addObject("pageTitle", "java开源博客系统");
		mav.addObject("mainPage", "foreground/blog/list.jsp");
		mav.setViewName("mainTemp");
		
		return mav;
	}

三、PageUtil工具类

/**
	 * 生成分页代码
	 * @param targetUrl 目标地址
	 * @param totalNum 总记录数
	 * @param currentPage 当前页
	 * @param pageSize 每页大小
	 * @return
	 */
	public static String genPagination(String targetUrl,long totalNum,int currentPage,int pageSize,String param){
		long totalPage=totalNum%pageSize==0?totalNum/pageSize:totalNum/pageSize+1;
		if(totalPage==0){
			return "未查询到数据";
		}else{
			StringBuffer pageCode=new StringBuffer();
			pageCode.append("<li><a href='"+targetUrl+"?page=1&"+param+"'>首页</a></li>");
			if(currentPage>1){
				pageCode.append("<li><a href='"+targetUrl+"?page="+(currentPage-1)+"&"+param+"'>上一页</a></li>");			
			}else{
				pageCode.append("<li class='disabled'><a href='"+targetUrl+"?page="+currentPage+"&"+param+"'>上一页</a></li>");		
			}
			for(int i=currentPage-2;i<=currentPage+2;i++){
				if(i<1||i>totalPage){
					continue;
				}
				if(i==currentPage){
					pageCode.append("<li class='active'><a href='"+targetUrl+"?page="+i+"&"+param+"'>"+i+"</a></li>");	
				}else{
					pageCode.append("<li><a href='"+targetUrl+"?page="+i+"&"+param+"'>"+i+"</a></li>");	
				}
			}
			if(currentPage<totalPage){
				pageCode.append("<li><a href='"+targetUrl+"?page="+(currentPage+1)+"&"+param+"'>下一页</a></li>");		
			}else{
				pageCode.append("<li class='disabled'><a href='"+targetUrl+"?page="+currentPage+"&"+param+"'>下一页</a></li>");	
			}
			pageCode.append("<li><a href='"+targetUrl+"?page="+totalPage+"&"+param+"'>尾页</a></li>");
			return pageCode.toString();
		}
	}

四、mapper sql语句

<select id="list" parameterType="Map" resultMap="BlogResult">
		select * from t_blog
		<where>
			<if test="title!=null and title!='' ">
				and title like #{title}
			</if>
			<if test="typeId!=null and typeId!='' ">
				and typeId=#{typeId}
			</if>
			<if test="releaseDateStr!=null and releaseDateStr!='' ">
				and DATE_FORMAT(releaseDate,'%Y年%m月')=#{releaseDateStr}
			</if>
		</where>
		order by releaseDate desc
		<if test="start!=null and size!=null">
			limit #{start},#{size}
		</if>
	</select>
	
	<select id="getTotal" parameterType="Map" resultType="Long">
		select count(*) from t_blog
		<where>
		    <if test="title!=null and title!='' ">
				and title like #{title}
			</if>
			<if test="typeId!=null and typeId!='' ">
				and typeId=#{typeId}
			</if>
			<if test="releaseDateStr!=null and releaseDateStr!='' ">
				and DATE_FORMAT(releaseDate,'%Y年%m月')=#{releaseDateStr}
			</if>
		</where>
	</select>

五、页面
在这里插入图片描述
list.jsp:

<div class="datas">
	 <ul>
	  	<c:forEach var="blog" items="${blogList }">
	  		<li style="margin-bottom: 30px">
	  					<span class="date"><a href="${pageContext.request.contextPath}/blog/articles/${blog.id }.html"><fmt:formatDate value="${blog.releaseDate }" type="date" pattern="yyyy年MM月dd日"/></a></span>
					  	<span class="title"><a href="${pageContext.request.contextPath}/blog/articles/${blog.id }.html">${blog.title }</a></span>
					  	<span class="summary">摘要: ${blog.summary }...</span>
					  	<span class="img">
					  		<c:forEach var="image" items="${blog.imageList }">
						  		<a href="/blog/articles/${blog.id }.html">${image}</a>
						  		&nbsp;&nbsp;
					  		</c:forEach>
					  	</span>
					  	<span class="info">发表于 <fmt:formatDate value="${blog.releaseDate }" type="date" pattern="yyyy-MM-dd HH:mm"/> 阅读(${blog.clickHit }) 评论(${blog.replyHit }) </span>
	  					
	  		</li>
	  		<hr style="height:5px;border:none;border-top:1px dashed gray;padding-bottom:  10px;" />
	  	</c:forEach>
	</ul>
</div>

<nav>
  <ul class="pagination pagination-sm">
  	${pageCode }
  </ul>
</nav>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值