mybatis分页原理实现

来源:互联网 发布:java调用mysql连接池 编辑:程序博客网 时间:2024/06/05 08:21

第一,需要一个封装好的page类

需要有五个属性

private int totalNum;  //4.总条数  private int  pageNo;   //2.页码 private int pageSize;       //3.每页显示记录数 private int tatalPage;  //1.总页数private  List<Emp> list;  //需要显示的数据public Page(int totalNum, int pageNo, int pageSize,                List<Emp> list) {            this.totalNum = totalNum;            this.pageNo = pageNo;            this.pageSize = pageSize;            int temp=totalNum/pageSize;            this.tatalPage = totalNum%pageSize==0?temp:temp+1;            this.list = list;}

前台只需要传入当前页码即可,每页显示的的条数事先写好的。
总条数写一个方法得到,注:这里用的是mybatis

//得到总页数public int  list(String name,String i){    Map<String, Object> map=new HashMap<String, Object>();    map.put("pid", i);    //根据id查找    map.put("pname", name);     //根据名字模糊查找    SqlSession sqlSession = sqlSessionFactory.openSession();    List<Emp> selectList = sqlSession.selectList("empNamespace.listAll",map);    int size = selectList.size();    return size;}//mapping、<select id="listAll" resultMap="empMap" parameterType="map">                SELECT * from emps                 <where>                    <if test="pname!= null">                         and ename like concat('%',#{pname},'%')                    </if>                    <if test="pid!=null  and pid!= '' ">                        and  eid =#{pid}                    </if>                </where></select>

得到page

public Page listEmp(String name,String i,int pageNo,int pageSize) {    int totalNum=list(name,i);    // //4.总条数       Map<String, Object> map=new HashMap<String, Object>();    map.put("pid", i);    //根据id查找    map.put("pname", name);     //根据名字模糊查找    int start =(pageNo-1)*pageSize;    map.put("start",start );      //开始    map.put("end", pageSize);  //结束    System.out.println(map);    SqlSession sqlSession = sqlSessionFactory.openSession();    List<Emp> list=sqlSession.selectList("empNamespace.list",map);    sqlSession.close();    Page page = new Page(totalNum,pageNo,pageSize,list);    return page;}mapping<select id="list" resultMap="empMap" parameterType="map">                SELECT * from emps                 <where>                    <if test="pname!= null">                         and ename like concat('%',#{pname},'%')                    </if>                    <if test="pid!=null  and pid!= '' ">                        and  eid =#{pid}                    </if>                </where>                limit #{start},#{end}</select>

在action中代码

注意:最开始pageno需要事先判断一下。赋初值,因为刚开始是null

private  int pageSize=3;  @RequestMapping(value="/listAll")    public String listMethod(Model model,String nameBy,String idBy,Integer  pageNo) throws Exception{    //调用业务层            if(pageNo == null ){                pageNo=1;            }            System.out.println("名字为:"+nameBy);            System.out.println("id名字为:"+idBy);            System.out.println("pageNo名字为:"+pageNo);            System.out.println("----------------------------------------------------------");            Page page = empService.listAll(nameBy,idBy,pageNo,pageSize);            model.addAttribute("page", page);            model.addAttribute("nameBy", nameBy);//回显            System.out.println(page.getList());            return "list";    }

在jsp中代码:

别忘了设置隐藏pageNo。

<input type="hidden" name="pageNo" id="pageNo" value="${ nameBy}"><c:choose>                <c:when test="${ page.totalNum >0}">            一共${ page.totalNum}条记录,一共${ page.tatalPage}页,当前${ page.pageNo}页,            <a href="javascript:goPage(1)">首页</a>                 <c:if test="${(page.pageNo - 1) >0 }">                    <a href="javascript:goPage(${ page.pageNo }-1)">上一页</a>                                </c:if>                <c:if test="${(page.pageNo ) <  page.tatalPage}">                    <a href="javascript:goPage(${ page.pageNo }+1)">下一页</a>                                </c:if>            <a href="javascript:goPage(${ page.tatalPage })">末页</a>             </c:when>            <c:otherwise>                      <font color="red">没有查询到数据!</font>            </c:otherwise></c:choose><script  type="text/javascript">        function goPage(pageNo){                document.getElementById("pageNo").value = pageNo;                var form = document.getElementById("form1");                form.submit();        }</script>