mybatis-plus分页查询

依赖


<dependency>
    <groupId>com.baomidougroupId>
    <artifactId>mybatis-plus-boot-starterartifactId>
    <version>3.3.2version>
dependency>

使用

  • 添加分页配置

@EnableTransactionManagement
@Configuration
@MapperScan("com.hash.dao")
public class MybatisPlusConfig {

    @Bean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        // 设置请求的页面大于最大页后操作, true调回到首页,false 继续请求  默认false
        // paginationInterceptor.setOverflow(false);
        // 设置最大单页限制数量,默认 500 条,-1 不受限制
        // paginationInterceptor.setLimit(500);
        return paginationInterceptor;
    }
    
}
  • dao层

public interface UserDao extends BaseMapper<User> {

    IPage<User> selectPageText(Page<User> page);

}

  • service层imp实现类

@Service
@Transactional
public class UserImp extends ServiceImpl<UserDao, User> implements IUserService {

    /**
     * 分页查询测试
     *
     * @param page
     * @return
     */
    @Override
    public IPage<User> selectPageText(Page<User> page) {
        return baseMapper.selectPageText(page);
    }
}
  • xml




<mapper namespace="com.hash.dao.UserDao">
    <select id="selectPageText" resultType="com.hash.entity.User">
        select *
        from user
        order by created_at desc
    select>
mapper>
  • 测试

@Test
public void textPage() {
    // 参数一是当前页,参数二是每页个数
    Page<User> userPage = new Page<>(1, 2);
    IPage<User> iPage = userService.selectPageText(userPage);
    System.out.println("总页数:" + iPage.getPages());
    System.out.println("总记录数:" + iPage.getTotal());
    if (iPage.getRecords().size() != 0) {
        List<User> mpUserList1 = iPage.getRecords();
        mpUserList1.forEach(System.out::println);
    } else {
        System.out.println("数据已经加载完成");
    }
}

你可能感兴趣的:(springboot,spring,boot)