Mybatis使用分页插件PageHelper做分页查询

首先在maven中添加一下依赖:

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

然后在Mybatis的配置文件中配置一下插件:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <!-- 配置分页插件 -->
    <plugins>
        <plugin interceptor="com.github.pagehelper.PageHelper">
            <!-- 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库-->        
            <property name="dialect" value="mysql"/>
        </plugin>
    </plugins>

</configuration>

进行单元测试:

我使用spring4作为环境

在查询之前先调用PageHelper.startPage()方法

这里第一个参数是取第几页,第二个参数是每页多少条记录

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:spring/applicationContext*.xml"})
public class PagerHelperTest {
	
	@Autowired
	SolutionMapper solutionMapper;
	
	@Test
	public void pageTest() {
		PageHelper.startPage(30, 15);
		SolutionExample example = new SolutionExample();
		List<Solution> list = solutionMapper.selectByExample(example);
		PageInfo<Solution> pageInfo = new PageInfo<>(list);
		System.out.println("总记录数:" + pageInfo.getTotal());
		System.out.println("总记页数:" + pageInfo.getPages());
		System.out.println("返回的记录数:" + list.size());
		for (Solution solution : list) {
			System.out.println(solution.getSolutionId());
		}
	}
}

返回的结果:



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值