一、创建配置类
注意paginationInterceptor方法一定要写上@Bean注解,否则无法执行分页查询
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
//分页插件类
@Configuration
public class MybatisPlusConfig {
@Bean
public PaginationInterceptor paginationInterceptor(){
return new PaginationInterceptor();
}
}
二、编写测试方法
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huang.dao.UserDao;
import com.huang.entity.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class SpringbootMybatisPlusApplicationTests {
@Autowired
private UserDao userDao;
//分页查询
@Test
void selectPage(){
//查询第一页,第一条数据
Page<User> page = new Page<>(1,1);
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.like("name","z");
IPage<User> iPage = userDao.selectPage(page, wrapper);
//数据的基本信息
System.out.println("数据总条数 = " + iPage.getTotal());
System.out.println("数据总页数 = " + iPage.getPages());
System.out.println("当前页数 = " + iPage.getCurrent());
//输出数据
iPage.getRecords().forEach(user -> System.out.println(user));
}
}