mybatis-plus分页操作,分页插件及page⽅法安装分页插件
定义⼀个分页插件的配置类
@Configuration
@MapperScan("dao或者Mapper的相对⽬录")
public class MybatisPlusConfig {
//旧版
@Bean
public PaginationInterceptor paginationInterceptor(){
return new PaginationInterceptor();
}
//新版3.5以后
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor(){
MybatisPlusInterceptor interceptor =new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.H2));
return interceptor;
}
}
测试分页⽅法
Dao层接⼝
@Mapper
public interface RoleDao extends BaseMapper<Role>{}
service接⼝
public interface RoleService extends IService<Role>{}
service接⼝实现类
分页查询插件
@Service
public class RoleServiceImpl extends ServiceImpl<RoleDao,Role>implements RoleService {}
测试⽅法
@Autowired
RoleService roleService;
@Test
void roleTest(){
Page<Role> rp =(Page<Role>) roleService.page(new Page<>(1,20),null);
System.out.Size());//当前页的记录数
System.out.println(rp.hasPrevious());//是否有上⼀页
System.out.println(rp.hasNext());//是否有下⼀页
System.out.Records());//当前页的全部信息
System.out.Total());//数据表中所有记录总数
System.out.Pages());//分了⼏页(总页数)
System.out.Current());//当前第⼏页
}