mvc+mysql实现的分页技术

论坛 期权论坛 脚本     
匿名技术用户   2021-1-4 01:28   566   0

上两个星期,我们小组一起做了个项目,网上商城的项目,我负责了分页这一块,第一次做项目,感觉学习到了不少。下面就分享下自己的代码吧!

1.javabean的代码这里就不贴了。

2.定义了一个接口,在接口中定义了一个方法public ArrayList ShangyiGoodsInfo(int Nowpage),详细代码如下:

package com.lucky.dao;
import java.util.ArrayList;
import com.lucky.javabean.IndexGoodsBean;
public interface IndexGoodsDao {
public ArrayList ShangyiGoodsInfo(int Nowpage)throws Exception;
}

3.引用该接口,并实现方法,详细的代码如下:

package com.lucky.daoimpl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import com.lucky.connection.IndexGoodsConnection;
import com.lucky.dao.IndexGoodsDao;
import com.lucky.javabean.IndexGoodsBean;


public class IndexGoodsDaoImpl implements IndexGoodsDao{
private Connection conn=null;
private int RowCount=0;
public IndexGoodsDaoImpl(Connection conn){
this.conn=conn;
}

@Override
public ArrayList ShangyiGoodsInfo(int Nowpage) throws Exception {
int startpage=(Nowpage-1)*24;//每一页从第几条数据开始显示
System.out.println(Nowpage);
ArrayList ShangyiAL=new ArrayList();
conn=IndexGoodsConnection.getCon();
String sql="select goods.goodsname,goodsimage,goods_info_price from goods,goods_info where goodstype='上衣' and goods.goodsname=goods_info.goodsname group by goods.goodsname,goodsimage,goods_info_price LIMIT "+startpage+",24";
PreparedStatement pstmt=conn.prepareStatement(sql);
ResultSet rs=pstmt.executeQuery();
while(rs.next()){
IndexGoodsBean goodsbean=new IndexGoodsBean();
goodsbean.setGoodsname(rs.getString(1));
goodsbean.setGoodsimage(rs.getString(2));
goodsbean.setGoodsprice(rs.getDouble(3));
ShangyiAL.add(goodsbean);
}
return ShangyiAL;//返回每页显示的商品集合
}
public int pageCount()throws Exception{//定义方法,总条数

conn=IndexGoodsConnection.getCon();
String sql="select count(*) from goods where goodstype='上衣'";
PreparedStatement psmt=conn.prepareStatement(sql);
ResultSet rs=psmt.executeQuery();
if(rs.next()){
RowCount=rs.getInt(1);
}
return RowCount;//返回数据库中商品的总条数
}
}

4.在servlet对返回的数据进行处理,并实现分页:

package com.lucky.servlet;
import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.lucky.daoimpl.IndexGoodsDaoImpl;
import com.lucky.javabean.IndexGoodsBean;
import com.lucky.javabean.Pagebean;
public class ProductServlet extends HttpServlet {

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doPost(req, resp);
}


@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
Integer page=Integer.parseInt(req.getParameter("page"));//获得当前页
Pagebean pageBean=new Pagebean();
pageBean.setFirstPage(1);
pageBean.setNowpage(page);
IndexGoodsDaoImpl indexgooodsdaoimpl=new IndexGoodsDaoImpl();
ArrayList shangyi;
try {
shangyi = indexgooodsdaoimpl.ShangyiGoodsInfo(page);
req.setAttribute("shangyi",shangyi);//将商品集合封装到request对象中
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int allCount=0;
try {
allCount = indexgooodsdaoimpl.pageCount();//获得返回的商品总数
int allpages=((allCount%24==0)?(allCount/24):(allCount/24+1));//计算出总页数
pageBean.setNextPage((page==allpages)?(allpages):(page+1));//计算出下一页
pageBean.setPreviousPage((page==1)?(1):(page-1));//计算出上一页
pageBean.setAllPages(allpages);
pageBean.setLastpage(allpages);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
req.setAttribute("page", pageBean);
req.getRequestDispatcher("shangyi.jsp").forward(req, resp);
}
}

5,在jsp页面对分页进行处理:

<div class="fenye">
<a href="ProductServlet?page=${page.firstPage}&pagecount=24"> 首页</a>&nbsp;
<c:choose>
<c:when test="${page.nowpage eq 1}">
<a href="javascript:window.alert('亲,已经是第一页哦!')"> 上一页</a>&nbsp;
</c:when>
<c:otherwise>
<a href="ProductServlet?page=${page.previousPage }&pagecount=24"> 上一页</a>&nbsp;
</c:otherwise>
</c:choose>

<c:choose>
<c:when test="${page.nowpage eq page.lastpage}">
<a href="javascript:window.alert('亲,已经是最后一页哦!')"> 下一页</a>&nbsp;
</c:when>
<c:otherwise>
<a href="ProductServlet?page=${page.nextPage}&pagecount=24"> 下一页</a>&nbsp;
</c:otherwise>
</c:choose>
<a href="ProductServlet?page=${page.lastpage }&pagecount=24"> 尾页</a>&nbsp;
当前第${page.nowpage}页/总共${page.allPages}页&nbsp;
</div>

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP