Use pageHelper to complete paging in ssm

First add the package in the pom file

  <!--分页-->
    <dependency>
      <groupId>com.github.pagehelper</groupId>
      <artifactId>pagehelper</artifactId>
      <version>5.1.2</version>
    </dependency>

Then add the configuration to the configuration file. I have springContext.xml here, and some people have applicationContext.xml. Anyway, it is the configuration file with database information.

  <!--分页配置-->
    <bean class="com.github.pagehelper.PageInterceptor">
        <property name="properties">
            <props>
                <prop key="helperDialect">mysql</prop>
                <prop key="reasonable">true</prop>
            </props>
        </property>
    </bean>

Then it is to modify the method in the service. What I have done here is the first jump after logging in, searching all articles in the database and displaying them on the homepage.

添加两个参数,page(当前页数),pagesize(当前页显示几条信息)
PageHelper.start是这个插件自带的方法。

 public List<Page> findAll(Integer page,Integer pagesize) {
    
    
        PageHelper.startPage(page,pagesize);
        return pageMapper.findAll();
    }

Then modify the control layer
@RequestParam: The parameters passed in GET and POST requests will be automatically converted and assigned to the variables annotated by @RequestParam.
DefaultValue: Set a default
value to pass the value obtained from the service into PageInfo, and then put it into the model pass To the front end

 public ModelAndView tiaozhuan(@RequestParam(defaultValue = "2")Integer page,@RequestParam(defaultValue = "6")Integer pagesize, Model model){
    
    
        List<Page> list = pageService.findAll(page,pagesize);
        PageInfo pi = new PageInfo(list);
        ModelAndView mv = new ModelAndView();
        mv.addObject("pagelists",pi);
        mv.setViewName("/lunbo/findAll.do");
        int size = list.size();
        model.addAttribute("size",size);
        model.addAttribute("pagelist",list);
        return mv;
    }

Modify the front end again and you're done.

<div class="box-tools pull-right">
                <ul class="pagination">
                    <li><a href="${pageContext.request.contextPath}/page/alllist.do?page=1&pagesize=6" aria-label="Previous">首页</a></li>
                    <li><a href="${pageContext.request.contextPath}/page/alllist.do.do?page=${pagelists.pageNum-1}">上一页</a></li>
                    <c:forEach begin="1" end="${pagelists.pages}" var="pagenum">

                        <li><a href="${pageContext.request.contextPath}/page/alllist.do.do?page=${pagenum}&pagesize=6">${pagenum}</a></li>

                    </c:forEach>

                    <li><a href="${pageContext.request.contextPath}/page/alllist.do.do?page=${pagelists.pageNum+1}">下一页</a></li>
                    <li><a href="${pageContext.request.contextPath}/page/alllist.do.do?page=${pagelists.pages}&pagesize=6" aria-label="Next">尾页</a></li>
                </ul>
            </div>

Final rendering
Insert picture description here

Guess you like

Origin blog.csdn.net/hzl529/article/details/103000989