一、MyBatisPlus分頁原理
MyBatisPlus是MyBatis的擴展工具包,它提供了豐富的功能和特性,其中包括分頁功能。MyBatisPlus的分頁原理是基於攔截器實現的。當我們在Mapper介面的方法上使用分頁註解時,MyBatisPlus會根據註解中的參數生成分頁攔截器,該攔截器會在執行SQL前進行攔截,將SQL改寫成相應的分頁SQL,最後執行並返回分頁結果。
下面是一個使用MyBatisPlus分頁的示例:
// Mapper介面 public interface UserMapper extends BaseMapper { // 分頁查詢用戶列表 @Select("select * from user") IPage selectUserPage(Page page); } // 業務層使用 Page page = new Page(1, 10); IPage userPage = userMapper.selectUserPage(page); List userList = userPage.getRecords();
可以看到,使用MyBatisPlus分頁非常方便,只需要在Mapper方法上添加註解即可。
二、MyBatis分頁條件查詢
在使用MyBatis進行分頁查詢時,我們還可以根據條件進行篩選。這裡我們以根據用戶名模糊查詢並分頁為例。首先在Mapper介面方法上添加參數註解@Param,指定查詢條件的參數名稱和分頁對象參數名稱。
// Mapper介面 public interface UserMapper { // 根據用戶名模糊查詢用戶列表 List selectUserPage(@Param("username") String username, @Param("page") Page page); }
然後在SQL語句中使用LIMIT分頁語句,並在WHERE語句中加入條件。
select * from user where username like CONCAT('%', #{username}, '%') limit #{page.offset}, #{page.size}
最後,在業務層中調用Mapper介面方法即可。
Page page = new Page(1, 10); List userList = userMapper.selectUserPage("admin", page);
三、MyBatis Page分頁原理
MyBatis原生提供的分頁功能是PageHelper,其分頁原理是基於攔截器鏈實現的。當我們在Mapper介面方法上使用分頁註解時,PageHelper會生成相應的攔截器,該攔截器會在執行SQL前進行攔截,將SQL改寫成相應的分頁SQL,最後執行並返回分頁結果。
下面是一個使用PageHelper進行分頁查詢的示例:
// Mapper介面 public interface UserMapper { // 分頁查詢用戶列表 @Select("select * from user") List selectUserPage(); } // 業務層使用 PageHelper.startPage(1, 10); List userList = userMapper.selectUserPage();
四、MyBatis自動分頁實現原理
MyBatis可以通過配置自動進行分頁,其原理是基於反射和攔截器鏈實現的。當我們在Mapper介面方法上使用分頁註解時,MyBatis會生成相應的攔截器,該攔截器會在執行SQL前進行攔截,將SQL改寫成相應的分頁SQL,最後執行並返回分頁結果。
下面是一個使用MyBatis自動分頁的示例:
// Mapper介面 public interface UserMapper { // 分頁查詢用戶列表 @Select("select * from user") List selectUserPage(RowBounds rowBounds); } // 業務層使用 RowBounds rowBounds = new RowBounds(0, 10); List userList = userMapper.selectUserPage(rowBounds);
五、MyBatis分頁查詢
在使用MyBatis進行分頁查詢時,我們可以通過RowBounds、Limit、Page三種方式實現分頁。其中,RowBounds方式是MyBatis默認的分頁方式,Limit方式需要手動設置分頁參數,Page方式是MyBatisPlus提供的分頁方式。
1、RowBounds方式分頁示例:
// Mapper介面 public interface UserMapper { // 分頁查詢用戶列表 @Select("select * from user") List selectUserPage(RowBounds rowBounds); } // 業務層使用 RowBounds rowBounds = new RowBounds(0, 10); List userList = userMapper.selectUserPage(rowBounds);
2、Limit方式分頁示例:
// Mapper介面 public interface UserMapper { // 分頁查詢用戶列表 @Select("select * from user limit #{offset}, #{size}") List selectUserPage(@Param("offset") int offset, @Param("size") int size); } // 業務層使用 int offset = 0; int size = 10; List userList = userMapper.selectUserPage(offset, size);
3、Page方式分頁示例:
// Mapper介面 public interface UserMapper extends BaseMapper { } // 業務層使用 Page page = new Page(1, 10); IPage userPage = userMapper.selectPage(page, null); List userList = userPage.getRecords();
六、MyBatisPlus分頁查詢原理
MyBatisPlus提供了豐富的分頁功能,其分頁原理是基於攔截器實現的。當我們在Mapper介面的方法上使用分頁註解時,MyBatisPlus會根據註解中的參數生成分頁攔截器,該攔截器會在執行SQL前進行攔截,將SQL改寫成相應的分頁SQL,最後執行並返回分頁結果。
下面是一個使用MyBatisPlus分頁查詢的示例:
// Mapper介面 public interface UserMapper extends BaseMapper { } // 業務層使用 Page page = new Page(1, 10); IPage userPage = userMapper.selectPage(page, null); List userList = userPage.getRecords();
七、MyBatis分頁方式原理
MyBatis提供了三種分頁方式,分別是RowBounds、Limit、Page。其中,RowBounds方式是MyBatis默認的分頁方式,Limit方式需要手動設置分頁參數,Page方式是MyBatisPlus提供的分頁方式。不同分頁方式的實現原理也不同。
1、RowBounds方式分頁原理:
RowBounds方式分頁是MyBatis最基本的分頁方式。在執行分頁查詢時,MyBatis會將RowBounds對象傳遞給Executor對象,Executor對象在查詢時會將RowBounds對象作為參數傳遞給StatementHandler對象,最終將分頁參數設置到PreparedStatement對象中。
2、Limit方式分頁原理:
Limit方式分頁是通過手動設置分頁參數實現的。在查詢時,我們需要手動設置offset和limit兩個參數,MyBatis會將這兩個參數通過參數映射傳遞給Executor對象,Executor對象在查詢時會將offset和limit參數作為參數傳遞給StatementHandler對象,最終將分頁參數設置到PreparedStatement對象中。
3、Page方式分頁原理:
Page方式分頁是MyBatisPlus提供的分頁方式。當我們在Mapper介面的方法上使用分頁註解時,MyBatisPlus會根據註解中的參數生成分頁攔截器,在執行分頁查詢時會自動將分頁參數設置到PreparedStatement對象中。
原創文章,作者:WNDDY,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/329415.html