1. 程式人生 > >在ssm框架中使用pagehelper分頁外掛

在ssm框架中使用pagehelper分頁外掛

期末大作業的前端也要自己寫,不會用分頁,於是百度了一下,發現百度知道里也是有好東西的,在裡面看到了pagehelper分頁外掛的用法,記錄下來,以後可能用得到。

先在pom.xml中匯入包

 <!-- pageHelper -->
    <dependency>
      <groupId>com.github.pagehelper</groupId>
      <artifactId>pagehelper</artifactId>
      <version>4.0.0</version>

在Spring-mybatis.xml檔案中加入相關配置

    <!-- MyBatis檔案配置,掃描所有.xml檔案 -->
    <bean id="sqlSessionFactory"
          class="org.mybatis.spring.SqlSessionFactoryBean"
          p:dataSource-ref="dataSource"
          p:configLocation="classpath:mybatis-config.xml"
          p:mapperLocations="classpath:com/nenu/newsManage/*.mapper.xml"
>
<!-- 配置分頁外掛 --> <property name="plugins"> <array> <bean class="com.github.pagehelper.PageHelper"> <!-- 這裡的幾個配置主要演示如何使用,如果不理解,一定要去掉下面的配置 --> <property name="properties"> <value
>
<!--使用的資料庫型別--> dialect=mysql reasonable=true supportMethodsArguments=true params=count=countSql autoRuntimeDialect=true </value> </property> </bean> </array> </property> </bean>

配置完成,開始使用

在ccontroller中配置

@RequestMapping(value = {"/", "index"}, method = RequestMethod.GET)
    public String toIndex(@RequestParam(required = false, defaultValue = "1") int pageNo,
                          @RequestParam(required = false, defaultValue = "10") int pageSize,
                          @RequestParam(required = false, defaultValue = "1") int columnId,
                          Model model) throws Exception {
        //開始分頁,分頁引數是頁碼和每頁記錄數
        PageHelper.startPage(pageNo, pageSize);
        List<Essay> essayList = essayService.listEssayByColumnId(pageNo,
                                                                 pageSize,
                                                                 columnId);
        //物件列表傳入頁面
        PageInfo<Essay> page = new PageInfo<Essay>(essayList);

        List<Columns> columnList = columnService.listColumn();
        Columns column = columnService.queryColumnById(columnId);
        model.addAttribute("essayPage", page);
        model.addAttribute("columnList", columnList);
        model.addAttribute("column", column);
        return "index";
    }

service:沒用listByPage方法

@Override
    public List<Essay> listEssayByColumnId(int pageNo,
                                           int pageSize,
                                           Integer columnId) throws Exception {
        List<Essay> essayList = essayMapper.listEssayByColumnId(pageNo, pageSize, columnId);
        return essayList;
    }

mapper裡也是正常查詢,沒有limit

<select id="listEssayByColumnId" resultMap="essayResultMap">
        SELECT *
        FROM t_essay
        LEFT JOIN t_column
        ON t_essay.column_id = t_column.column_id
        WHERE t_essay.column_id = #{columnId}
        AND t_essay.essay_state = 1
        ORDER BY t_essay.essay_id ASC
    </select>

index.jsp

<!-- 頁面列表部分 -->
<ul class="nei_1">
                <c:forEach items="${essayPage.list}" var="essay">
                    <li id="line_u7_0">
                        <a href="essayDetail.jsp?eid=${essay.essayId}"
                           target="_top" title="${essay.essayName}" indepth="true">${essay.essayName}</a>
                    </li>
                    <span id="section_u7_0" style="display: none;">
                            <hr style="height: 1px; border-width: 1px medium medium; border-style: dashed none none; border-color: rgb(204, 204, 204) currentcolor currentcolor; -moz-border-top-colors: none; -moz-border-right-colors: none; -moz-border-bottom-colors: none; -moz-border-left-colors: none; border-image: none;">
                            </span>
                </c:forEach>
            </ul>

<!-- 分頁頁碼部分 -->
<div id="pageNum">
        <c:if test="${essayPage != null && essayPage.getTotal() > 0 }">
            <nav style="text-align: center">
                <ul class="pagination pagination-lg">
                    <li><a>共 ${essayPage.total } 條記錄</a></li>
                    <li><a>當前第 ${essayPage.pageNum} 頁</a></li>
                    <c:if test="${essayPage.pageNum!= 1 }">
                        <li><a href="?columnId=${column.columnId}&pageNo=${essayPage.pageNum - 1}">上一頁</a></li>
                    </c:if>
                    <c:if test="${essayPage.pageNum < essayPage.pages }">
                        <li><a href="?columnId=${column.columnId}&pageNo=${essayPage.pageNum + 1}">下一頁</a></li>
                    </c:if>
                    <li><a>共 ${essayPage.pages} 頁</a></li>
                </ul>
            </nav>
        </c:if>
    </div>

之後就能正常使用了