在ssm框架中使用pagehelper分页插件

期末大作业的前端也要自己写,不会用分页,于是百度了一下,发现百度知道里也是有好东西的,在里面看到了pagehelper分页插件的用法,记录下来,以后可能用得到。

先在pom.xml中导入包

 
    <dependency>
      <groupId>com.github.pagehelpergroupId>
      <artifactId>pagehelperartifactId>
      <version>4.0.0version>

在Spring-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 essayList = essayService.listEssayByColumnId(pageNo,
                                                                 pageSize,
                                                                 columnId);
        //对象列表传入页面
        PageInfo page = new PageInfo(essayList);

        List 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 listEssayByColumnId(int pageNo,
                                           int pageSize,
                                           Integer columnId) throws Exception {
        List 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>

之后就能正常使用了

你可能感兴趣的:(SSM)