教你一步步搭建ssm框架,第五步之分页插件pagehelper - 2018
原创    java_world    发表于:2018-05-14 16:33:12
  阅读 :226   收藏   编辑

上一步:教你一步步搭建ssm框架,第四步自动生成mybaits实体类,配置文件

pom.xml 新增

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

classpath中的mybatis_cfg.xml,新增如下,如settings同级别

<plugins>
        <!-- com.github.pagehelper为PageHelper类所在包名 -->
        <plugin interceptor="com.github.pagehelper.PageHelper">
            <!-- 该参数默认为false -->
            <!-- 设置为true时,会将RowBounds第一个参数offset当成pageNum页码使用 -->
            <!-- 和startPage中的pageNum效果一样-->
            <property name="offsetAsPageNum" value="true"/>
            <!-- 该参数默认为false -->
            <!-- 设置为true时,使用RowBounds分页会进行count查询 -->
            <property name="rowBoundsWithCount" value="true"/>
            <!-- 设置为true时,如果pageSize=0或者RowBounds.limit = 0就会查询出全部的结果 -->
            <!-- (相当于没有执行分页查询,但是返回结果仍然是Page类型)-->
            <property name="pageSizeZero" value="true"/>
            <!-- 支持通过Mapper接口参数来传递分页参数 -->
            <property name="supportMethodsArguments" value="false"/>
            <!-- always总是返回PageInfo类型,check检查返回类型是否为PageInfo,none返回Page -->
            <property name="returnPageInfo" value="none"/>
        </plugin>
</plugins>

使用:

    /**
     * 
     * @param pageNum
     * @param pageSize
     * @param searchVo
     * @return
     */
    @RequestMapping("")
    public String Index(TestVo searchVo,HttpServletRequest request) {
        //分页bean
        PageVo pageList = new PageVo();
        int pageNum  = 1;
        //默认分页大小
        int pageSize  = 5;
        try {

            try {
                pageNum = Integer.valueOf(searchVo.getPageNum());
                //pageNum 应 > 1
                if(pageNum < 1) {
                    pageNum = 1;
                }
            } catch (Exception e) {
                //
            }


            try {
                pageSize = Integer.valueOf(searchVo.getPageSize());
                //pageNum 应 > 1
                if(pageSize < 1) {
                    pageSize = 5;
                }
            } catch (Exception e) {
                //
            }


            PageHelper.startPage(pageNum, pageSize);
            List<TestVo> list = testService.getTestPageData(searchVo);
            PageInfo<TestVo> pageInfo = new PageInfo<TestVo>(list);


            //设置list,和total
            pageList.setList(pageInfo.getList());
            pageList.setTotal(pageInfo.getTotal());
            pageList.setPageNo(pageNum);
            pageList.setTotalPage(pageInfo.getPages());

            request.setAttribute("vo", pageList);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return "index";
    }

打印结果:

[2018-05-14 16:18:14.335]-[com.faceghost.app.dao.TestMapper.getTestPageData_COUNT]-[http-bio-8080-exec-1]  - ==>  Preparing: SELECT count(0) FROM test t WHERE 1 = 1 
[2018-05-14 16:18:14.361]-[com.faceghost.app.dao.TestMapper.getTestPageData_COUNT]-[http-bio-8080-exec-1]  - ==> Parameters: 
[2018-05-14 16:18:14.805]-[com.faceghost.app.dao.TestMapper.getTestPageData_COUNT]-[http-bio-8080-exec-1]  - <==      Total: 1
[2018-05-14 16:18:14.806]-[com.faceghost.app.dao.TestMapper.getTestPageData]-[http-bio-8080-exec-1]  - ==>  Preparing: select t.id, t.user_name userName, t.addr , t.sex from test t where 1 = 1 order by t.id desc limit ?,? 
[2018-05-14 16:18:14.807]-[com.faceghost.app.dao.TestMapper.getTestPageData]-[http-bio-8080-exec-1]  - ==> Parameters: 0(Integer), 5(Integer)
[2018-05-14 16:18:14.809]-[com.faceghost.app.dao.TestMapper.getTestPageData]-[http-bio-8080-exec-1]  - <==      Total: 5
评论
条评论