• Mybatis 框架 ( 五 ) 分页


    4.6.分页

    Mybatis-plus 内置分页插件, 并支持多种数据库

    官网 : 分页插件 | MyBatis-Plus (baomidou.com)

    4.6.1.增加拦截器

    通过 @MapperScan 指定 mapper接口的路径

    import com.baomidou.mybatisplus.annotation.DbType;
    import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
    import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    @MapperScan("com.yuan.mapper")
    public class MybatisPlusConfig {
    
        /**
         * 添加分页插件
         */
        @Bean
        public MybatisPlusInterceptor mybatisPlusInterceptor() {
            MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
            interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));//如果配置多个插件,切记分页最后添加
            //interceptor.addInnerInterceptor(new PaginationInnerInterceptor()); 如果有多数据源可以不配具体类型 否则都建议配上具体的DbType
            return interceptor;
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    4.6.2.封装分页信息

    准备封装用于分页查询的工具类 BrandInfoPage

    import lombok.Data;
    
    @Data
    public class BrandInfoPage {
    
        private Integer pageNo = 1;
        private Integer pageSize = 10;
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    4.6.3.serviceImpl

    服务层查询

    import com.baomidou.mybatisplus.core.metadata.IPage;
    import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
    import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
    import com.yuan.entity.BrandInfo;
    import com.yuan.mapper.BrandInfoMapper;
    import com.yuan.page.BrandInfoPage;
    import com.yuan.service.BrandInfoService;
    import org.springframework.stereotype.Service;
    
    
    @Service
    public class BrandInfoServiceImpl extends ServiceImpl<BrandInfoMapper, BrandInfo>
        implements BrandInfoService{
    
        @Override
        public IPage<BrandInfo> listForPage(BrandInfoPage page) {
            IPage<BrandInfo> pageInfo = new Page<>();
            // 设置页号
            pageInfo.setCurrent(page.getPageNo());
            // 设置页大小
            pageInfo.setSize(page.getPageSize());
    
            return baseMapper.selectPage(pageInfo, null);
        }
    }
    
    • 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

    baseMapper.selectPage(pageInfo, null); 第二个参数为 QueryWrapper 用于条件查询

    4.6.4.返回 Controller类

    分析数据

    import com.baomidou.mybatisplus.core.metadata.IPage;
    import com.yuan.entity.BrandInfo;
    import com.yuan.page.BrandInfoPage;
    import com.yuan.service.BrandInfoService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class BrandInfoController {
    
        @Autowired
        private BrandInfoService brandInfoService;
    
        @RequestMapping("/brandInfo/query")
        public IPage<BrandInfo> query(BrandInfoPage page){
            System.out.println("page = " + page);
    
            IPage<BrandInfo> list = brandInfoService.listForPage(page);
            System.out.println("list = " + list);
            
            System.out.println("分页数据集合 = " + list.getRecords());
            System.out.println("当前页号 = " + list.getCurrent());
            System.out.println("每页最多多少条 = " + list.getSize());
            System.out.println("总记录数 = " + list.getTotal());
            System.out.println("总页数 = " + list.getPages());
        }
    }
    
    • 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

    4.6.5.增加条件查询

    4.6.5.1.修改 page类
    import lombok.Data;
    
    @Data
    public class BrandInfoPage {
    
        private Integer pageNo = 1;
        private Integer pageSize = 10;
    
        /**
         * 中文名 模糊
         */
        private String cnName;
        /**
         * 英文名 精确
         */
        private String enName;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    4.6.5.2.修改ServiceImpl
    import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
    import com.baomidou.mybatisplus.core.metadata.IPage;
    import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
    import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
    import com.yuan.entity.BrandInfo;
    import com.yuan.mapper.BrandInfoMapper;
    import com.yuan.page.BrandInfoPage;
    import com.yuan.service.BrandInfoService;
    import org.springframework.stereotype.Service;
    import org.springframework.util.StringUtils;
    
    
    @Service
    public class BrandInfoServiceImpl extends ServiceImpl<BrandInfoMapper, BrandInfo>
        implements BrandInfoService{
    
        @Override
        public IPage<BrandInfo> listForPage(BrandInfoPage page) {
            IPage<BrandInfo> pageInfo = new Page<>();
            // 设置页号
            pageInfo.setCurrent(page.getPageNo());
            // 设置页大小
            pageInfo.setSize(page.getPageSize());
    
            LambdaQueryWrapper<BrandInfo> queryWrapper = new LambdaQueryWrapper<BrandInfo>()
                    .like(page.getCnName() != null, BrandInfo::getBrandCnName, page.getCnName())
                    .eq( !StringUtils.isEmpty(page.getEnName()), BrandInfo::getBrandEnName, page.getEnName())
                    .orderByAsc(BrandInfo::getBrandNum)
                    ;
            return baseMapper.selectPage(pageInfo, queryWrapper);
        }
    }
    
    • 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
  • 相关阅读:
    arthas怎么在容器中配置
    器利而工善,以RPA+LCAP赋能企业司库管理数字化升级
    怎么提取一个python文件中所有得函数名称
    用实例带你深入理解Java内存模型
    JVM 虚拟机 ---->垃圾收集算法
    grafana 监控无图解决
    创建飞书自定义机器人发送 Gerrit 消息提醒
    chrome插件-Web开发者助手 FeHelper
    vue 创建vue项目
    KMP中的资源处理(字符串,图片等)
  • 原文地址:https://blog.csdn.net/yuanchun05/article/details/132818635