bookDao.selectPage(null,null);
进入selectPage 里面
第一个null 需要加入 Ipage类型的
进入IPage
ctrl
+ h 查看继承关系
加入参数
第一页显示五条数据
分页如果要使用 必须要使用mp 提供的拦截器
创建一个拦截器的类
package com.itheima.config;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MPConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor(){
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); // 拦截器的壳 没有拦截器
interceptor.addInnerInterceptor(new PaginationInnerInterceptor());// 添加內部分页拦截器
return interceptor;
}
}
void testGetPage(){
IPage iPage = new Page(2,5);
bookDao.selectPage(iPage,null);
System.out.println(iPage.getCurrent()); // 当前页
System.out.println(iPage.getSize());// 一页显示多少数据
System.out.println(iPage.getTotal());// 一共多少数据
System.out.println(iPage.getPages());// 一共能分多少页
System.out.println(iPage.getRecords());// 当前页的数据
}
@Test
void testgetBy(){
QueryWrapper<Book> queryWrapper = new QueryWrapper(); // 条件的壳
queryWrapper.like("name","spring"); //查询 name 值中包含spring的数据
bookDao.selectList(queryWrapper);
}
该方法 如果name 属性值 写错 比如写成 anme 那么就会报错
可以改成LambdaQueryWrapper
@Test
void testgetBy2(){
LambdaQueryWrapper<Book> lqw = new LambdaQueryWrapper(); // 条件的壳
lqw.like(Book::getName,"spring");
bookDao.selectList(lqw);
}
如果查询结果是null 则 会将null 当成字符串查询
String name = null;
LambdaQueryWrapper<Book> lqw = new LambdaQueryWrapper(); // 条件的壳
lqw.like(Book::getName,name);
bookDao.selectList(lqw);
有一个condition 条件 如果是true 就连 如果是false 就不连后面的查询条件