以前不会分页的时候上CSDN搜索
发现很多分页教程已经老了 比较生涩难懂
这里使用mybatis-plus分别使用
1 PageHelper
2 mybaits-plus 自带的分页操作
来演示如何具体查询分页数据
pageHelper包含mybatis的依赖,和mybatis-plus有冲突
如果发生了依赖冲突
把注释给打开
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.13</version>
<!-- <!– 去除mybatis依赖 –>-->
<!-- <exclusions>-->
<!-- <exclusion>-->
<!-- <groupId>org.mybatis</groupId>-->
<!-- <artifactId>mybatis</artifactId>-->
<!-- </exclusion>-->
<!-- </exclusions>-->
</dependency>
@RestController
@RequestMapping("/page")
public class PageTestController {
@Autowired
TestService testService;
/**
* 使用PageHelper进行分页操作 并对parms中的Key进行模糊查询
*/
@GetMapping("/1")
public PageInfo findPage(@RequestParam Integer pageNum, @RequestParam Integer pageSize,
@RequestParam Map<String, Object> params) {
PageInfo pageInfo = testService.pageHelper(pageNum,pageSize,params);
return pageInfo;
}
}
public interface TestService extends IService<TestEntity> {
PageUtils queryBaseTestPage(Map<String, Object> params, Integer userId);
PageInfo pageHelper(Integer pageNum, Integer pageSize, Map<String, Object> params);
}
@Service
public class TestServiceImpl extends ServiceImpl<TestDao, TestEntity> implements TestService {
@Autowired
CourseDao courseDao;
@Autowired
StudentCourseRelationDao relationDao;
@Autowired
TestDao testDao;
@Override
public PageInfo pageHelper(Integer pageNum, Integer pageSize,Map<String,Object> params) {
//首先创建一个queryWrapper
QueryWrapper<TestEntity> queryWrapper = new QueryWrapper<>();
//开启分页功能
PageHelper.startPage(pageNum,pageSize);
// 如果有传来的params中有Key这个数据 我们让其对Name进行模糊查询
String key = (String) params.get("key");
if (StringUtils.hasText(key)){
queryWrapper.like("name",key);
}
//查所有数据返回
PageInfo pageInfo = new PageInfo(testDao.selectList(queryWrapper));
return pageInfo;
}
}
http://localhost:8082/page/1?pageNum=1&pageSize=5&key=孙
<dependencies>
<!-- web mvc-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- mysql链接-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- lombok简化插件-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- test测试-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- myabtis-plus依赖-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3</version>
</dependency>
</dependencies>
@Configuration
public class MybatisPlusPageConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
}
/**
* 使用Mybatis-plus自带分页操作 并对parms中的Key进行模糊查询
*/
@GetMapping("/2")
public IPage findPageByMybatisPlus(@RequestParam Integer pageNum, @RequestParam Integer pageSize,
@RequestParam Map<String, Object> params) {
IPage page = testService.pageByMybatisPlus(pageNum,pageSize,params);
return page;
}
public interface TestService extends IService<TestEntity> {
PageUtils queryBaseTestPage(Map<String, Object> params, Integer userId);
PageInfo pageHelper(Integer pageNum, Integer pageSize, Map<String, Object> params);
IPage pageByMybatisPlus(Integer pageNum, Integer pageSize,Map<String, Object> params);
}
@Override
public IPage pageByMybatisPlus(Integer pageNum, Integer pageSize,Map<String, Object> params) {
QueryWrapper<TestEntity> queryWrapper = new QueryWrapper<>();
String key = (String) params.get("key");
//如果名字等于Key 或者 email like key再查询出结果
if (StringUtils.hasText(key)){
queryWrapper.eq("name",key).or().like("email",key);
}
IPage<TestEntity> page = this.page(new Page<TestEntity>(pageNum, pageSize), queryWrapper);
return page;
}
很简单吧