①ssm:ssm也就是spring+springmvc+mybaits三大框架,通过对以前的jdbc+servlet+mysql实现的分页效果进行整合和修改实现一个比较简单的分页功能..
分页工具类:PageUtil.(这是一个基本上的通用的工具类吧,都差不多,这个工具类,没有写其他方法,是一个泛型类吧)。
package com.toggery.util;
import java.util.List;
/**
* 分页工具类.泛型类
*/
public class PageUtil{
//属性
private int pageNumber;//总记录数
private int pageCount;//总页数
private int pageIndex;//当前页
private int pageSize;//每页大小
private Listlist;//当前页的数据
public int getPageNumber() {
return pageNumber;
}
public void setPageNumber(int pageNumber) {
this.pageNumber = pageNumber;
}
public int getPageCount() {
return pageCount;
}
public void setPageCount(int pageCount) {
this.pageCount = pageCount;
}
public int getPageIndex() {
return pageIndex;
}
public void setPageIndex(int pageIndex) {
this.pageIndex = pageIndex;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
}
②下面是相关的实体类:
package com.toggery.entity;
import java.util.List;
import org.springframework.stereotype.Repository;
import com.toggery.util.PageUtil;
/**
* 服装信息表
*/
@Repository
public class ClothInfo extends PageCloths{
private int clothId;//服装编号
private String clothName;//服装名称
private int clothTVolume;//总销量
private int clothMVolume;//月销量
private int clothPoint;//积分
private String clothYear;//出厂年份
private int clothState;//状态(是否已下架)
private String clothImg;//图片信息
private double clothPrice; //衣服价格(用于价格固定的商品)
private double clothCutPrice;//衣服折扣价
public double getClothPrice() {
return clothPrice;
}
public void setClothPrice(double clothPrice) {
this.clothPrice = clothPrice;
}
public double getClothCutPrice() {
return clothCutPrice;
}
public void setClothCutPrice(double clothCutPrice) {
this.clothCutPrice = clothCutPrice;
}
private List clothDetails;//外键属性
public List getClothDetails() {
return clothDetails;
}
public void setClothDetails(List clothDetails) {
this.clothDetails = clothDetails;
}
public String getClothImg() {
return clothImg;
}
public void setClothImg(String clothImg) {
this.clothImg = clothImg;
}
public int getClothId() {
return clothId;
}
public void setClothId(int clothId) {
this.clothId = clothId;
}
public String getClothName() {
return clothName;
}
public void setClothName(String clothName) {
this.clothName = clothName;
}
public int getClothTVolume() {
return clothTVolume;
}
public void setClothTVolume(int clothTVolume) {
this.clothTVolume = clothTVolume;
}
public int getClothMVolume() {
return clothMVolume;
}
public void setClothMVolume(int clothMVolume) {
this.clothMVolume = clothMVolume;
}
public int getClothPoint() {
return clothPoint;
}
public void setClothPoint(int clothPoint) {
this.clothPoint = clothPoint;
}
public String getClothYear() {
return clothYear;
}
public void setClothYear(String clothYear) {
this.clothYear = clothYear;
}
public int getClothState() {
return clothState;
}
public void setClothState(int clothState) {
this.clothState = clothState;
}
@Override
public String toString() {
return "clothInfo [clothId=" + clothId + ", clothName=" + clothName
+ ", clothTVolume=" + clothTVolume + ", clothMVolume="
+ clothMVolume + ", clothPoint=" + clothPoint + ", clothYear="
+ clothYear + ", clothState=" + clothState + ",clothImg=" + clothImg + "]";
}
}
③与实体类相对应的接口类ClothInfoDao,由于界面的原因,这理写的方法只传一个limit中的从第几条数据开始取的数值。因为一个界面只显示4条数据。也就是pageSize.
package com.toggery.dao;
import java.util.List;
import com.toggery.entity.ClothInfo;
import com.toggery.util.PageUtil;
/**
* 服装 dao接口
* @author Administrator
*
*/
public interface ClothInfoDao {
//分页显示所有衣服
public List showlist(int index);
④接口对应的映射文件ClothInfoDao.xml.
<mapper namespace="com.toggery.dao.ClothInfoDao">
<resultMap type="ClothInfo" id="ClothinfoResult">
<id column="clothId" property="clothId"/>
<result column="clothName" property="clothName" />
<result column="clothPrice" property="clothPrice"/>
<result column="clothCutPrice" property="clothCutPrice"/>
<result column="clothImg" property="clothImg"/>
resultMap>
<select id="showlist" parameterType="int" resultMap="ClothinfoResult">
select clothId,clothName,clothPrice,clothCutPrice,clothImg from clothinfo limit #{index},4
select>
<select id="pagecount" resultType="int">
select count(*) from clothinfo
select>
⑤相应的service.
package com.toggery.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.toggery.dao.ClothInfoDao;
import com.toggery.entity.ClothInfo;
/**
* 服装信息service
* @author Administrator
*
*/
@Service
public class ClothInfoService {
@Autowired
ClothInfoDao dao;
//获取总页数
public int pagecount(){
return dao.pagecount();
}
//分页显示所有衣服
public List showlist(int index){
return dao.showlist(index);
}
⑥相应的控制器ClothInfoController.java
package com.toggery.controller;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import com.mysql.fabric.xmlrpc.base.Array;
import com.toggery.entity.ClothDetail;
import com.toggery.entity.ClothInfo;
import com.toggery.entity.PageCloths;
import com.toggery.service.ClothDetailService;
import com.toggery.service.ClothInfoService;
import com.toggery.util.PageUtil;
/**
* 服装controller
*
* @author Administrator
*
*/
@Controller
@RequestMapping("/test")
public class ClothInfoController {
@Autowired
ClothInfoService service;
@Autowired
ClothInfo clothInfo;
//分页显示列表
@RequestMapping("/page")
public ModelAndView showlist(HttpServletRequest request, Model model) {
int pageIndex = 1;//设置初始的当前页,页面显示的都是第一页
int pageSize = 4;//设置每一页显示几条数据,用于计算总页数,此处设置的值必须与sql语句的limit最后的数值一样
PageUtil pageUtil = new PageUtil();//初始化工具类
List list = new ArrayList();
if (request.getParameter("pageIndex") != null) {
pageIndex = Integer.parseInt(request.getParameter("pageIndex"));
}//对页面上的分页标签传的值,进行获取,也是就点击'上一页或者下一页'传过来的pageindex
pageUtil.setPageIndex(pageIndex);//保存至工具类
int number = service.pagecount();//调用service层方法计算出总数据量,就是多少条数据.
pageUtil.setPageNumber(number);//保存至工具类
pageUtil.setPageSize(pageSize);//保存至工具类
pageUtil.setPageCount((int) Math.ceil((double) (pageUtil
.getPageNumber() / pageUtil.getPageSize())) + 1);//计算出总页数,并封装到工具类
int index = (pageIndex - 1) * pageSize;//计算出每一页从数据库中第几条数据开始取值,也就是limit后面的第一个数字
list = service.showlist(index);//调用service层的方法,取得数据库中的值
pageUtil.setList(list);//保存到工具类中的集合
model.addAttribute("pageUtil", pageUtil);//传递到页面,存入值栈中
return new ModelAndView("menpage");//跳转的相关页面
⑦相应的jsp页面,这里就贴关于分页的一些标签
<div class='page all'>
<b>共${pageUtil.pageNumber}b>条,当前第<span>${pageUtil.pageIndex}span>页
<a href="test/page?pageIndex=1" class='first'>首页a>
<a href="test/page?pageIndex=${pageUtil.pageIndex>1?pageUtil.pageIndex-1:1}" class='pre'>上一页a>
<c:forEach begin="1" end="${pageUtil.pageCount}" var="i">
<a href="test/page?pageIndex=${i}" style="text-decoration: none;">${i}a>
c:forEach>
<a href="test/page?pageIndex=${pageUtil.pageIndex class='next'> 下一页a>
<a href="test/page?pageIndex=${pageUtil.pageCount}" class='last'>末页a>
div>
个人理解其实分页就是将读取到所有内容装进一个容器,然后对这个容器进行操作就行了,比如在读取到数据库中所有的衣服信息,取的是一个list集合,通过分页工具类的setlist方法将其封装进去,然后在值栈中,对其进行遍历,同样也能取到值。初始的limit的后面的数字设置为0,也就是index=0,显示出第一页,数据是从第一条开是取,一页只有4条(可以随便设置)。当点击下一页的时候,进行判断,如果当前页是不是最后一页,则传入当前页的pageIndex+1即可,通过改变pageindex的值从而改变index的值,所以每一页显示的结果自然也会不同..(想法上有错误的,欢迎指出,正在学习中ing….)