pageHelper分页插件的使用

1、在pom.xml文件加入依赖

  <dependency>
      <groupId>com.github.pagehelper</groupId>
      <artifactId>pagehelper</artifactId>
      <version>5.1.3</version>
    </dependency>

2、在mybatis.xml内配置分页插件

 <!--分页插件-->
    <plugins>
        <plugin interceptor="com.github.pagehelper.PageInterceptor">
        </plugin>
    </plugins>

applicationContext.xml中也必须读取mybatis文件,否则分页总是显示一页

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="typeAliasesPackage" value="com.ssm.domain"/>
    <!--mapper.xml文件的位置(适用于接口与Mapper文件不在同一目录下)-->
    <property name="mapperLocations" value="classpath:/mapper/*.xml"/>
  <property name="configLocation" value="classpath:conf/mybatis.xml"/>

3、在Controller中使用分页插件

  @RequestMapping("/emps")
    public String getEmps(@RequestParam(value = "pn",defaultValue = "1")Integer pn, Model model){
        //在查询之前调用,传入页码,以及每页的大小
        Integer pageSize=5;
        PageHelper.startPage(pn,pageSize);
        List<Employee> emps=employeeService.getAll();
        PageInfo page = new PageInfo(emps,5);
        model.addAttribute("pageInfo",page);
        return "list";
    }

4、测试分页插件是否能使用----关于MockMvc的使用

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations = {"classpath:conf/applicationContext.xml","file:F:\\IdeaProjects\\ssm\\src\\main\\resources\\conf\\applicationContext.xml"})
public class MvcTest {
    @Autowired
    WebApplicationContext context;

    MockMvc mockMvc;
    @Before
    public void initMockMvc(){
        mockMvc  = MockMvcBuilders.webAppContextSetup(context).build();
    }
    @Test
    public void testPage() throws Exception{
        //模拟请求拿到返回值
      MvcResult result=mockMvc.perform(MockMvcRequestBuilders.get("/emps").param("pn","1")).andReturn();
        //请求成功以后,从请求域中取出pageInfo验证
        MockHttpServletRequest request = result.getRequest();
        PageInfo page = (PageInfo) request.getAttribute("pageInfo");
        System.out.println("当前页码:"+page.getPageNum());
        System.out.println("总页码:"+page.getPages());
        System.out.println("总记录数:"+page.getTotal());
        System.out.println("连续显示页码:");
        int[] nums = page.getNavigatepageNums();
        for(int i:nums){
            System.out.println(" "+i);
        }
//获取员工数据
        List<Employee> list = page.getList();
        for(Employee employee:list){
            System.out.println(" "+employee);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值