SpringBoot2.0整合MyBatis的分頁外掛PageHelper
PageHelper是MyBatis的一款分頁外掛,支援常見的 12 種資料庫,如Oracle,MySql,MariaDB,SQLite,DB2,PostgreSQL,SqlServer 等,專案地址:https://github.com/pagehelper/Mybatis-PageHelper
一、配置好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());
}
}
}
分頁成功,顯示前三條資料: