PageHelper是MyBatis的一款分页插件,支持常见的 12 种数据库,如Oracle,MySql,MariaDB,SQLite,DB2,PostgreSQL,SqlServer 等,项目地址:https://github.com/pagehelper/Mybatis-PageHelper
更多关于SpringBoot的总结请点击:SpringBoot使用总结
一、配置好MyBatis
二、build.gradle导入pagehelper的依赖
compile group: 'com.github.pagehelper', name: 'pagehelper-spring-boot-starter', version: '1.2.7'
三、使用方法
PageHelper的使用十分简单,只有一行代码:
PageHelper.startPage(0,3);//limit 0,3
四、单元测试
一个单元测试demo,注意必须在使用mapper接口进行查询前,先调用分页插件进行分页:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = CoreApplication.class)
@WebAppConfiguration
public class TestPageHelper {
@Autowired
private ContentVoMapper contentVoMapper;
@Test
public void testPageHelper(){
PageHelper.startPage(0,3);//limit 0,3
ContentVoExample ex = new ContentVoExample();
List<ContentVo> contentVos = contentVoMapper.selectByExample(ex);
for (ContentVo contentVo : contentVos) {
System.out.println(contentVo.getTitle());
}
}
}
分页成功,显示前三条数据: