①. 分页插件
@Configuration
public class MybatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
}
@Test
public void testPage(){
Page<User>page=new Page<>(1,2);
Page<User> userPage = userMapper.selectPage(page, null);
List<User> records = userPage.getRecords();
System.out.println("当前页:"+page.getCurrent());
System.out.println("每页显示的条数:"+page.getSize());
System.out.println("总记录数:"+page.getTotal());
System.out.println("总页数:"+page.getPages());
System.out.println("是否有上一页:"+page.hasPrevious());
System.out.println("是否有下一页:"+page.hasNext());
}
②. xml自定义分页
@Repository
public interface UserMapper extends BaseMapper<User> {
Page<User>selectPageVoByAge(@Param("page") Page<User>page, @Param("age") Integer age);
}
<mapper namespace="com.xiaozhi.mybatisplus.mapper.UserMapper">
<select id="selectPageVoByAge" resultType="com.xiaozhi.mybatisplus.entities.User">
select * from t_user where age>#{age}
</select>
</mapper>
@Test
public void testSelectPageVoByAge(){
Page<User>page=new Page<>(1,2);
Page<User> userPage = userMapper.selectPageVoByAge(page, 20);
List<User> records = userPage.getRecords();
System.out.println("当前页:"+page.getCurrent());
System.out.println("每页显示的条数:"+page.getSize());
System.out.println("总记录数:"+page.getTotal());
System.out.println("总页数:"+page.getPages());
System.out.println("是否有上一页:"+page.hasPrevious());
System.out.println("是否有下一页:"+page.hasNext());
}
|