ssm框架中的分页

我的类叫做tongxunlu,该类里有tname字段

<!-- 获得总条数 -->

<select id="queryRecordPageCount" resultType="int">
select count(tid) fromttongxunlu
<where>
<if test="tname!=null">
and tname like '%${tname}%'
</if></where> </select>
<!-- 分页 start是起始页,end是结束页-->
<select id="getRecordByPage" parameterType="hashMap"resultType="com.yanruan.ssm.pojo.Tongxunlu">
select * from (select rownum rw,t.* from ttongxunlu t
<where>
<if test="tname!=null">
and tname like '%${tname}%'
</if>
</where>
)where rw <=${start} and rw >${end}order by tid desc
</select>

dao.java的代码

        /**
	 * 根据每页的起始条数和末尾条数查用户
	 * @return
	 */
	public List<Tongxunlu> getRecordByPage(Map map);

	
	/**
	 * 获得总条数
	 */
	public int queryRecordPageCount(Map map) ;
service.java,因为我的前端需要 共计条记录、共几页上一页 下一页这几个信息

/**
	 * 查询改页数下所有人员信息列表
	 * @param page
	 * @param map
	 * @return
	 */
	public List<Tongxunlu> getRecordByPage(int page,Map map);
	/**
	 * 查询人员信息条数
	 * @return
	 */
     public int getRecordCount(Map map);
	/**
	 * 查询人员信息共几页
	 * @return
	 */
	public int queryRecordPage(Map map);
	


serviceImpl.java,可以看到map里我放的是start和end,当要根据条件查找信息时,还要向map里放入条件

/**
	 * 根据页数查询该页所有的人员信息
	 */
	public List<Tongxunlu> getRecordByPage(int page, Map map) {
		// TODO Auto-generated method stub
		int count = tongxunluDao.queryRecordPageCount(map);
		// 起始条数,按照每页显示五条进行计算
		int start = count - (page - 1) * 5;
		// 末尾条数
		int end = start - 5;

		// 将start和end放入map中
		map.put("start", start);
		map.put("end", end);
		return tongxunluDao.getRecordByPage(map);
	}

	/**
	 * 查询人员信息一共多少页
	 */
	public int queryRecordPage(Map map) {
		int count = tongxunluDao.queryRecordPageCount(map);
		// 如果总条数可以被5整除,那么页数等于count / 5
		if (count % 5 == 0) {
			return count / 5;
		} else {
			// 若不能整除 则等于count / 5 + 1
			return count / 5 + 1;
		}
	}

	public int getRecordCount(Map map) {
		// TODO Auto-generated method stub
		return tongxunluDao.queryRecordPageCount(map);
	}

controller.java


@RequestMapping("recordlist.do")
	public String userlist(String page, ModelMap model, String tname) {
		// 如果页数等于0或者为空,那么page=1
		if ("".equals(page) || page == null) {
			page = "1";
		}
		Map<String, String> map = new HashMap<String, String>();

		if (!StringUtil.isNull(tname)) {
			map.put("tname", tname);
			model.addAttribute("TNAME", tname);
		}

		List<Tongxunlu> tList = ts.getRecordByPage(Integer.parseInt(page), map);
		int yema = ts.queryRecordPageCount(map);
		int count = ts.getRecordCount(map);

		// 页数
		model.addAttribute("PAGE", page);
		model.addAttribute("COUNT", count);
		model.addAttribute("YEMA", yema);
		model.addAttribute("TLIST", tList);
		return "right";
	}

注:在最开始进入列表的时候,传过来的page应该是个" ",或者null,这样,我们另page=1后进行跳转,这个方法里建立的map是模糊查找的时候用到的,若是不涉及可以去掉。模糊查找坐下来,在分页的时候有些问题,还没有解决


前端页面:

<div class="pagin">
			<div class="message">
				共<i class="blue"><c:out value="${COUNT}"></c:out></i>条记录,当前显示第 <i
					class="blue"><c:out value="${PAGE}"></c:out> </i>页
			</div>
			<ul class="paginList">
				<li class="paginItem"><a
					href="recordlist.do?page=<c:out value='${(PAGE-1)<=0?1:(PAGE-1)}'/>"><span
						class="pagepre"></span></a></li>
				
				<li class="paginItem"><a
					href="recordlist.do?page=<c:out value='${(PAGE+1)>=YEMA?YEMA:(PAGE+1)}'/>"><span
						class="pagenxt"></span></a></li>
			</ul>
		</div>


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值