使用PageHelper就要引入其对应的jar包pagehelper.jar
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.11</version>
</dependency>
Spring-Mybatis.xml中的配置
<!--SQL会话工厂:mybatis-spring提供,方便与spring集成-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 配置数据源-->
<property name="dataSource" ref="dataSource"/>
<!-- 扫描mapper接口对应的映射文件-->
<property name="mapperLocations" value="classpath:mapper/*Mapper.xml"/>
<!-- 实体类取别名-->
<property name="typeAliasesPackage" value="com.kgc.ssm.pojo"/>
<!-- 配置插件-->
<property name="plugins">
<!-- pageHelper分页插件-->
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<props>
<!-- 设置分页插件使用的是MYSQL的语言言,即连接的数据库-->
<prop key="helperDialect">mysql</prop>
</props>
</property>
</bean>
</property>
</bean>
在spring-mybatis.xml文件中配置好pageHelper插件之后,就可以在项目中使用了
使用细节
Controller
@Controller
@RequestMapping("/user")
public class UserController {
@GetMapping
@ResponseBody
public ResponseEntity<PageInfo<User>> search(@RequestParam Map<String, Object> params) {
PageInfo<User> pageInfo = userService.search(params);
//
return ResponseEntity.ok(pageInfo);
}
}
使用的RESTFUL设计风格,GET请求进行查询,@GetMapping 表示拦截get请求,@ResponseBody 表名处理的是一个AJAX请求,PageInfo 是pageHelper提供的一个用于获取分页信息的类
返回时设置状态码为200,并且response的data属性中存放的时pageInfo这个对象
return ResponseEntity.ok(pageInfo);
相应源码
/**
* A shortcut for creating a {@code ResponseEntity} with the given body and
* the status set to {@linkplain HttpStatus#OK OK}.
* @return the created {@code ResponseEntity}
* @since 4.1
*/
public static <T> ResponseEntity<T> ok(T body) {
BodyBuilder builder = ok();
return builder.body(body);
}
Servcie
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public PageInfo<User> search(Map<String, Object> params) {
int currentPage = Integer.parseInt((String) params.get("currentPage"));
int displayCount = Integer.parseInt((String) params.get("displayCount"));
//告诉PageHelper当前查询的页码,和每页显示条数
PageHelper.startPage(currentPage, displayCount);
List<User> users = userMapper.getUsers(params);
//将查询的信息组装到PageInfo中
return new PageInfo<>(users);
}
}
PageHelper.startPage(currentPage, displayCount);
这一句是通过pagehelper实现分页的重点,它设置了分页显示的页码和每页显示数据的条目,有兴趣的小伙伴可以跟踪一下源码。
mapper
@Mapper
public interface UserMapper {
List<User> getUsers(@Param("params") Map<String, Object> params);
}
mapper对应的映射文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.kgc.ssm.mapper.UserMapper">
<select id="getUsers" resultType="user">
SELECT username,password,salt
FROM user
WHERE 1 =1
<if test="params.username !=null and params.username !='' ">
AND username = #{params.username}
</if>
</select>
</mapper>
这里的 if test=" " 只是为了后面添加查询条件放上去的。
可以看到在映射文件中我们并没有配置 LIMIT ?,?,这是因为使用了pagehelper,会通过spring的面向切面编程AOP的方式自动添加。
前端
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>用户管理</title>
</head>
<body ng-app="userApp" ng-controller="userCtrl">
<div>
<input type="button" value="查询" ng-click="search()">
</div>
<div ng-repeat="u in users" ng-bind="u"></div>
<div class="message" ng-show="pageInfo.pages != 0"
ng-bind="'当前显示第'+pageInfo.pageNum +'页,总'+pageInfo.pages+'页,共'+pageInfo.total+'条记录'"></div>
<div ng-show="pageInfo.pages != 0">
<input type="button" value="首页" ng-click="gotoPage(1)">
<input type="button" value="上一页" ng-click="gotoPage('pre')">
<input type="button" value="下一页" ng-click="gotoPage('next')">
<input type="button" value="尾页" ng-click="gotoPage(pageInfo.pages)">
</div>
</body>
<script type="text/javascript" src="static/js/angular.js"></script>
<script type="text/javascript">
let app = angular.module("userApp", []);
app.controller("userCtrl", function ($scope, $http) {
$scope.users = [];
$scope.currentPage = 1;
$scope.displayCount = 1;
//使用pageInfo对象获取服务器返回的PageInfo对象中的一些属性
$scope.pageInfo = {
hasNextPage: false,
hasPreviousPage: false,
isFirstPage: false,
isLastPage: false,
total: 0,
pageNum: 0,
pages:0,
nextPage:0,
prePage:0
};
$scope.search = function (isSearchBtn) {
$http({
url: 'user',
method: 'get',
params: {
currentPage: $scope.currentPage,
displayCount: $scope.displayCount
}
}).then(resp => {
console.log(resp.data);
//服务器返回的PageInfo中list属性中存放的就是要显示的数据
$scope.users = resp.data.list;
//获取页码中要使用的属性
$scope.pageInfo = {
hasNextPage: resp.data.hasNextPage,
hasPreviousPage: resp.data.hasPreviousPage,
isFirstPage: resp.data.isFirstPage,
isLastPage: resp.data.isLastPage,
total: resp.data.total,
pages:resp.data.pages,
pageNum: resp.data.pageNum,
nextPage:resp.data.nextPage,
prePage:resp.data.prePage,
};
});
};
$scope.gotoPage = function (target) {
//主要用于判断点击首页与尾页时,是否不需要进行查询
if (target == $scope.currentPage) return;
//下一页
if (target == 'next') {
if ($scope.pageInfo.hasNextPage) $scope.currentPage = $scope.pageInfo.nextPage;
else return;
}else if (target == 'pre') {
//上一页
if ($scope.pageInfo.hasPreviousPage) $scope.currentPage = $scope.pageInfo.prePage;
else return;
} else {
//首页与尾页跳转
$scope.currentPage = target;
}
//查询
$scope.search();
};
});
</script>
</html>
这里查询的时候只是传入了当前要显示数据的页码
$scope.currentPage
每页显示数据的数量
$scope.diaplayCount
使用效果(没做表格显示,直接输出对象,原谅我偷懒)
第一次写博客,不足之处还望谅解。