SSM 中 PageHelper(配置使用)分页查询辅助工具

随着不断地学习,了解到的官方提供的辅助工具越来越多,在我们拥有这些工具之后,大大的简化了我们的开发,下面小编来介绍一下PageHelper的配置使用吧

1.官方教程是在mybatis-config.xml文件中配置标签

<?xmlversion="1.0" encoding="UTF-8" ?>
<!DOCTYPEconfiguration
        PUBLIC"-//mybatis.org//DTD Config 3.0//EN"
       "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <!-- 配置分页插件 -->
    <plugins>
        <plugin interceptor="com.github.pagehelper.PageHelper">
            <!-- 设置数据库类型Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库-->      
            <property name="dialect" value="mysql"/>
        </plugin>
    </plugins>
</configuration>

但是,我们在使用SSM整合的时候并不会去创建mybatis-config.xml配置文件,所以有很多小伙伴会有疑问,为什么配置了PagerHelper拦截器不生效等等问题,因为我们根本没有去加载mybatis-config配置文件,所以我们应该把这段配置写在SqlSessionFactoryBean中

 <!-- 配置SqlSessionFactoryBean -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="datasource"/>
        <!-- PageHelper配置 -->
        <property name="plugins">
            <array>
                <bean class="com.github.pagehelper.PageHelper">
                    <!--  如果不配置的话可以不写 -->
                    <property name="properties">
                        <!--使用下面的方式配置参数,一行配置一个 -->
                        <value>
                            params=value1
                        </value>
                    </property>
                </bean>
            </array>
        </property>
    </bean>

2.maven坐标地址

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

    <dependency>
      <groupId>com.github.jsqlparser</groupId>
      <artifactId>jsqlparser</artifactId>
      <version>0.9.5</version>
    </dependency>

3.在controller层编写一个测试的方法:

 @RequestMapping("/testSpring")
    public String testPageHelper(@RequestParam(value="pn",defaultValue = "1") Integer pn , Model model){
        System.out.println("成功");
        //分页查询
        PageHelper.startPage(pn,10);
        //dao层查询所有的方法
        List<Account> alist = accountService.findAll();
        //一次展示几页
        PageInfo<Account> info = new PageInfo<>(alist,6);
       System.out.println("总页码:" + info.getPages());
        System.out.println("第几页:"+info.getPageNum());
        System.out.println("总记录数:"+ info.getTotal());
        System.out.println("当前页有几条记录:"+ info.getSize());
        System.out.println("当前页的pageSize"+info.getPageSize());
        System.out.println("前一页:"+info.getPrePage());
        System.out.println("后一页:"+info.getNextPage());
        System.out.println("获取所有list:"+info.getList());
        System.out.println("获取分页大小:"+ Arrays.toString(info.getNavigatepageNums()));
        model.addAttribute("alist",info);
		//跳转到list.jsp界面
        return "list";
    }
输出结果:
总页码:203
第几页:1
总记录数:2029
当前页有几条记录:10
当前页的pageSize10
前一页:0
后一页:2
获取所有list:Page{count=true, pageNum=1, pageSize=10, startRow=0, endRow=10, total=2029, pages=203, countSignal=false, orderBy='null', orderByOnly=false, reasonable=false, pageSizeZero=false}
获取分页大小:[1, 2, 3, 4, 5, 6]

4. 编写list.jsp界面

<table cellpadding="5" border="1">
<c:forEach items="${alist.list}" var="account">

    <tr bgcolor="#fafad2">
        <td>姓名: ${account.name}</td>
        <td>金额: ${account.money}</td>
    </tr>
</c:forEach>

    <tr>
        <a href="testSpring?pn=1">首页</a>
       <a href="testSpring?pn=${alist.prePage}">上一页</a>

        <c:forEach items="${alist.navigatepageNums}" var="num">
            <c:if test="${num == alist.pageNum}">
            【${alist.pageNum}
        </c:if>
            <c:if test="${num != alist.pageNum}">
                <a href="testSpring?pn=${num}"> ${num} </a>
            </c:if>
        </c:forEach>

       <a href="testSpring?pn=${alist.nextPage}">下一页</a>
        <a href="testSpring?pn=${alist.pages}">末页</a>
    </tr>

</table>

5.最终效果展示:

首页
在这里插入图片描述
末页

在这里插入图片描述

下一页

在这里插入图片描述
虽然界面难看了一点点…但是这个工具用起来根本不要我们写什么代码,大大提高开发效率了!!!

猜你喜欢

转载自blog.csdn.net/weixin_43409994/article/details/105772447
今日推荐