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 方法时,传入页码和每页数量即可获取相应的分页数据。