How to implement paging operation in SMM framework

Keywords: xml github Database Maven

1. Import the PageHelper jar package, add the following dependencies to pom.xml under the parent project, and refresh it with Maven on the right (in idea)

  <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>5.1.2</version>
 </dependency>

2. Add the configuration information of PageHelper to applicationContext.xml under the control layer

<!-- 3.To configure SqlSessionFactory object -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- Inject database connection pool -->
        <property name="dataSource" ref="dataSource"/>
        <!-- scanning bean Package use alias -->
        <property name="typeAliasesPackage" value="edu.whut.bean"></property>

        <!--Configure load mapping file UserMapper.xml-->
        <property name="mapperLocations" value="classpath:mapper/*.xml"/>

        <!--To configure pagehelper-->
        <property name="plugins">
            <array>
                <bean class="com.github.pagehelper.PageInterceptor">
                    <property name="properties">
                        <props>
                            <prop key="helperDialect">mysql</prop><!--Database you are connected to-->
                            <prop key="reasonable">true</prop><!--Prevent pages from exceeding the limit-->
                        </props>
                    </property>
                </bean>
            </array>
        </property>

    </bean>

3. Set the startPage in the implementation part of the service layer

public List<UserInfo> listAll(int page,int size) {
        PageHelper.startPage(page,size);
        return userDao.listAll();
    }

4. In the control layer, give the result set queried to PageInfo, and set the default page number and size.

@RequestMapping("listAll.do")
    public ModelAndView listAll(@RequestParam(value = "page",defaultValue = "1") int page,
                                @RequestParam(value = "size",defaultValue = "5") int size){
        List<UserInfo> users = userService.listAll(page,size);
        PageInfo pageInfo = new PageInfo(users);
        ModelAndView mv = new ModelAndView();
        mv.addObject("infos",pageInfo);
        mv.setViewName("user-list");
        return mv;
    }

5. Implement the next page Jump in the corresponding display jsp, where infos is the result set mv.addObject("infos", pageInfo) saved in the corresponding control layer.
(ps: if ModelAndView is not used in the control layer, but the url string is used for control jump, the Model can be used for data storage and transmission)

<ul class="pagination">
						<li><a href="${pageContext.request.contextPath}/user/listAll.do?page=1&size=5" aria-label="Previous">home page</a></li>
						<li><a href="${pageContext.request.contextPath}/user/listAll.do?page=${infos.pageNum-1}&size=5">Previous page</a></li>
                        <c:forEach begin="1" end="${infos.pages}" var="pageNum">
                            <li><a href="${pageContext.request.contextPath}/user/listAll.do?page=${pageNum}&size=5">${pageNum}</a></li>
                        </c:forEach>
						<li><a href="${pageContext.request.contextPath}/user/listAll.do?page=${infos.pageNum+1}&size=5">next page</a></li>
						<li><a href="${pageContext.request.contextPath}/user/listAll.do?page=${infos.lastPage}&size=5" aria-label="Next">Tail page</a></li>
					</ul>

Posted by dawieharmse on Sat, 02 Nov 2019 03:36:19 -0700