PageHelper分页插件

x33g5p2x  于2021-10-07 转载在 其他  
字(1.3k)|赞(0)|评价(0)|浏览(239)

一、Pagehelper

1. 导入pom依赖
		<!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper -->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>5.1.10</version>
        </dependency>
	2. 在mybatis配置文件中设置plugins
		<plugins>
	        <plugin interceptor="com.github.pagehelper.PageInterceptor">
	<!--            reasonable:true  表示开启 合理判断 当页数小于0会从第一页开始查询,大于页码会默认查询最大页码数-->
	            <property name="reasonable" value="true"/>
	        </plugin>
	    </plugins>
	3. 测试
		@Test
	    public void demo6(){
	    	//设置完查询条件后,让mybatis执行sql语句时,会被拦截,然后把limit查询条件拼接在sql查询后面
	    	//PageHelper.startPage(1,5);必须紧邻sql查询语句,并且只对第一条查询语句生效
	        PageHelper.startPage(1,5);
	        List<Team> teams = mapper.queryPage();
	        teams.forEach(team -> System.out.println(team));
	    }

二、PageInfo 可以获取数据的各种详细信息

PageInfo对象中查看详细内容

1. 测试
		@Test
	    public void demo6(){
	        PageHelper.startPage(1,5);
	        List<Team> teams = mapper.queryPage();
	        teams.forEach(team -> System.out.println(team));
	        //传入上面查询的list集合数据 
	        PageInfo pageInfo = new PageInfo(teams);
	        System.out.println("当前页"+pageInfo.getPageNum());
	        System.out.println("总页数"+pageInfo.getPages());
	        System.out.println("前一页"+pageInfo.getPrePage());
	        System.out.println("后一页"+pageInfo.getNextPage());
	    }

相关文章

微信公众号

最新文章

更多