MyBatis Plus 實(shí)現(xiàn)分頁(yè)功能非常簡(jiǎn)單,只需要使用 Page 類即可。下面是一個(gè)簡(jiǎn)單的示例:
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);
}
}
這樣就可以實(shí)現(xiàn) MyBatis Plus 的分頁(yè)功能。在調(diào)用 getUserPage 方法時(shí),傳入頁(yè)碼和每頁(yè)數(shù)量即可獲取相應(yīng)的分頁(yè)數(shù)據(jù)。