【项目demo02】分页插件的使用

我准备开一个系列,就是写一些在简要的学习项目中可能会用到的奇奇怪怪的功能,比如线程池或者统一异常处理类
取名为【项目demo】系列
然后将会同步到GitHub中:https://github.com/Livorth/FunctionalLearning


分页插件的使用

使用MybatisPlus的分页插件

这里参照官网的流程来写:https://baomidou.com/guide/page.html

其中最主要的就是添加MybatisPlus的分页配置,然后在使用时,传递的参数改为Page和IPage

  1. 进行一个MybatisPlus的配置

    注意有新旧两个版本,既然官方都给出新版了,那么直接用新版吧

    package cn.livorth.functionallearning.config;
    
    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;
    
    /**
     * @program: FunctionalLearning
     * @description: MybatisPlus配置
     * @author: livorth
     * @create: 2021-10-02 17:50
     **/
    @Configuration
    @MapperScan("cn.livorth.functionallearning.dao")
    public class MybatisPlusConfig {
    
        /**
         * 新版
         * 这里主要是在MybatisPlus的拦截器中添加page的拦截器,相较于老版泛用性更高
         * 实际使用上还是差不多的,然后记得在新建PaginationInnerInterceptor的时候注意自己是使用的哪个数据库
         * @return
         */
        @Bean
        public MybatisPlusInterceptor mybatisPlusInterceptor() {
            MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
            PaginationInnerInterceptor paginationInterceptor = new PaginationInnerInterceptor(DbType.MYSQL);
            // 添加一个单页最大数量,-1则不受限制
            paginationInterceptor.setMaxLimit(100L);
            interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
            return interceptor;
        }
    }
    
  2. 在UserMapper中添加分页查询的方法

    /**
         * 返回分页后的用户信息
         * @param page
         * @return
         */
    IPage<User> getAllUserByPage(Page<User> page);
    
  3. 既然是自己写的方法,那么也要写对应xml文件:UserMapper.xml

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    
    <mapper namespace="cn.livorth.functionallearning.dao.UserMapper">
    
        <resultMap id="userMap" type="cn.livorth.functionallearning.entity.User">
            <id column="user_id" property="userId" />
            <result column="user_name" property="userName"/>
            <result column="password" property="password"/>
        </resultMap>
    
        <select id="listArticle" resultType="cn.livorth.functionallearning.entity.User">
            select * FROM user
        </select>
    
    </mapper>
    
  4. 继续往上层写,即service层

    UserService.java

    /**
    * 返回分页后的用户信息
    * @return
    */
    List<User> getAllUserByPage(Page<User> page);
    

    UserServiceImpl.java

    @Override
    public List<User> getAllUserByPage(Page<User> page) {
        // 调用分页方法
        IPage<User> allUserByPage = userMapper.getAllUserByPage(page);
        // 转换为List
        List<User> records = allUserByPage.getRecords();
        return records;
    }
    
  5. 最后来写controller的请求

    UserController.java

    /**
    * /user/page/3/5
    * @param thePage 当前多少页
    * @param pageSize 每页多少个
    * @return
    */
    @GetMapping("page/{thePage}/{pageSize}")
    public List<User> getAllUserByPage(@PathVariable("thePage") int thePage, @PathVariable("pageSize") int pageSize){
        Page<User> page = new Page<>(thePage, pageSize);
        return userService.getAllUserByPage(page);
    }
    
  6. 接下来启动项目进行一个测试
    在这里插入图片描述

写在后面

其实实际上结合前端的分页插件,请求是post,之后再改就好了

然后就是其中的参数传递,直接使用Page我个人觉得不是很合理,不论是从前端传过来的数据还是传往Service层的数据,都可以使用vo或bo封装来进行传递


我的项目demo系列都会同步到GitHub上,欢迎围观

https://github.com/Livorth/FunctionalLearning

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值