mysql +spring mvc+jsp+jquery怎么分页,Spring MVC+MyBatis+MySQL实现分页功能实例

Spring MVC+MyBatis+MySQL实现分页功能实例

这里有新鲜出炉的MyBatis教程,程序狗速度看过来!

MyBatis 基于Java的持久层框架

MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis 。

分页功能是我们日常开发中经常会遇到的,下面这篇文章主要给大家介绍了Spring MVC+MyBatis+MySQL实现分页功能的相关资料,文中介绍的非常详细,对大家具有一定的参考学习价值,需要的朋友们下面来一起看看吧。

前言

最近因为工作的原因,在使用SSM框架实现一个商品信息展示的功能,商品的数据较多,不免用到分页,查了一番MyBatis分页的做法,终于是实现了,在这里记录下来分享给大家,下面来一起看看详细的介绍:

方法如下:

首先写一个分页的工具类,定义当前页数,总页数,每页显示多少等属性。

/**

* 分页 工具类

*/

publicclassPageimplementsSerializable{

privatestaticfinallongserialVersionUID=-2213069645383858323L;

privateintpageNow=1;// 当前页数

privateintpageSize=4;// 每页显示记录的条数

privateinttotalCount;// 总的记录条数

privateinttotalPageCount;// 总的页数

privateintstartPos;// 开始位置,从0开始

privatebooleanhasFirst;// 是否有首页

privatebooleanhasPre;// 是否有前一页

privatebooleanhasNext;// 是否有下一页

privatebooleanhasLast;// 是否有最后一页

/**

* 通过构造函数 传入 总记录数 和 当前页

* @param totalCount

* @param pageNow

*/

publicPage(inttotalCount,intpageNow){

this.totalCount=totalCount;

this.pageNow=pageNow;

}

/**

* 取得总页数,总页数=总记录数/总页数

* @return

*/

publicintgetTotalPageCount(){

totalPageCount=getTotalCount()/getPageSize();

return(totalCount%pageSize==0)?totalPageCount:totalPageCount+1;

}

publicvoidsetTotalPageCount(inttotalPageCount){

this.totalPageCount=totalPageCount;

}

publicintgetPageNow(){

returnpageNow;

}

publicvoidsetPageNow(intpageNow){

this.pageNow=pageNow;

}

publicintgetPageSize(){

returnpageSize;

}

publicvoidsetPageSize(intpageSize){

this.pageSize=pageSize;

}

publicintgetTotalCount(){

returntotalCount;

}

publicvoidsetTotalCount(inttotalCount){

this.totalCount=totalCount;

}

/**

* 取得选择记录的初始位置

* @return

*/

publicintgetStartPos(){

return(pageNow-1)*pageSize;

}

publicvoidsetStartPos(intstartPos){

this.startPos=startPos;

}

/**

* 是否是第一页

* @return

*/

publicbooleanisHasFirst(){

return(pageNow==1)?false:true;

}

publicvoidsetHasFirst(booleanhasFirst){

this.hasFirst=hasFirst;

}

/**

* 是否有首页

* @return

*/

publicbooleanisHasPre(){

// 如果有首页就有前一页,因为有首页就不是第一页

returnisHasFirst()?true:false;

}

publicvoidsetHasPre(booleanhasPre){

this.hasPre=hasPre;

}

/**

* 是否有下一页

* @return

*/

publicbooleanisHasNext(){

// 如果有尾页就有下一页,因为有尾页表明不是最后一页

returnisHasLast()?true:false;

}

publicvoidsetHasNext(booleanhasNext){

this.hasNext=hasNext;

}

/**

* 是否有尾页

* @return

*/

publicbooleanisHasLast(){

// 如果不是最后一页就有尾页

return(pageNow==getTotalCount())?false:true;

}

publicvoidsetHasLast(booleanhasLast){

this.hasLast=hasLast;

}

}

接着Mapper接口中定义分类的方法

ab7653affab982b574eb7acc55df2e04.gif

传入两个参数,分别是开始页和每页显示记录的条数。

Mapper的映射文件中的SQL分页语句

select g.id,g.name,g.price,g.num,c.class_name,g.pic,g.des from tb_goods

g,tb_class c where g.class_id=c.cid limit #{startPos},#{pageSize}

接着在控制类(Controller)中查询商品,同时进行分页。

查询商品方法

/**

* 查询商品信息,实现分页

* @param goods

* @return

* @throws Exception

*/

@RequestMapping("/queryPages")publicStringqueryPages(HttpServletRequestrequest,Modelmodel)throwsException{

StringpageNow=request.getParameter("pageNow");

Pagepage=null;

Listgoods=newArrayList();

inttotalCount=(int)service.getGoodsCount(1);

if(pageNow!=null){

page=newPage(totalCount,Integer.parseInt(pageNow));

goods=this.service.findPages(page.getStartPos(),page.getPageSize());

}else{

page=newPage(totalCount,1);

goods=this.service.findPages(page.getStartPos(),page.getPageSize());

}

model.addAttribute("goods_list",goods);

model.addAttribute("page",page);

return"goods/FenYeTest";

}

这里需要注意一下,遍历商品时没有点击页数,所以当前页默认是空的,这里要在方法里面进行判断,如果pageNow为空,传一个1到构造函数,否则会报空指针。

最后,在jsp页面显示查询的商品列表,同时实现点击上一页,下一页,首页,尾页等常见分页中的操作

pageEncoding="UTF-8"%>

商品查询列表选择商品名称商品价格生产数量商品类别商品图片商品信息操作${item.id}${item.name }${item.price }${item.num }${item.classInfo.class_name }

src="http://localhost/pic/${item.pic }">

${item.des }

修改

删除

${page.pageNow} 页首页

0}">

上一页

上一页

下一页

下一页

= page.totalPageCount}">

下一页

尾页

尾页

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值