1. pom.xml
<!-- 分页 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
</dependency>
<!-- Mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
</dependency>
2.Spring/applicationContext-dao.xml配置
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 数据库连接池 -->
<property name="dataSource" ref="dataSource" />
<!-- 加载mybatis的全局配置文件 -->
<property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" />
</bean>
3.MyBatis配置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<plugins>
<!-- com.github.pagehelper 为 PageHelper 类所在包名 -->
<!-- 分页插件 -->
<plugin interceptor="com.github.pagehelper.PageHelper">
<!-- 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL 六种数据库-->
<property name="dialect" value="mysql"/>
</plugin>
</plugins>
</configuration>
4.PageResult实体类
/**
* 分页结果类
* @author ronybo
*
*/
public class PageResult implements Serializable{
// 总记录数
private long total;
// 当前记录数
private List 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;
}
public PageResult(long total, List rows) {
super();
this.total = total;
this.rows = rows;
}
}
服务层
@Service
public class BrandServiceImpl implements BrandService {
/**
* 数据访问层
*/
@Autowired
private TbBrandMapper brandMapper;
@Override
public PageResult findPage(int pageNum, int pageSize) {
/**
* MyBatis分页插件
* 在前面写这一句话,会自动实现分页
* 后面该咋写咋写
*/
PageHelper.startPage(pageNum, pageSize);
/**
* 查询所有,返回MyBatis的分页对象
*/
Page<TbBrand> pageBrands = (Page<TbBrand>) brandMapper.selectByExample(null);
/**
* 返回一个PageResult
* getTotal() : PageResult的总记录数
* getResult() : PageResult的当前记录数
*/
return new PageResult(pageBrands.getTotal(), pageBrands.getResult());
}
}
angular.js
<script type="text/javascript" src="../plugins/angularjs/angular.min.js"></script>
<!-- 分页组件开始 -->
<script type="text/javascript" src="../plugins/angularjs/pagination.js"></script>
<link rel="stylesheet" href="../plugins/angularjs/pagination.css"/>
<!-- 分页组件结束 -->
<script type="text/javascript">
var app = angular.module("pinyougou", ['pagination']);
app.controller("brandController", function($scope, $http){
// 分页控件配置
$scope.paginationConf = {
currentPage:1,// 当前页
totalItems:10,// 总记录数
itemsPerPage:10,// 每页显示的记录数
perPageOptions:[10, 20, 30, 40, 50],// 分页选项
onChange: function() {
$scope.reloadList();
}
};
// 刷新列表
$scope.reloadList = function() {
$scope.findPage($scope.paginationConf.currentPage, $scope.paginationConf.itemsPerPage);
}
$scope.findPage = function(page, size) {
$http.get("../brand/findPage.do?page="+page+"&size="+size).success(function(response) {
$scope.list = response.rows;// 显示当前页数据
$scope.paginationConf.totalItems = response.total;// 总记录数
});
}
});
</script>
....
<tbody>
<tr ng-repeat="entity in list">
<td><input type="checkbox" ></td>
<td>{{entity.id}}</td>
<td>{{entity.name}}</td>
<td>{{entity.firstChar}}</td>
<td class="text-center">
<button type="button" class="btn bg-olive btn-xs" data-toggle="modal" data-target="#editModal" >修改</button>
</td>
</tr>
</tbody>
<tm-pagination conf="paginationConf"></tm-pagination>
<!-- 分页控件/ -->