MybatisPlus实现分页
生活随笔
收集整理的這篇文章主要介紹了
MybatisPlus实现分页
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
select
1、根據id查詢記錄
//刪除操作 物理刪除 @Test public void testDeleteById(){int result = userMapper.deleteById(1231125349744828417L);System.out.println(result); }2、通過多個id批量查詢
完成了動態sql的foreach的功能
//批量刪除 @Test public void testDeleteBatchIds() {int result = userMapper.deleteBatchIds(Arrays.asList(1,2));System.out.println(result); }3、簡單的條件查詢
通過map封裝查詢條件
@Test public void testSelectByMap(){HashMap<String, Object> map = new HashMap<>();map.put("name", "Jone");map.put("age", 18);List<User> users = userMapper.selectByMap(map);users.forEach(System.out::println); }注意:map中的key對應的是數據庫中的列名。例如數據庫user_id,實體類是userId,這時map的key需要填寫user_id
分頁
MyBatis Plus自帶分頁插件,只要簡單的配置即可實現分頁功能
(1)創建配置類
此時可以刪除主類中的?@MapperScan?掃描注解
/*** 分頁插件*/ @Bean public PaginationInterceptor paginationInterceptor() {return new PaginationInterceptor(); } (2)測試selectPage分頁 測試:最終通過page對象獲取相關數據 //分頁查詢 @Test public void testPage() {//1 創建page對象//傳入兩個參數:當前頁 和 每頁顯示記錄數Page<User> page = new Page<>(1,3);//調用mp分頁查詢的方法//調用mp分頁查詢過程中,底層封裝//把分頁所有數據封裝到page對象里面userMapper.selectPage(page,null);//通過page對象獲取分頁數據System.out.println(page.getCurrent());//當前頁System.out.println(page.getRecords());//每頁數據list集合System.out.println(page.getSize());//每頁顯示記錄數System.out.println(page.getTotal()); //總記錄數System.out.println(page.getPages()); //總頁數System.out.println(page.hasNext()); //下一頁System.out.println(page.hasPrevious()); //上一頁}控制臺sql語句打印:SELECT id,name,age,email,create_time,update_time FROM user LIMIT 0,5?
?
(3)測試selectMapsPage分頁:結果集是Map
@Test public void testSelectMapsPage() {Page<User> page = new Page<>(1, 5);IPage<Map<String, Object>> mapIPage = userMapper.selectMapsPage(page, null);//注意:此行必須使用 mapIPage 獲取記錄列表,否則會有數據類型轉換錯誤mapIPage.getRecords().forEach(System.out::println);System.out.println(page.getCurrent());System.out.println(page.getPages());System.out.println(page.getSize());System.out.println(page.getTotal());System.out.println(page.hasNext());System.out.println(page.hasPrevious()); }?
超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生總結
以上是生活随笔為你收集整理的MybatisPlus实现分页的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MybatisPlus实现乐观锁
- 下一篇: MybatisPlus实现逻辑删除