SpringBoot分组查询

引入MybatisPlusConfig 插件

新建一个名字为config的文件夹,在该文件夹下新建MybatisPlusConfig.java

1.作用

@Configuration注解的作用:声明一个类为配置类,用于取代bean.xml配置文件注册bean对象。

2.基础运用

@Configuration注解最常见的搭配使用有两个:@Bean@Scope

@Bean标注在方法上给容器注册组件,默认是单实例的

导入插件代码:

1
2
3
4
5
6
7
8
9
@Configuration
public class MybatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.H2));
return interceptor;
}
}

编写service文件夹下的接口

1
public IPage<Student> getPage(int currentPage, int pageSize, Student student);

编写Impl

注释部分为分组条件查询

1
2
3
4
5
6
7
8
9
10
11
12
@Override
public IPage<Student> getPage(int currentPage, int pageSize, Student student) {
IPage<Student> page = new Page<>(currentPage, pageSize);
/*LambdaQueryWrapper<Student> lqw = new LambdaQueryWrapper();
lqw.like(Strings.isNotEmpty(student.getUsername()), Student::getUsername, student.getUsername());
lqw.like(Strings.isNotEmpty(student.getPassword()), Student::getPassword, student.getPassword());
lqw.like(Strings.isNotEmpty(student.getEmail()),Student::getEmail,student.getEmail());
*/

studentMapper2.selectPage(page, lqw);
return page;
}

Controller类

编写个人创建的Controller

1
2
3
4
5
@GetMapping("/{currentPage}/{pageSize}")
public IPage<Student> getPage(@PathVariable int currentPage,@PathVariable int pageSize, Student student){

return studentService.getPage(currentPage, pageSize, student);
}