Mybatis Plus分页查询

我们两清 提交于 2020-12-16 06:15:02

一、配置

/**
* @author beth
* @data 2019-10-14 20:01
*/
@Configuration
public class MybatisPlusConfig {
   @Bean
   public PaginationInterceptor paginationInterceptor(){
       return  new PaginationInterceptor();
   }
}

二、分页查询

/**
 * 分页查询
 */
@Test
public void selectPage() {
    QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<UserInfo>();
    queryWrapper.ge("age",26);
    Page<UserInfo> page = new Page<UserInfo>(1,2);
    IPage<UserInfo> iPage = userInfoMapper.selectPage(page,queryWrapper);
    System.out.println("总页数"+iPage.getPages());
    System.out.println("总记录数"+iPage.getTotal());
    List<UserInfo>  userInfoList = iPage.getRecords();
    userInfoList.forEach(System.out::println);
}

三、分页查询,返回map

/**
 * 分页查询,返回map
 */
@Test
public void selectMapPage() {
    QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<UserInfo>();
    queryWrapper.ge("age",26);
    Page<UserInfo> page = new Page<UserInfo>(1,2);
    //Page<UserInfo> page = new Page<UserInfo>(1,2,false);
    IPage<Map<String,Object>> iPage = userInfoMapper.selectMapsPage(page,queryWrapper);
    System.out.println("总页数"+iPage.getPages());
    System.out.println("总记录数"+iPage.getTotal());
    List<Map<String, Object>> userInfoList = iPage.getRecords();
    userInfoList.forEach(System.out::println);
}

四、自定义sql分页查询

1、

@Repository
public interface UserInfoMapper extends BaseMapper<UserInfo> {
  IPage<UserInfo> selectUserPage(Page<UserInfo> page, @Param(Constants.WRAPPER) Wrapper<UserInfo> wrapper);
}

2、

<select id="selectUserPage" resultMap="com.xiao.permission_system.entity.UserInfo">
  select * from user ${ew.customSqlSegment}
</select>

3、

/**
 * 自定义sql分页查询
 */
@Test
public void selectUserPage() {
    QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<UserInfo>();
    queryWrapper.ge("age",26);
    Page<UserInfo> page = new Page<UserInfo>(1,2);
    IPage<UserInfo> iPage = userInfoMapper.selectUserPage(page,queryWrapper);
    System.out.println("总页数"+iPage.getPages());
    System.out.println("总记录数"+iPage.getTotal());
    List<UserInfo>  userInfoList = iPage.getRecords();
    userInfoList.forEach(System.out::println);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!