要在Spring Boot中使用MyBatis的分頁功能,可以按照以下步驟進行操作:
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.2.1</version>
</dependency>
mybatis.mapper-locations=classpath:mapper/*.xml
pagehelper.helperDialect=mysql
pagehelper.reasonable=true
pagehelper.supportMethodsArguments=true
import com.github.pagehelper.Page;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface UserMapper {
List<User> selectAll(Page<User> page);
}
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public List<User> getAllUsers(int pageNum, int pageSize) {
Page<User> page = PageHelper.startPage(pageNum, pageSize);
return userMapper.selectAll(page);
}
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/users")
public List<User> getUsers(@RequestParam int pageNum, @RequestParam int pageSize) {
return userService.getAllUsers(pageNum, pageSize);
}
}
通過以上步驟,就可以在Spring Boot中使用MyBatis的分頁功能了。在Controller層中傳入pageNum和pageSize參數(shù),調(diào)用Service層的方法查詢相應頁的數(shù)據(jù),并返回給前端頁面展示。