• MybatisPlus【SpringBoot】 6 插件 6.1 分页插件 & 6.2 xml 自定义分页


    MybatisPlus【SpringBoot】

    【【尚硅谷】2022版MyBatisPlus教程(一套玩转mybatis-plus)】

    6 插件

    6.1 分页插件

    MyBatis Plus自带分页插件,只要简单的配置即可实现分页功能

    6.1.1 添加配置类
    package com.dingjiaxiong.mybatisplus.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;
    
    /**
     * ClassName: MybatisPlusConfig
     * date: 2022/10/14 10:56
     *
     * @author DingJiaxiong
     */
    
    @Configuration
    @MapperScan("com.dingjiaxiong.mybatisplus.mapper") //可以将启动类中的注解写到这儿
    public class MybatisPlusConfig {
    
        @Bean
        public MybatisPlusInterceptor mybatisPlusInterceptor(){
            MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
            interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
    
            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
    • 25
    • 26
    • 27
    • 28

    在这里插入图片描述

    6.1.2 测试

    在这里插入图片描述

    在这里插入图片描述

    6.2 xml 自定义分页
    6.2.1 UserMapper中定义接口方法
    package com.dingjiaxiong.mybatisplus.mapper;
    
    import com.baomidou.mybatisplus.core.mapper.BaseMapper;
    import com.baomidou.mybatisplus.core.metadata.IPage;
    import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
    import com.dingjiaxiong.mybatisplus.domain.User;
    import org.apache.ibatis.annotations.Param;
    import org.apache.ibatis.annotations.Select;
    import org.springframework.stereotype.Repository;
    
    /**
     * ClassName: UserMapper
     * date: 2022/10/12 19:39
     *
     * @author DingJiaxiong
     */
    
    @Repository
    public interface UserMapper extends BaseMapper<User> {
    
        /*
         * 根据年龄查询用户列表,分页显示
         * */
        Page<User> selectPageVo(@Param("page") Page<User> page, @Param("age") Integer age);
    }
    
    • 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
    6.2.2 UserMapper.xml 中编写SQL
    
    DOCTYPE mapper
            PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="com.dingjiaxiong.mybatisplus.mapper.UserMapper">
    
        <sql id="BaseColumns">uid,username,age,emailsql>
    
        <select id="selectPageVo" resultType="com.dingjiaxiong.mybatisplus.domain.User">
            select <include refid="BaseColumns">include> from t_user where age > #{age}
        select>
    mapper>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    在这里插入图片描述

    6.2.3 测试
    @Test
    public void testSelectPageVo(){
        //设置分页参数
        Page<User> page = new Page<>(1,5);
        userMapper.selectPageVo(page,20);
        //获取分页数据
        List<User> list = page.getRecords();
        list.forEach(System.out::println);
    
        System.out.println("当前页:" + page.getCurrent());
        System.out.println("每页显示的条数:" + page.getSize());
        System.out.println("总记录数:" + page.getTotal());
        System.out.println("总页数:" + page.getPages());
        System.out.println("是否有上一页:" + page.hasPrevious());
        System.out.println("是否有下一页:" + page.hasNext());
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    测试结果

    在这里插入图片描述

    这是因为字段名和属性不一致导致的,

    最简单的一种办法,就是在映射文件的xml SQL语句中使用别名

    在这里插入图片描述

  • 相关阅读:
    SQL中的非重复计数(进阶)
    各语言语法
    操作系统学习笔记(V):设备管理
    理解HTTP、HTTPS、TCP、UDP与OSI七层模型:网络访问的基础
    一分钟总结Spring的IOC和DI
    urllib.error.URLError 提示错误解决方法和爬虫基本知识
    手机备忘录可以设置密码吗 能锁屏加密的备忘录
    04UE4 C++ 入门【力和扭矩】
    python每日一题【剑指 Offer 48. 最长不含重复字符的子字符串】
    使用GPT和FastAPI构建智能数据库查询服务器
  • 原文地址:https://blog.csdn.net/weixin_44226181/article/details/127456924