关于PageHelper分页

  做过一个小项目是关于SpringBoot+SpringMvc+mybatis+pagehelper,这里简单实现了PageHelper。

  先上效果图:

        

(实现了功能我就懒到美化都不想美化了,可这并不是问题)

话不多说,直接上手吧。

1、Pom依赖

1 <dependency>
2             <groupId>com.github.pagehelper</groupId>
3             <artifactId>pagehelper</artifactId>
4             <version>5.1.2</version>
5 </dependency>

2、设置Mybatis配置xml中配置拦截器插件,一般在Resource路径下。我这里叫mybatis-config.xml。

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE configuration
 3   PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
 4   "http://mybatis.org/dtd/mybatis-3-config.dtd">
 5 <configuration>
 6     <typeAliases>
 7         <package name="com.ms.pojo"/>
 8     </typeAliases>
 9       <plugins>
10         <!--com.github.pagehelper为PageHelper类所在包名-->
11         <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
12     </plugins>
13 </configuration>

3、配置完mybatis之后,,就以分页查询用户列表为例,添加查询所有用户的mapper接口

public List<Student> SelectAllStu();

4、对应的sql语句

1     <select id="SelectAllStu" resultType="student">
2         select * from info
3     </select>

5、在Controller里面写业务代码

 1     //查询所有学生信息
 2     @RequestMapping("SelectAllStu")
 3     public ModelAndView SelectAllStu(@RequestParam(defaultValue="1") Integer page,
 4             HttpServletRequest request,HttpServletResponse response){
 5         PageHelper.startPage(page,5);//每页5个数据
 6         List<Student> list=studentService.SelectAllStu();
 7         PageInfo pageinfo = new PageInfo(list,5);
 8         
 9         ModelAndView model = new ModelAndView();
10         model.addObject("pageinfo", pageinfo);
11         model.setViewName("showAllStu");
12         return  model;
13         
14     }
  PageHelper.startPage(pageNum, pageSize);这句非常重要,这段代码表示分页的开始,意思是从第pageNum页开始,每页显示pageSize条记录。
  PageInfo这个类是插件里的类,这个类里面的属性会在输出结果中显示,
  使用PageInfo这个类,你需要将查询出来的list放进去
 

6、补充一下,PageHelper的配置我放在了Myconfig里面

 1 package com.ms.config;
 2 
 3 import java.util.Properties;
 4 
 5 import org.springframework.context.annotation.Bean;
 6 import org.springframework.context.annotation.Configuration;
 7 
 8 import com.github.pagehelper.PageHelper;
 9 
10 @Configuration
11 public class MyConfig {
12     @Bean
13     public PageHelper pageHelper(){
14         PageHelper pageHelper = new PageHelper();
15         Properties properties = new Properties();
16         /**默认false,设置为true时,会将RowBounds第一个参数offset当成pageNum页码使用*/
17         properties.setProperty("offsetAsPageNum","true");
18         /**默认false,设置为true时,使用RowBounds分页会进行count查询 */
19         properties.setProperty("rowBoundsWithCount","true");
20         /** 禁用合理化时,如果pageNum<1或pageNum>pages会返回空数据 */
21         properties.setProperty("reasonable","true");
22         /** always总是返回PageInfo类型,check检查返回类型是否为PageInfo,none返回Page */
23         properties.setProperty("returnPageInfo","check");
24         /** 支持通过Mapper接口参数来传递分页参数 */
25         properties.setProperty("supportMethodsArguments","false");
26         /**  配置数据库的方言  */
27         properties.setProperty("dialect","mysql");
28         pageHelper.setProperties(properties);
29         return pageHelper;
30     }
31 }

   

  个人感觉,pagehelper还是很不错的,当然还有其他分页的方法也可以勇于尝试哈。

 

原文地址:https://www.cnblogs.com/fzzzjjj/p/11268702.html