MyBatis Plus 實現分頁功能非常簡單,只需要使用 Page 類即可。下面是一個簡單的示例:
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
public interface UserMapper extends BaseMapper<User> {
Page<User> selectUserPage(Page<User> page);
}
<select id="selectUserPage" resultType="User">
select * from user
</select>
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public Page<User> getUserPage(int pageNum, int pageSize) {
Page<User> page = new Page<>(pageNum, pageSize);
return userMapper.selectUserPage(page);
}
}
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/users")
public Page<User> getUsers(int pageNum, int pageSize) {
return userService.getUserPage(pageNum, pageSize);
}
}
這樣就可以實現 MyBatis Plus 的分頁功能。在調用 getUserPage 方法時,傳入頁碼和每頁數量即可獲取相應的分頁數據。