我出现的问题,
- 使用方式一这种写法能得到全部数据,不能实现分页,注意第一种写法PageHelper.startPage(pageNum, pageSize);需要紧跟查询语句
- 使用方式二的写法得不到数据
@Service
public class UserServiceImpl {
@Resource
private UserMapper userMapper;
@Transactional(propagation = Propagation.REQUIRED,readOnly = true)
public PageInfo<User> getUsers (Integer pageNum,Integer pageSize,User user){
//方式一
PageHelper.startPage(pageNum, pageSize);
List<User> list = userMapper.getUsers(user);
PageInfo<User> pageInfo = new PageInfo<>(list);
//方式二
/*PageInfo<User> pageInfo = PageHelper.startPage(pageNum, pageSize).doSelectPageInfo(() -> {
userMapper.getUsers(user);
});*/
return pageInfo;
}
}
原因:在使用springboot时,没有进行配置,只引入了依赖就开始使用,配置类代码如下:
package com.zhao.config;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.github.pagehelper.PageInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MybatisPlusConfig {
/**
*
* pagehelper的分页插件
*/
@Bean
public PageInterceptor pageInterceptor() {
return new PageInterceptor();
}
}
总结,出现问题的检查一定不能忽略配置
总结不同场景下pagehelper的用法:
1.在ssm项目中只需要引入依赖就可以
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.10</version>
</dependency>
2.在使用通用mybatis时只需要引入依赖
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.3</version>
</dependency>
3.使用增强mybatis时需要引入依赖,并进行配置
- 使用的依赖
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.11</version>
<scope>compile</scope>
</dependency>
- 配置类
package com.zhao.config;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.github.pagehelper.PageInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MybatisPlusConfig {
/**
*
* pagehelper的分页插件
*/
@Bean
public PageInterceptor pageInterceptor() {
return new PageInterceptor();
}
}