分页插件
调用通用mapper提供的selectPage分页方法;
2、自定义查询方法使用分页功能:
- mapper接口中的方法返回值是Page
- 第一个参数要求是Page
3、映射文件中可以使用sql标签定义公共sql片段,在要使用的地方使用include标签进行引用。
application.yml
server: port: 8080 #数据源配置spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/mybatisplus212?characterEncoding=utf-8&serverTimezone=UTC username: root password: 123456 type: com.zaxxer.hikari.HikariDataSourcemybatis-plus: configuration: #配置日志信息 log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #配置mapper映射文件的位置,默认在类路径/mapper*.xml mapper-locations: classpath:/mapper/*.xml #设置指定包下所有类设置类型别名 type-aliases-package: com.dzqc.pojo global-config: db-config: #统一设置实体类对应的数据表的默认前缀 table-prefix: t_ #设置主键自增 id-type: auto
创建一个MyBatisPlusConfig类,先建一个包config
package com.dzqc.config;import com.baomidou.mybatisplus.annotation.DbType;import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configurationpublic class MyBatisPlusConfig { @Bean public MybatisPlusInterceptor mybatisPlusInterceptor(){ MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor(); //添加分页插件 mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); //添加乐观锁插件 mybatisPlusInterceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor()); return mybatisPlusInterceptor; }}
测试类Page
package com.dzqc;import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;import com.baomidou.mybatisplus.extension.plugins.pagination.Page;import com.dzqc.mapper.UserMapper;import com.dzqc.pojo.User;import org.junit.jupiter.api.Test;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import java.util.List;@SpringBootTestpublic class PageTest { @Autowired private UserMapper userMapper; @Test public void testpage(){ //设置分页参数 Page page = new Page<>(1,2); //调用分页查询方法 userMapper.selectPage(page,null); List records = page.getRecords(); records.forEach(System.out::println); System.out.println("总页数:"+page.getPages()); System.out.println("总记录数:"+page.getTotal()); System.out.println("当前页码:"+page.getCurrent()); System.out.println("每页记录数:"+page.getSize()); System.out.println("是否有上一页:"+page.hasPrevious()); System.out.println("是否有下一页:"+page.hasNext()); } @Test public void testPage2(){ //设置分页参数 Page page = new Page<>(1, 2); //调用自定义分页查询方法 userMapper.selectPageByAge(page,20); //获取分页查询结果 List records = page.getRecords(); records.forEach(System.out::println); System.out.println("总页数:"+page.getPages()); System.out.println("总记录数:"+page.getTotal()); System.out.println("当前页码:"+page.getCurrent()); System.out.println("每页记录数:"+page.getSize()); System.out.println("是否有上一页:"+page.hasPrevious()); System.out.println("是否有下一页:"+page.hasNext()); }}
测试结果
来源地址:https://blog.csdn.net/m0_68367226/article/details/130587602