SSM框架如何实现分页

摘要:很久很久没有与大家学习上的交流了;因为我成为了社会工作者的一份子;努力三年的汗水,换来了一份工作;自己一直坚持的工作;陪伴我的是三年前买的惠普笔记本;是不记得哪年夏天我姐给我买的篮球;是写不完,学不透的许多代码…我很开心,能够通过自己的努力走到现在,一直坚持到现在,我也希望像我这样的同学能够找到自己所想要的东西。今天咋们上一节,我工作这么一个月来所学到的东西想与大家一起分享,希望能够对大家有所帮助。
主要思路
对于从数据库中取出大量的数据,唯一的办法就是分页显示,所有的操作离不开口诀;从外送内,从内送外;我这里的外是页面的送入数据库的数据;内是数据库的信息送给页面显示;说起来是非常简单,实践起来细节处理的东西比较多;但是总体思路要在实践之前在脑海中所搭建好;咋们今天无非玩的是从内到外。
详细步骤
分页分页;让我第一联想到的就是sql中的limit关键字;所以取数据库的关键还得是limit;limit的用法实现从第几行开始,长度多少;所以创建sql语句

>     <!-- 根据班级名次分页查询  -->
>     	<select id="findByCnameLimit" parameterType="hashMap" resultMap="studentbean">
>     		SELECT x.* FROM(SELECT y.*, @rownum := @rownum + 1 AS rownum FROM  (SELECT X.*,Y.cname FROM info X JOIN team Y ON X.CID=Y.CID ORDER
> BY X.grade DESC) y,(SELECT @rownum := 0) x
>     		WHERE y.cname=#{cname}) x LIMIT #{page.beginIndex},#{page.pageSize}
>     	</select>

LIMIT 后面跟的两参数我把它搞成动态性;不写死,操作时就动态查询不同的数据,我这里首先是根据班级查询所有人员并以分数从高到低排序。有很多人看不懂page.beginIndex这样写,因为我hashMap中存了一个page对象;所以我是对象.属性名获取它本身的值。希望看懂这段话后举一反三。其中还需要查询出总条数目的sql语句:

<!-- 查询总数 -->
	<select id="findCount" parameterType="String" resultType="int">
		SELECT COUNT(*) FROM (SELECT X.*,Y.cname FROM info X JOIN team Y ON X.CID=Y.CID ORDER BY X.grade DESC) y
		WHERE y.cname=#{cname} 
	</select>

既然是对象的话;那我就必须写一个这样的对象;如下:

package com.lp.entity;

public class Page {

	private int pageSize=6;//页面显示行数
	private int currentPage=1;//当前页面
	private int pageCount;//页面总数
	private int beginIndex;//查询开始值
	private int totalCounts;//一共多少条
	
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public int getCurrentPage() {
		return currentPage;
	}
	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}
	public int getPageCount() {
		return pageCount;
	}
	public void setPageCount(int pageCount) {
		this.pageCount = pageCount;
	}
	public int getBeginIndex() {
		return beginIndex;
	}
	public void setBeginIndex(int beginIndex) {
		this.beginIndex = beginIndex;
	}
	public int getTotalCounts() {
		return totalCounts;
	}
	public void setTotalCounts(int totalCounts) {
		this.totalCounts = totalCounts;
	}
	@Override
	public String toString() {
		return "Page [pageSize=" + pageSize + ", currentPage=" + currentPage + ", pageCount=" + pageCount
				+ ", beginIndex=" + beginIndex + ", totalCounts=" + totalCounts + "]";
	}
}

初始化一些必要的参数。
在这里插入图片描述输入班级进行搜索;在控制器执行findByCnameLimit路径下的方法;

//实现一对多表的分页查询
		@RequestMapping(value= {"/findByCnameLimit"}) //@RequestMapping该注解为映射URL来执行方法
		public ModelAndView findByCnameLimit(Page page,HttpServletRequest request) throws UnsupportedEncodingException {
			Page p=new Page();
			String  currentPage=request.getParameter("currentPage");//获取从页面传过来的当前页面值
			
			if(currentPage==null) {//第一次执行传过来的一定是null值;所以在此做个判断
				p.setCurrentPage(page.getCurrentPage());
			}else {
				p.setCurrentPage(Integer.parseInt(currentPage));
			}
			
			p.setPageSize(page.getPageSize());
			String cname=request.getParameter("cname");
			int beginIndex=(p.getCurrentPage()-1)*p.getPageSize();
			p.setBeginIndex(beginIndex);
			int totalCounts=studentService.findCount(cname);//总条数
			p.setTotalCounts(totalCounts);
			int rootCount=(totalCounts%p.getPageSize()==0)?(totalCounts/p.getPageSize()):(totalCounts/p.getPageSize()+1);//做一个三目运算;来运算出可以分多少页
			p.setPageCount(rootCount);
			HashMap<String, Object> hasMap=new HashMap<>();
			hasMap.put("cname",cname);
			hasMap.put("page", p);
			List<Student> studentList=studentService.findByCnameLimit(hasMap);
			
			if(studentList!=null) {
				ModelAndView mv=new ModelAndView();
				mv.addObject("studentList", studentList);
				mv.addObject("cname", cname);
				mv.addObject("page", p);//把对象保存到hashMap中
				mv.setViewName("rearch");
				return mv;
			}
				return null;
		}

页面分页代码:

<table  width="700" >
			
				<tr>
				<td align="right"> <label>第${page.currentPage}/${page.pageCount}页 共${page.totalCounts}条</label></td>
				<td align="center"><font color="#999999"><a href="findByCnameLimit.action?cname=${cname}&currentPage=1" >首页</a></font></td>
				<td align="center"><font color="#999999"><a href="findByCnameLimit.action?cname=${cname}&currentPage=${page.currentPage-1}" onclick="return checkFirst()">上一页</a></font></td>
				<td align="center"><font color="#999999"><a href="findByCnameLimit.action?cname=${cname}&currentPage=${page.currentPage+1}" onclick="return checkNext()">下一页</a></font></td>
				<td align="center"><font color="#999999"><a href="findByCnameLimit.action?cname=${cname}&currentPage=${page.pageCount}">尾页</a></font></td>
				<td align="left"> 跳转到:
				   <input type="text" style="width:30px" id="turnPage" />页 
				   <input type="button" onclick="startTurn()" value="跳转" /></td>
			</table>

其中我运用到了js;主要是用来做一个页面超额或负值的判断;代码为:

 <script type="text/javascript">
 
    function checkFirst(){
         if(${page.currentPage>1}){
           return true;
         }
         alert("已到页首,无法加载更多");
       return false;
    }
    
    function checkNext(){
    if(${page.currentPage<page.pageCount}){
      return true;
    }
    alert("已到页尾,无法加载更多页");
    return false;
    }
    
     function startTurn(){
    	 var cname='${cname}';
    var turnPage=document.getElementById("turnPage").value;
    if(turnPage>${page.pageCount}){
      alert("对不起已超过最大页数");
      return false;
    } 
    var shref="findByCnameLimit.action?cname="+cname+"&currentPage="+turnPage;
    window.location.href=shref;
}
</script>

页面显示效果图:
在这里插入图片描述在这里插入图片描述总结:我相信这个世上的努力还是公平的;也许有很多的挫折;也许有很多都要去学习;但是只要心永恒;自己终究还是会感谢自己,加油吧少年;希望我的同学好友都能拿到自己所想要的结果,有问题的很欢迎大家评论。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小面包CC

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值