MyBatis-Plus分頁插件提供了IPage接口來支持分頁查詢。下面是一個使用示例:
public interface UserMapper extends BaseMapper<User> {
IPage<User> selectUserPage(IPage<User> page, @Param("username") String username);
}
<select id="selectUserPage" parameterType="com.baomidou.mybatisplus.extension.plugins.pagination.Page">
SELECT *
FROM user
<where>
<if test="username != null and username != ''">
AND username like CONCAT('%', #{username}, '%')
</if>
</where>
</select>
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
@Override
public IPage<User> selectUserPage(int pageNum, int pageSize, String username) {
Page<User> page = new Page<>(pageNum, pageSize);
return baseMapper.selectUserPage(page, username);
}
}
@RestController
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/users")
public IPage<User> getUsers(@RequestParam(defaultValue = "1") int pageNum,
@RequestParam(defaultValue = "10") int pageSize,
@RequestParam(defaultValue = "") String username) {
return userService.selectUserPage(pageNum, pageSize, username);
}
}
這樣就可以通過訪問/users
接口來實現分頁查詢,傳入頁碼和每頁數量可以獲取對應的分頁數據。