JavaWeb12(实现基础分页&模糊查询的分页)

目录

一. 效果预览

​编辑 二. 实现基本分页

2.1 分页sql

 --每页3条  取第二页

--由于伪列不能作用与大于符号也不能作用于between....and

--因此需要将伪列----->名列

 2.2 万能公式

 2.3 首页&上一页&下一页实现

②前端代码

2.4 末页实现&优化

①底层代码

②前端优化

 三.实现模糊查询(下拉框)的分页

3.1 确定sql语句

--查询简介中含有'商'的商品 每页2条  取第二页

--查询简介中含有'你'的商品 每页3条  取第二页

3.2 代码实现

①底层代码

         模糊查询的分页         

②前端优化

        拿到模糊查询分页后的商品集合


一. 效果预览


 


 


 二. 实现基本分页

2.1 分页sql

 --每页3条  取第二页

--由于伪列不能作用与大于符号也不能作用于between....and

--因此需要将伪列----->名列

select * from(
       select a.*,rownum as rid from tb_goods a where gname like '%商%'
) b where b.rid between 5 and 8

 2.2 万能公式

/*

*如果每页3条即pageSize=3

*当pageIndex=1:1-3

*当pageIndex=2:4-6

*当pageIndex=3:7-9

*

*如果每页5条即pageSize=5

*当pageIndex=1:1-5

*当pageIndex=2:6-10

*当pageIndex=3:11-15

*/

(pageIndex-1)*pageSize+1-------(pageIndex*pageSize)

 2.3 首页&上一页&下一页实现

①底层代码

    /**
         * 模糊查询的分页
         * @param pageIndex 设置第几页
         * @param pageSize  每页多少条数据
         * @param str 关键字
         * @param colName 列名
         * @return 商品集合
         */

        public List<Goods> page(int pageIndex,int pageSize,String str,String colName){
            List<Goods> ls = new ArrayList<>();
            try {
                con=DBHelper.getCon();
                String sql="select * from(\r\n" + 
                        "       select a.*,rownum as rid from tb_goods a where "+colName+" like '%"+str+"%' order by gid desc \r\n" + 
                        ")b where b.rid between ? and ?";
                ps=con.prepareStatement(sql);
                //给占位符赋值
                ps.setInt(1, (pageIndex-1)*pageSize+1);
                ps.setInt(2, (pageIndex*pageSize));
                //System.out.println("SQL:"+sql); 打印出来为null 在前端要判断 goodsServlet
                rs=ps.executeQuery();
                while(rs.next()) {
                    Goods g = new Goods();
                    g.setGid(rs.getInt(1));
                    g.setGname(rs.getString(2));
                    g.setGprice(rs.getDouble(3));
                    g.setGinfo(rs.getString(4));
                    g.setGtime(rs.getString(5));
                    g.setGpath(rs.getString(6));
                    g.setGxl(rs.getInt(7));
                    g.setTid(rs.getInt(8));
                    ls.add(g);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                DBHelper.myClose(con, ps, rs);
            }
            return ls;
            
        }

    
        int pageIndex=1;//当前页 第几页
        int pageSize=3;//每页多少条数据
        
        //接收参数
        String pid = req.getParameter("pid");
        
        if(pid!=null) {//说明点了"首页"/"上一页"/"下一页"/"末页"

            //改变页码
            pageIndex=Integer.parseInt(pid);
        }
       
       //拿到集合
      
 List<Goods> ls = igd.page(pageIndex, pageSize);

        //把当前页码存起来

        req.setAttribute("pageIndex,"pageIndex)

        
    } 

②前端代码

<!-- 分页 -->
        <nav aria-label="Page navigation example">
         <ul class="pagination justify-content-center pagination-lg">
        <li class="page-item" ><a class="page-link" href="goods.do?pid=1">首页</a></li>
          <li class="page-item " id="prev"><a class="page-link" href="goods.do?pid=$goods.do?pid=${pageIndex-1}">上一页</a></li>
          <li class="page-item "><a class="page-link text-info"><span id="ys"></span>/<span id="max"></span></a></li>
          <li class="page-item " id="next"><a class="page-link" href="goods.do?pid=1"${pageIndex+1}>下一页</a></li>
          <li class="page-item"><a class="page-link" href="goods.do?pid=${max}">末页</a></li>
  </ul>
</nav>

2.4 末页实现&优化

①底层代码

/**
         * 获取总行数
         * @param str 表名等
         * @return 总行数
         */

        public int getRows(String str) {
            int n = 0;
            try {
                con=DBHelper.getCon();
                String sql="select count(*) from "+str;
                ps=con.prepareStatement(sql);
                rs=ps.executeQuery();
                if(rs.next()) {
                    n=rs.getInt(1);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                DBHelper.myClose(con, ps, rs);
            }
            return n;
        }

---【在业务逻辑层biz做处理】---

/*

*拿到最大页码

*@param tabName 表名等
* @param pageSize 每页多少条数据

*@return 最大页码

*/

public int getMax(String str,int pageSize) {
            //拿到总行数
            int rows = igd.getRows(str);
            //计算总页数
            int max = rows/pageSize;//10/3=3
            if(rows%pageSize!=0) {//除不尽
                max++;//多加一页
            }
            return max;
        }

 //拿到最大页码

 int max = igd.getMax("tb_goods ",pageSize)

//把最大页码存起来

req.setAttribute("max", max);

②前端优化

 三.实现模糊查询(下拉框)的分页

3.1 确定sql语句

--查询简介中含有'商'的商品 每页2条  取第二页

select * from(
       select a.*,rownum as rid from tb_goods a where gname like '%商%'
) b where b.rid between 5 and 8

--查询简介中含有'你'的商品 每页3条  取第二页
select * from(
       select a.*,rownum as rid from tb_goods a where gname like '%你%'
) b where b.rid between 1 and 3

3.2 代码实现

①底层代码

/**
         * 模糊查询的分页
         * @param pageIndex 设置第几页
         * @param pageSize  每页多少条数据
         * @param str 关键字
         * @param colName 列名
         * @return 商品集合
         */

        public List<Goods> page(int pageIndex,int pageSize,String str,String colName){
            List<Goods> ls = new ArrayList<>();
            try {
                con=DBHelper.getCon();
                String sql="select * from(\r\n" + 
                        "       select a.*,rownum as rid from tb_goods a where "+colName+" like '%"+str+"%' order by gid desc \r\n" + 
                        ")b where b.rid between ? and ?";
                ps=con.prepareStatement(sql);
                //给占位符赋值
                ps.setInt(1, (pageIndex-1)*pageSize+1);
                ps.setInt(2, (pageIndex*pageSize));
                //System.out.println("SQL:"+sql); 打印出来为null 在前端要判断 goodsServlet
                rs=ps.executeQuery();
                while(rs.next()) {
                    Goods g = new Goods();
                    g.setGid(rs.getInt(1));
                    g.setGname(rs.getString(2));
                    g.setGprice(rs.getDouble(3));
                    g.setGinfo(rs.getString(4));
                    g.setGtime(rs.getString(5));
                    g.setGpath(rs.getString(6));
                    g.setGxl(rs.getInt(7));
                    g.setTid(rs.getInt(8));
                    ls.add(g);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                DBHelper.myClose(con, ps, rs);
            }
            return ls;
            
        }

②前端优化

//拿到模糊查询分页后的商品集合
 List<Goods> ls = igd.page(pageIndex, pageSize, str, xl);

  • 7
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

瑶大头*^_^*

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

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

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

打赏作者

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

抵扣说明:

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

余额充值