优化 mybatis-plus 分页查询 / 可自定义总数查询sql的 MybatisPlus 分页插件 CustomizablePaginationInterceptor

CustomizablePaginationInterceptor (SpringBoot)

Mybatis-plus.HuanyuMake

CustomizablePaginationInterceptor.java 文件下载: Github源码地址

发现网上没有使用 PaginationInnerInterceptor 来自定义总数查询sql的方法,遂自创之
Compare with the PaginationInnerInterceptor, the Interceptor supports user to custom the SQL to query total records of you other SQL result
与Mybatis-plus官方提供的内置分页插件 PaginnationInnerInterceptor 相比,该拦截器支持用户自定义用来查总记录数的sql语句

CustomizablePaginationInterceptor (Alias: Customizable)
PaginnationInnerInterceptor (Alias: Inner)
Customizable 继承 Inner
Customizable只是对原生Inner的拓展,使用Customizable插件不会给原项目带来额外负担

1.在 application.yaml 中的设置

mybatis-plus:
  type-aliases-package: org.HuanyuMake.pojo 
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    map-underscore-to-camel-case: true
    cache-enabled: true
    # 以下设置 CustomizablePaginationInterceptor Bean的属性
    customizable-pagination-interceptor:
      count-field: 'COUNT(1)'   # 自定义 select countField from (xx) 的countField默认内容, 默认为 'COUNT(1)'
      count-field-alias: 'total' # 自定义 select countField AS countFieldAlias from (xx) countFieldAlias 内容, 默认为 'total'
      use-mapper-count-sql: true  # 开启对同mapper中 总记录查询语句的<select>字句的使用, 默认为 false
      count-sql-suffix: '_COUNT' # 选择使用什么后缀的同id<select>子句作为总记录数查询sql, 默认为'_COUNT'
      max-limit: 2000  # 自定义一页最多有多少条记录数
      

2.在项目目录中放置 CustomizablePaginationInterceptor.java 源文件,记得改包名

package com.yourCom.project.mybatisPlus.Interceptor;

public class CustomizablePaginationInterceptor extends PaginationInnerInterceptor {...}

3.注册 CustomizablePaginationInterceptor 插件

@Configuration
@Getter
@Setter
public class MybatisPlusConfig {

   private final CustomizablePaginationInterceptor cpi;
   @Bean
   public MybatisPlusInterceptor mybatisPlusInterceptor() {
       MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();

       interceptor.addInnerInterceptor(cpi);
       return interceptor;
   }

   @Autowired
   public MybatisPlusConfig(CustomizablePaginationInterceptor cpi) {
       this.cpi = cpi;
   }
}

4.在mapper.xml中配置对应自定义查询sql,这里引用 其它说明第3点 的例子

5.调用mapper接口方法, 这里调用的是 selectPage(new Page<>(1,10),null)

6.控制台日志

==>  Preparing: select TABLE_ROWS AS total from information_schema.TABLES where TABLE_SCHEMA = 'project' AND TABLE_NAME = 'users'
==> Parameters: 
<==    Columns: total
<==        Row: 12
<==      Total: 1
==>  Preparing: SELECT id,name,login_state,latest_login_time FROM users LIMIT ?
==> Parameters: 10(Long)

其它说明

  1. 以上 customizable-pagination-interceptor 下的属性都不是必要项, 都有默认值, 按需设置就行

  2. 不注册插件的话, CustomizablePaginationInterceptor是不会工作的

  3. 如果不设置 use-mapper-count-sqltrue, 则不能自动使用自定义sql来查询总记录数
    使用以上配置, 例子:

   mapper.xml

   <!--这是支持 selectPage(Page<T>) 分页方法查询总数的sql-->
  <select id="selectAllSubscriberById" parameterType="int" resultType="user">
      SELECT subscriber.*
      FROM blogger, subscriber
  </select>

  <!-- 设置了use-mapper-count-sql = true , 且 count-sql-suffix = '_COUNT' 则会使用该句查询总记录数 -->
  <!-- 这是自定义 selectAllSubscriberById sql的总数查询sql的sql语句, 但要注意这些语句的 resultType 必须为 java.lang.Long-->
  <select id="selectAllSubscriberById_COUNT" parameterType="int" resultType="Long">
      SELECT COUNT(subscriber.id)
      FROM blogger, subscriber
  </select>

  <!-- 自定义mybatis-plus中selectPage的总数查询sql -->
  <select id="selectPage_COUNT" resultType="Long">
      select TABLE_ROWS AS total from information_schema.TABLES where TABLE_SCHEMA = 'project' AND TABLE_NAME = 'users'
  </select>
  1. pom.xml 概览
   <parent>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-parent</artifactId>
       <!-- 注意! 我这里使用的是SpringBoot3 -->
       <version>3.0.2</version>  
       <relativePath/>
   </parent>

   <dependencies>
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-web</artifactId>
       </dependency>

       <dependency>
           <groupId>org.mybatis.spring.boot</groupId>
           <artifactId>mybatis-spring-boot-starter</artifactId>
           <version>3.0.1</version>
       </dependency>
     
       <dependency>
           <groupId>com.baomidou</groupId>
           <artifactId>mybatis-plus-boot-starter</artifactId>
           <!-- 注意! 我使用的是3.x新版本的mybatis-plus-->
           <version>3.5.3.1</version> 
       </dependency>
   </dependencies>
  1. 版本不匹配咋办?
    A: 笔者没试过SpringBoot2 和 1 (初学者一枚), 这个不是很清楚. 依赖的 PaginnationInnerInterceptor 类好像在 mybatis-plus-boot-starter 2.x的老版本时好像不叫这个名字, 下载源码后直接修改使用即可
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
MyBatis-Plus提供了非常方便的分页查询功能,可以直接使用Page类来进行分页查询。 使用MyBatis-Plus的分页查询,需要进行以下步骤: 1. 引入MyBatis-Plus的依赖:在pom.xml中添加以下依赖: ``` <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.0</version> </dependency> ``` 2. 定义实体类:定义需要进行分页查询的实体类,并使用注解@TableField进行字段映射。 3. 定义Mapper接口:定义Mapper接口,并继承BaseMapper类,继承BaseMapper类后,MyBatis-Plus会自动提供一些基本的CRUD操作。 ``` public interface UserMapper extends BaseMapper<User> {} ``` 4. 分页查询:在Service层中调用分页查询方法,使用Page对象设置分页参数,然后调用selectPage方法进行分页查询。 ``` @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public Page<User> getUserList(int pageNum, int pageSize) { Page<User> page = new Page<>(pageNum, pageSize); return userMapper.selectPage(page, null); } } ``` 如果需要自定义sql分页,可以在xml中使用MyBatis的分页件进行分页查询。 1. 引入分页件:在pom.xml中添加以下依赖: ``` <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.1.10</version> </dependency> ``` 2. 配置分页件:在MyBatis的配置文件中配置分页件。 ``` <plugins> <plugin interceptor="com.github.pagehelper.PageInterceptor"> <property name="dialect" value="mysql"/> </plugin> </plugins> ``` 3. 自定义sql分页查询:在xml中使用分页件的PageHelper.startPage方法进行分页查询。 ``` <select id="getUserList" resultMap="userMap"> select * from user <where> <if test="name != null"> and name like concat('%',#{name},'%') </if> </where> order by id desc </select> ``` ``` @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public PageInfo<User> getUserList(int pageNum, int pageSize, String name) { PageHelper.startPage(pageNum, pageSize); List<User> userList = userMapper.getUserList(name); return new PageInfo<>(userList); } } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值