• mybatis-plus分页


    1.导入依赖

    
          <dependency>
             <groupId>com.baomidougroupId>
             <artifactId>mybatis-plus-boot-starterartifactId>
             <version>3.1.1version>
          dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    2.分页插件配置

     /**
         * mybatis-plus分页插件
         */
        @Bean
        public PaginationInterceptor paginationInterceptor() {
            return new PaginationInterceptor();
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    3.service层引用插件

    @Service
    public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel> implements AdChannelService {
    
        /**
         * 分页查询频道
         *
         * @param dto
         * @return
         */
        @Override
        public ResponseResult findByNameAndPage(ChannelDto dto) {
            //检验参数
            if (dto == null) {
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
            }
    
            //默认分页
            dto.checkParam();
    
            //Page用于定义每页的规格
            //IPage以规格和其他内容为参数,将记录进行分页
            Page page = new Page(dto.getPage(), dto.getSize());
            LambdaQueryWrapper<AdChannel> wrapper = Wrappers.lambdaQuery();
            String adChannelName = dto.getName();
            if(StringUtils.isNotBlank(adChannelName)){
                wrapper.like(AdChannel::getName,adChannelName);
            }
            IPage result = page(page, wrapper);
    
            //返回分页对象
            PageResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) result.getTotal());
            responseResult.setData(result.getRecords());
            return responseResult;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • Mybatis-plus的分页插件----IPage:
      <1>IPage 需要传入IPage的实现类Page对象,该对象实现了IPage,IPage内部原理是基于拦截器,拦截的是方法以及方法中的参数,会判断是否是查询操作。如果是查询操作,才会进入分页的逻辑处理。
      <2>进入分页的逻辑处理后,拦截器会通过反射获取该方法的参数进行判断是否存在IPage对象的实现类。如果不存在就不进行分页,存在则将该参数赋值给IPage对象,然后进行拼接sql处理完成分页操作。
      <3>limit (curPage-1)*pageSize,pageSize;curPage表示从第几条数据开始查(默认索引是0,如果写1,从第pageSize条开始查),pageSize表示这页显示几条数据

    4.sql打印yml配置

    mybatis-plus:
      mapper-locations: classpath*:mapper/*.xml
      # 设置别名包扫描路径,通过该属性可以给包中的类注册别名
      type-aliases-package: com.heima.model.user.pojos
      configuration:
        log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
  • 相关阅读:
    结构型模式-装饰模式
    在Windows上使用nginx具体步骤
    虚拟现实(VR)开发框架
    HTTP响应详解
    下一代 无线局域网--强健性
    python使用websocket服务传输数据的例子,可以保持长连接
    GitLab CI/CD系列教程(一)
    第四章 玩转捕获数据包
    html中a标签的属性
    容器技术-Docker的优点
  • 原文地址:https://blog.csdn.net/m0_47649585/article/details/126614283