目录
一、导入pom
<!-- mybatis分页插件依赖 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.0.0</version>
</dependency>
二、配置PageHelper拦截器
<!-- 配置pageHelp的拦截器 -->
<bean id="plugin" class="com.github.pagehelper.PageHelper">
<property name="properties">
<value>helperDialect=mysql</value>
</property>
</bean>
三、在SqlSessionFactoryBean中注入属性
<!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 自动扫描mapping.xml文件 -->
<property name="mapperLocations" value="classpath:mapper/*.xml"></property>
<!-- 配置分页插件 -->
<property name="plugins">
<list>
<ref bean="plugin"></ref>
</list>
</property>
</bean>
四、controller层:
@RestController
@RequestMapping("student")
public class StudentController {
@Autowired
StudentService services;
@RequestMapping("queryByPage")
public ModelAndView queryByPage1(@RequestParam(defaultValue = "1") Integer page , @RequestParam(defaultValue="2") Integer pageSize){
PageHelper.startPage(page,pageSize);//开始分页
List<Student> list = services.getStudentAll();
PageInfo<Student> pageInfo = new PageInfo<Student>(list);//封装分页数据
ModelAndView mav = new ModelAndView();
mav.setViewName("pages/students");
mav.addObject("pageInfo", pageInfo);
return mav;
}
@RequestMapping("queryByPage2")
public PageInfo<Student> queryByPage2(@RequestParam(defaultValue = "1") Integer page , @RequestParam(defaultValue="10") Integer pageSize){
PageHelper.startPage(page,pageSize);//开始分页
List<Student> list = services.getStudentAll();
PageInfo<Student> pageInfo = new PageInfo<Student>(list);//封装分页数据
return pageInfo;
}
}
五、service层:
@Service
public class StudentServiceImpl implements StudentService {
@Autowired
private StudentMapper studentMapper;
@Override
public List<Student> getStudentAll() {
return studentMapper.selectByExample(new StudentExample());
}
/**
* 静态方法startPage
*/
@Override
public List<Student> getStudentListByPage(StudentExample StudentExample, Integer pageNum, Integer pageSize) {
// 在你需要进行分页的 MyBatis 查询方法前调用 PageHelper.startPage 静态方法即可,紧跟在这个方法后的第一个MyBatis 查询方法会被进行分页。
// 只要你可以保证在 PageHelper 方法调用后紧跟 MyBatis 查询方法,这就是安全的
PageHelper.startPage(pageNum, pageSize);
return studentMapper.selectByExample(StudentExample);
}
/**
* 分页时,实际返回的结果list类型是Page<E>,如果想取出分页信息,需要强制转换为Page<E>
* 因为 public class Page<E> extends ArrayList<E> implements Closeable
*/
@Override
public Page<Student> getStudentListByPage1(StudentExample StudentExample, Integer pageNum, Integer pageSize) {
PageHelper.startPage(pageNum, pageSize);
List<Student> list = studentMapper.selectByExample(StudentExample);
if (null != list) {
Page<Student> page = (Page<Student>) list;
System.out.println(page);
return page;
}
return null;
}
}
六、jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<p>当前${pageInfo.pageNum}页,共${pageInfo.pages}页,总共${pageInfo.total}条记录</p>
<a href="queryByPage?page=${pageInfo.firstPage}">第一页</a>
<c:if test="${pageInfo.hasPreviousPage}">
<a href="queryByPage?page=${pageInfo.pageNum-1}">上一页</a>
</c:if>
<c:if test="${pageInfo.hasNextPage}">
<a href="queryByPage?page=${pageInfo.pageNum+1}">下一页</a>
</c:if>
<a href="queryByPage?page=${pageInfo.lastPage}">最后一页</a>
</body>
</html>