MyBatis中的分頁查詢可以通過使用RowBounds對(duì)象或者使用Mapper接口中的@SelectProvider注解來實(shí)現(xiàn)。具體步驟如下:
int offset = 0; // 起始行
int limit = 10; // 每頁顯示條數(shù)
RowBounds rowBounds = new RowBounds(offset, limit);
List<User> userList = sqlSession.selectList("com.example.mapper.UserMapper.selectUsers", null, rowBounds);
@SelectProvider(type = UserProvider.class, method = "selectUsers")
List<User> selectUsers(@Param("offset") int offset, @Param("limit") int limit);
public class UserProvider {
public String selectUsers(Map<String, Object> parameter) {
int offset = (int) parameter.get("offset");
int limit = (int) parameter.get("limit");
return "SELECT * FROM user LIMIT " + offset + "," + limit;
}
}
int offset = 0; // 起始行
int limit = 10; // 每頁顯示條數(shù)
List<User> userList = userMapper.selectUsers(offset, limit);
以上就是MyBatis中實(shí)現(xiàn)分頁查詢的方法。通過設(shè)置RowBounds對(duì)象或者使用@SelectProvider注解,可以輕松地實(shí)現(xiàn)分頁功能。