手动分页(简单易懂)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_36537546/article/details/81026384

写了一个简单的分页查询,希望对你们有帮助:

就不多说了,直接看操作:

首先是我们的Dao层

这里为什么加@Param呢,因为我们穿的是多个参数,但是系统不知道你传的是多个参数,就需要用@Param来声明一下这个变量

//分页+查询
List<Hy> findByPage(@Param("hy")Hy hy,@Param("pageno")Integer pageno,@Param("pagesize")Integer pagesize,@Param("mingcheng")String mingcheng);
//条数
long findCount(@Param("hy")Hy hy,@Param("mingcheng")String mingcheng);

接下来是我们的mapping  :

这个地方因为构造表的时候没用使用连表,所以在这个地方加了一个连接查询,最后给他了一个分页排序

要注意!!一定是排序在前边,分页在后边。。想当年。。因为这个栽了一个大坑。。咳咳。。忽略这点水文

<!--分页+查询 -->
<select id="findByPage" resultMap="BaseResultMap">
    select * from hy inner join gongsi ON hy.gongsi_id=gongsi.id 
    <where>
	<if test="hy.id != null">
       	    and hy.id=#{hy.id}
	</if>
	<if test="hy.nicheng != null  and  hy.nicheng!=''">
	    and nicheng LIKE concat('%',#{hy.nicheng},'%')
	</if>
	<if test="hy.xingbie != null ">
	    and xingbie=#{hy.xingbie}
	</if>
	<if test="hy.leibie != null ">
	    and leibie=#{hy.leibie}
	</if>
	<if test="hy.shenfen != null and hy.shenfen !=''">
	    and shenfen=#{hy.shenfen}
	</if>
	<if test="hy.shiqv != null and hy.shiqv !=''">
	    and shiqv=#{hy.shiqv}
	</if>
	<if test="hy.xingming != null and hy.xingming !=''">
	    and xingming=#{hy.xingming}
	</if>
	<if test="hy.shoujihao != null and hy.shoujihao !=''">
	    and shoujihao=#{hy.shoujihao}
	</if>
	<if test="hy.tuijianrenid != null">
	    and tuijianrenid=#{hy.tuijianrenid}
	</if>
	<if test="mingcheng != null and mingcheng != ''">
	    and gongsi.mingcheng LIKE concat('%',#{mingcheng},'%')
	</if>
    </where>
        ORDER BY hy.id DESC limit #{pageno},#{pagesize} 
</select>
<!--条数 -->
<select id="findCount" resultType="long" parameterType="com.zr.bean.Hy">
    select count(2) from hy inner join gongsi ON hy.gongsi_id=gongsi.id 
    <where>
        <if test="hy.id != null ">
            and hy.id=#{hy.id}
        </if>
        <if test="hy.nicheng != null  and  hy.nicheng!=''">
	    and nicheng LIKE concat('%',#{hy.nicheng},'%')
        </if>
        <if test="hy.xingbie != null ">
            and xingbie=#{hy.xingbie}
        </if>
        <if test="hy.leibie != null ">
	    and leibie=#{hy.leibie}
        </if>
        <if test="hy.shenfen != null and hy.shenfen !=''">
	    and shenfen=#{hy.shenfen}
        </if>
        <if test="hy.shiqv != null and hy.shiqv !=''">
    	    and shiqv=#{hy.shiqv}
        </if>
        <if test="hy.xingming != null and hy.xingming !=''">
	    and xingming=#{hy.xingming}
        </if>
        <if test="hy.shoujihao != null and hy.shoujihao !=''">
	    and shoujihao=#{hy.shoujihao}
        </if>
        <if test="hy.tuijianrenid != null">
	    and tuijianrenid=#{hy.tuijianrenid}
        </if>
        <if test="mingcheng!= null and mingcheng!=''">
	    and gongsi.mingcheng LIKE concat('%',#{mingcheng},'%')
        </if>
    </where>
</select>

下面是我们的Serivce层:

//因为在dao加了@param,所以这个地方就不用加了
List<Hy> findByPage(Hy hy, Integer pageno, Integer pagesize,String mingcheng);

long findCount(Hy hy,String mingcheng);

然后是serivce的实现类:

@Override <!-- 分页+查询 -->
public List<Hy> findByPage(Hy hy, Integer pageno, Integer pagesize,String mingcheng) {
	// TODO Auto-generated method stub
	if(hy==null){
		hy=new Hy();
	}
	if(pageno == null){
		pageno = 1;
	}
	if(pagesize == null){
		pagesize = AdminController.getPageSize();
	}
	return hyDAO.findByPage(hy, (pageno-1)*pagesize, pagesize,mingcheng);
}
@Override <!-- 条数 -->
public long findCount(Hy hy,String mingcheng) {
	// TODO Auto-generated method stub
	if(hy==null){   //判断对象如果为空 则创建一个对象
		hy=new Hy();
	}
	return hyDAO.findCount(hy,mingcheng);
}

最后是我们的Controller层:

// 分页+提示
// 这个是写在最上边的
// 开始为第一页
private Integer pageNo = 1;  
// 每页显示15条
private static Integer pageSize = 15;
// 总条数
private Long totalCount;
private Long totalPage;
// 弹窗
private String prompt;

@RequestMapping(value = "/hyList")
public String hyList(Model model, @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo, Hy 

hy,String mingcheng) {
	//判断对象是否为空
	if (hy != null) {
	  //循环遍历出一个集合
	  hyList = hyService.findByPage(hy, pageNo, pageSize, mingcheng);
	  //查询出总条数
	  totalCount = hyService.findCount(hy, mingcheng);
	  // 总页数= 总页/每页大小 :总页/每页大小+1
	  totalPage = totalCount % pageSize == 0 ? totalCount / pageSize : totalCount / pageSize + 1;
	  //Model获取到对象的值
	  model.addAttribute("gongsiList", gongsiList);
 	  model.addAttribute("totalPage", totalPage);
	  model.addAttribute("totalCount", totalCount);
	  model.addAttribute("pageNo", pageNo);
	  model.addAttribute("hyList", hyList);
	  //这个判断写不写都行。。如果不写的话就在最后return admin/hy_List(按你的路径来)
	  if (hy.getLeibie() == 0) {
		return "admin/hy_list";
	  } else if (hy.getLeibie() == 1) {
		return "admin/hy_list";
	  }
	}
	return null;
}

忘了还有一个JSP页面。。。

这个地方我就直接给你看分页的这一部分

如果这个地方有什么不懂得地方。。可以留言评论。。(搞的我跟打广告似的。。)

<div style="float: right;margin-top: -20px;">
    <ul class="pagination" style="margin: 5px 0;">
	<li class="paginate_button">
	    <a href="hyList?pageNo=1&hy.nicheng=${hy.nicheng}&hy.id=${hy.id }&hy.leibie=${hy.leibie }&mingcheng=${mingcheng}">首页</a>
	</li>
	<li class="paginate_button ">
	    <c:if test="${pageNo>1}">
	        <a href="hyList?pageNo=${pageNo-1}&hy.nicheng=${hy.nicheng }&hy.id=${hy.id }&hy.leibie=${hy.leibie }&mingcheng=${mingcheng}">上一页</a>
	    </c:if>
	    <c:if test="${currentPage==1}">
	        <a href="javascript:void(0)">上一页</a>
	    </c:if>
	</li>
	<li class="paginate_button disabled">
	    <a href="javascript:void(0)">${pageNo}/${totalPage}</a>
	</li>
	<li class="paginate_button ">
	    <input type="text" id="mubiaoye" value="${pageNo}" style="float: left;line-height: 1.42857;margin-left: -1px;padding: 4px 10px;position: relative;text-decoration: none;border: 1px solid #DDDDDD;color:#000;width: 50px;" maxlength="10" onkeyup="value=value.replace(/[^\d]/g,'')" />
	</li>
	<li class="paginate_button ">
	    <a href="javascript:void(0)" onclick="tiaozhuan('hyList?hy.nicheng=${hy.nicheng }&hy.id=${hy.id }&mingcheng=${mingcheng}&hy.leibie=${hy.leibie}&pageNo=')">跳转</a>
	</li>
	<li class="paginate_button ">
	    <c:if test="${pageNo<totalPage}">
	        <a href="hyList?pageNo=${pageNo+1}&hy.nicheng=${hy.nicheng }&mingcheng=${mingcheng}&hy.id=${hy.id }&hy.leibie=${hy.leibie}">下一页</a>
	    </c:if>
	    <c:if test="${pageNo>=totalPage}">
	        <a href="javascript:void(0)">下一页</a>
	    </c:if>
	</li>
	<li class="paginate_button next">
    	    <a href="hyList?pageNo=${totalPage == 0 ? 1 : totalPage}&mingcheng=${mingcheng}&hy.nicheng=${hy.nicheng }&hy.id=${hy.id }&hy.leibie=${hy.leibie}">尾页</a>
	</li>
    </ul>
</div>


猜你喜欢

转载自blog.csdn.net/qq_36537546/article/details/81026384