ssm之用pagehelper实现mybatis数据分页

ssm之用pagehelper实现mybatis数据分页

自己程序员一枚,还在做框架的不断整合,就ssm文件上传来说,网上已经有很多案例。在此仅作自己以后参考笔记,希望能帮到一部分处于还在整合框架的初级人员。

1.引入pagehelper jar包的maven依赖

<dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>5.1.2-beta</version>
 </dependency>

2.在mybatis的配置文件中添加如下配置

<!--分页工具配置-->
    <plugins>
        <!--&lt;!&ndash; com.github.pagehelper 为 PageHelper 类所在包名 &ndash;&gt;-->
        <plugin interceptor="com.github.pagehelper.PageInterceptor">
            <!--4.0以后版本,自动识别数据库 类型-->
            <!--&lt;!&ndash; 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL 六种数据库&ndash;&gt;--
            <!--<property name="dialect" value="mysql"/>-->
        </plugin>
    </plugins>

pegehelper4.0以后版本已经可以自动识别数据的类型了,不需要再指定数据库类型,添加了会报如下错误:com.github.pagehelper: com.github.pagehelper.PageException: java.lang.ClassNotFoundException: mysql
3.实现结果实体类PageResult

import java.io.Serializable;
import java.util.List;
/**
 * 分页结果类
 * @author jiangdoc
 */
public class PageResult implements Serializable{

    private Long total;	//总记录数
    private List rows;	//当前页数据

    public PageResult(long total, List rows) {
        this.total = total;
        this.rows = rows;
    }
    public Long getTotal() {
        return total;
    }
    public void setTotal(Long total) {
        this.total = total;
    }
    public List getRows() {
        return rows;
    }
    public void setRows(List rows) {
        this.rows = rows;
    }
}

注意这里实现了序列化接口,继承Serializable接口
4.在service层实现下列代码(xxxMapper.java和xxxMapper.xml逆向工程以实现)

   /**
     * 
     * @param pageNum 当前页数
     * @param row   每页多少行数据
     * @return
     */
   public PageResult getPage(int pageNum, int rows) {
        PageHelper.startPage(pageNum, rows);
        Page<User> page = (Page<User>) bookdataMapper.selectByExample(null);
        return new PageResult(page.getTotal(),page.getResult());
        }

4.在controller层只添加

@RequestMapping("/getPage")
@ResponseBody
    public PageResult getPage(int page,int rows){
        return userService.getPage(page,rows);
    }

5.剩下的就是前端的知识了,仅作自己记录用,大家自由发挥
由于自己用的angular.js的插件
只需要在js中添加

 //重新加载列表 数据
    $scope.reloadList = function () {
        //切换页码
        $scope.findPage($scope.paginationConf.currentPage, $scope.paginationConf.itemsPerPage);
    };
    //分页控件配置
    $scope.paginationConf = {
        currentPage: 1,         //当前页码
        totalItems: 10,         //总条数
        itemsPerPage: 2,        //默认每页几条数据
        perPageOptions: [5, 20, 30, 40, 50],//页码选项
        onChange: function () {     //更改页面时触发事件
            $scope.reloadList();//重新加载
        }
    };
//分页
    $scope.findPage = function (page, rows) {
        $http.get('../user/getPage?page=' + page + '&rows=' + rows).success(
            function (response) {
                $scope.list = response.rows;
                $scope.paginationConf.totalItems = response.total;//更新总记录数
            }
        );
    };

在html中引入angular.js的分页插件

 <!-- 分页组件开始 -->
    <script src="../plugins/angularjs/pagination.js"></script>
    <link rel="stylesheet" href="../plugins/angularjs/pagination.css">
    <!-- 分页组件结束 -->

在需要分页的位置添加即可实现分页控件

   <tm-pagination conf="paginationConf"></tm-pagination>

本文到此结束:如有不足之处,望多加指正;

猜你喜欢

转载自blog.csdn.net/jiang18238032891/article/details/88367340