ssm框架下的分页查询

一、开发项目时,对于多条记录的显示,我们采用分页查询将会更加方便。

二、基于ssm框架,我们通过一个记录的起始值start,和记录的大小size封装到一个list集合里,再通过Page类进行封装,然后遍历出来。

1、数据库表的 设计



2、编写 一个Page的实体类

public class Page<T> {//封装某个类
        private int currPage;//当前页数
        public Page() {
            super();
        }
        public int getCurrPage() {
            return currPage;
        }

        public void setCurrPage(int currPage) {
            this.currPage = currPage;
        }

        public int getPageSize() {
            return pageSize;
        }

        public void setPageSize(int pageSize) {
            this.pageSize = pageSize;
        }

        public int getTotalCount() {
            return totalCount;
        }

        public void setTotalCount(int totalCount) {
            this.totalCount = totalCount;
        }

        public int getTotalPage() {
            return totalPage;
        }

        public void setTotalPage(int totalPage) {
            this.totalPage = totalPage;
        }

        public List<T> getList() {
            return list;
        }

        public void setList(List<T> list) {
            this.list= list;
        }

        private int pageSize;//每页显示的记录数

        private int totalCount;//总记录数

        private int totalPage;//总页数

        private List<T> list;//封装某个页面

}

如果照着我的数据表去设计,也需要Qualification表的实体类。

3、Mapper层

    public int selectCount();//查询用户记录总数;
    public List<Qualification> findByPage(HashMap<String,Object> map);//通过起始记录,和记录size放到一个hashMap再封装到List里,

4、Mybatis配置Sql

<mapper namespace="com.lysoc.jmi.tracing.mapper.QualificationMapper">
  <resultMap id="BaseResultMap" type="com.lysoc.jmi.tracing.model.Qualification">

<select id="findByPage" parameterType="map" resultMap="BaseResultMap">
          select * from qualification   
          <if test="start!=null and size!=null">//不允许为空
              limit #{start},#{size}//起始的记录索引,记录总数
          </if>
  </select>

5、服务层Service

服务层用来编写接口和实现类,此处我直接写一个实现类QuaService

@Service//声明一下
public class QuaService {
    
    @Autowired
    QualificationMapper qmap;//注入Mapper
    
  
   public Page<Qualification> findByPage(int currentPage){
        HashMap<String,Object> map=new HashMap<String,Object>();
        Page<Qualification> page=new Page<Qualification>();
        //封装当前页数
        page.setCurrPage(currentPage);
         //每页显示的数据
        int pageSize=4;
        page.setPageSize(pageSize);
        //封装总记录数
        int totalCount=qmap.selectCount();
        page.setTotalCount(totalCount);
        //封装总页数
        double totalcount=totalCount;
        Double num=Math.ceil(totalcount/pageSize);
        page.setTotalPage(num.intValue());//转换为整数
        map.put("start", (currentPage-1)*pageSize);//起始的记录值
        map.put("size", page.getPageSize());//获取每个页面的pageSize
        List<Qualification> list=qmap.findByPage(map);
        page.setList(list);
        return page;返回一个page
        
    }

}

6、Controller

package com.lysoc.jmi.tracing.controller;

import java.sql.Array;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.ibatis.annotations.Param;
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.lysoc.jmi.tracing.mapper.QualificationMapper;
import com.lysoc.jmi.tracing.model.Page;
import com.lysoc.jmi.tracing.model.Qualification;
import com.lysoc.jmi.tracing.service.user.QuaService;

@Controller
public class QualificationController {
        @Autowired
        private QuaService quaService;
        
     
        //分页查询
        @RequestMapping("/Mainpage")  //一个请求
        public ModelAndView getPage(@RequestParam(defaultValue="1") int currentPage,Model model,HttpServletRequest request){    
           Page<Qualification> page= quaService.findByPage(currentPage) ;
           model.addAttribute("pagemsg",page );//设置一个属性
          return new ModelAndView("qualification_main");
        }

}

7、qualification_main.jsp

<table id="form">
    <tr><th><th>序号</th><th>类别</th><th>名称</th><th>描述</th></tr>
    <c:forEach items="${pagemsg.list}" var="qua">//进行遍历
    <tr>
        <td><input type="checkbox" name="ids" value="${qua.id }"/></td>
        <td>${qua.id}</td>
        <td>${qua.classification}</td>
        <td>${qua.name}  </td>
        <td>${qua.description}</td>
    </tr>
    
    </c:forEach>
    
</table>

<table >
    <tr>
    <td class="td2">
   <span>第${requestScope.pagemsg.currPage}/ ${requestScope.pagemsg.totalPage}页</span> 
   <span>总记录数:${requestScope.pagemsg.totalCount } 每页显示:
                   ${requestScope.pagemsg.pageSize}</span> 
   <span>
       <c:if test="${requestScope.pagemsg.currPage != 1}">
           <a href="${pageContext.request.contextPath }/Mainpage?currentPage=1">[首页]</a> 
           <a href="${pageContext.request.contextPath }/Mainpage?currentPage=${requestScope.pagemsg.currPage-1}">[上一页]</a> 
       </c:if>
       <c:if test="${requestScope.pagemsg.currPage != requestScope.pagemsg.totalPage}">
           <a href="${pageContext.request.contextPath }/Mainpage?currentPage=${requestScope.pagemsg.currPage+1}">[下一页]</a>  
           <a href="${pageContext.request.contextPath }/Mainpage?currentPage=${requestScope.pagemsg.totalPage}">[尾页]</a>  
       </c:if>

   </span>
    </td>
    </tr>
    </table>



8、效果图



 


总结:时间仓促,精力有限,这个编辑器用着不太灵活,代码写得不整洁还望见谅,希望能帮到那些Java爱好者。


  


 

  • 5
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 8
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

源码客栈-逍遥游

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

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

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

打赏作者

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

抵扣说明:

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

余额充值