SSM整合之PageHelper分页插件的使用

       在之前的博客中,我给大家分享了如何使用原生态mysql+mybatis完成分页,今天给大家分享一款mybatis的分页插件PageHelper:
       大家都知道在编写Web后台代码或者进行JavaEE应用开发,分页都是必不可少的,当然最原始的思路是通过在Sql中使用分页关键字来进行分页。但是在实际开发中,更多的是使用分页插件来减少代码冗杂,使编码更加清晰,当然书写也更加方便;

思路:分页控制的代码写在拦截器中,从而不影响本来的业务逻辑代码

插件的环境引入:
1.pom文件中引入分页插件的资源位置:

<!-- 分页插件 -->
<dependency>
	<groupId>com.github.pagehelper</groupId>
	<artifactId>pagehelper</artifactId>
	<version>5.0.0</version>
</dependency>

2.在spring-bean.xml配置文件中引入插件:

 <!-- spring整合mybatis之后,数据源的配置交由spring进行管理 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!-- 自动扫描mapping.xml文件 -->
        <property name="mapperLocations" value="classpath:mybatis-config.xml"></property>
        <!-- 配置分页插件 -->
        <property name="plugins">
            <array>
                <bean class="com.github.pagehelper.PageHelper">
                    <property name="properties">
                        <value>
                            <!--数据库环境-->
                            dialect=mysql
                            reasonable=true
                        </value>
                    </property>
                </bean>
            </array>
        </property>
    </bean>

或者mybatis-config.xml中配置

   <!--pageHelper插件 -->
     <plugins>
         <plugin interceptor="com.github.pagehelper.PageInterceptor">
              <property name="helperDialect" value="mysql"/>
         </plugin>
     </plugins>

3.编写拦截器(规定以paging结尾的方法就是分页的方法,拦截controller层中以paging为结尾的方法)

@Aspect
@Component
public class PageInterceptor {
    @Around("execution(* com.yueqian.pagehelper.controller..*.*Paging(..))")
    public Object process(ProceedingJoinPoint point) throws Throwable {
        Object[] args = point.getArgs();
        if(args.length<2) {
            //规定原方法的参数最后两个是当前页和每页条数
            throw new Exception("参数不够分页");
        }
        PageHelper.startPage((Integer)args[args.length-2],(Integer)args[args.length-1]);
        List list = (List) point.proceed();
        PageInfo pageInfo = new PageInfo(list);
        return pageInfo;
    }
}

//注意:
//其实分页的核心代码就两句代码:
//1、PageHelper.startPage("当前页码","每页显示的条数","是否查询count[true|false]");
//2、PageInfo pageInfo = new PageInfo("实际的数据集合List<T>");

       到此你已经编写出“万能”的分页插件了,只要你想分页,只需满足:1.方法名字以paging结尾 .2.方法参数最后两个是当前页和每页条数。

为了看出效果,贴出controller层等相关代码 
controller层:

    /**
     * 分页获取数据
     */
    @RequestMapping("/page")
    @ResponseBody
    public Object getUsersPaging(@RequestParam("currentPage")Integer 
        currentPage,@RequestParam("size")Integer size) {

        List<User> list = userService.getUsers();
        return list;
    }

以下是gerUsers()这个方法调用的sql

 <select id="selectUsers" resultMap="BaseResultMap">
       select * from user
 </select>

可以看出service,dao层的代码没有受到分页的任何影响.

总结: 
1.pageHelper的安全性,当遇到pageHelper有效与否等问题时,查阅资料解决,由于这里只是简单使用,只是提一下 
2.pageInfo这个类,可以看到,拦截分页方法最后返回的是pageInfo这个类的对象,他是里面的内容可以打印到前台看一下: 

具体内容如下:

{"pageNum":1,"pageSize":3,"size":3,"orderBy":null,"startRow":1,"endRow":3,"total":6,"pages":2,"list":[{"id":1,"username":"jack","address":"kkkk"},{"id":2,"username":"rose","address":"jjjj"},{"id":3,"username":"wade","address":"3"}],"firstPage":1,"prePage":0,"nextPage":2,"lastPage":2,"isFirstPage":true,"isLastPage":false,"hasPreviousPage":false,"hasNextPage":true,"navigatePages":8,"navigatepageNums":[1,2]}
1
里面有很多对于前台来说有用的信息,所以直接返回他了
--------------------- 
作者:ALearrring 
来源:CSDN 
原文:https://blog.csdn.net/alearrring/article/details/79208113 
版权声明:本文为博主原创文章,转载请附上博文链接!

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值