分页查询与SpringBoot3.x的pageHelper配置问题
在Spring Boot 3.x中,使用MyBatis进行分页查询时,可以使用PageHelper
插件。以下是配置和使用PageHelper
的步骤:
- 在
pom.xml
中添加PageHelper
依赖:
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>最新版本</version>
</dependency>
- 在
application.properties
或application.yml
中配置PageHelper
参数(如果有特殊需求):
# application.properties
pagehelper.helperDialect=mysql
pagehelper.reasonable=true
pagehelper.supportMethodsArguments=true
pagehelper.params=count=countSql
- 在MyBatis的Mapper接口中使用
PageHelper
进行分页:
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
@Repository
public interface YourMapper {
@Select("SELECT * FROM your_table")
PageInfo<YourEntity> selectByPage(int pageNum, int pageSize);
}
- 在Service层调用Mapper方法进行分页查询:
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class YourService {
@Autowired
private YourMapper yourMapper;
public PageInfo<YourEntity> queryByPage(int pageNum, int pageSize) {
PageHelper.startPage(pageNum, pageSize);
return yourMapper.selectByPage(pageNum, pageSize);
}
}
- 在Controller中调用Service层的分页方法:
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;
@RestController
public class YourController {
@Autowired
private YourService yourService;
@GetMapping("/queryByPage")
public PageInfo<YourEntity> queryByPage(@RequestParam int pageNum, @RequestParam int pageSize) {
评论已关闭