pageHelper依赖代码
时间: 2024-12-18 21:23:20 浏览: 83
PageHelper是一个常用的MyBatis分页插件,它简化了在MyBatis中进行分页查询的操作。以下是使用PageHelper的基本依赖和配置示例:
首先,在项目的pom.xml文件中添加PageHelper的依赖(如果你使用的是Maven):
```xml
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.x.x</version> <!-- 根据最新版本替换 -->
</dependency>
```
其中,你需要替换`5.x.x`为你实际使用的PageHelper版本。
然后,如果在Spring Boot项目中,可以在application.properties或application.yml文件中配置PageHelper的相关参数:
```properties
mybatis-pagehelper.helper-dialect=mysql
# 或者其他数据库,如mysql、oracle等
mybatis-pagehelperreasonable=true
# 是否启用合理化分页
# 其他可选配置项,比如:
# pagehelper.pageSize=10 设置每页默认大小
# pagehelper.params={} 可以在这里设置额外的查询参数,例如:{'sort': 'id desc'}
```
在你的服务层或Mapper接口上添加注解,表示这是一个分页查询:
```java
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.Page;
import com.example.demo.entity.YourEntity;
public interface YourMapper extends BaseMapper<YourEntity> {
Page<YourEntity> selectByParams(Object params); // 参数可以是自定义的实体类或者PageHelper提供的Page对象
}
```
在Service层,你可以这样调用分页查询:
```java
@Autowired
private YourMapper yourMapper;
List<YourEntity> entities = yourMapper.selectByParams(new Page<>(current_page, per_page)); // current_page 和 per_page 分别是当前页数和每页数量
```
以上就是基本的PageHelper使用步骤。
阅读全文
相关推荐


















