mybatisplus分页使用

MyBatis Plus 实现多表分页查询_骑驴的小牧童的博客-CSDN博客_mybatis-plus多表分页

最简单的 MyBatis Plus 的多表联接、分页查询实现方法_IT小村的博客-CSDN博客_mybatisplus多表联查

Mybatis-Plus中分页插件PaginationInterceptor, MybatisPlusInterceptor在SpringBoot中的使用_Sakura_Lu_的博客-CSDN博客_paginationinterceptor

项目中有用到 mybatis-plus(mbp) 进行分页的需求,所以笔记记录下。

一.声明配置类

@Configuration
@EnableTransactionManagement(proxyTargetClass = true)
public class MybatisPlusConfiguration {

    /**
     * 分页插件
     */
    @Bean
    public MybatisPlusInterceptor paginationInnerInterceptor() {

        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        //声明针对分页的拦截器
        PaginationInnerInterceptor pageInterceptor = new PaginationInnerInterceptor();
        //声明数据库类型
        pageInterceptor.setDbType(DbType.MYSQL);
        //添加到拦截器链中
        interceptor.addInnerInterceptor(pageInterceptor);
        return interceptor;
    }
}

二.实体类

@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
public class RuleConfiguration implements Serializable {

    private static final long serialVersionUID=1L;

    /**
     * 自增主键ID
     */
    @TableId(value = "id", type = IdType.AUTO)
    private Long id;

    /**
     * 规则名称
     */
    @TableField("name")
    private String name;


    public static final String ID = "id";

    public static final String NAME = "name";
}

实体类中须包含 @TableField 等注解

三.mapper

public interface RuleConfigurationMapper extends BaseMapper<RuleConfiguration> {

    /**
     * 规则分页
     *
     * @param page 分页对象
     * @param name 规则名称
     * @return
     */
    List<ScheduleRulePageDTO> scheduleRulePage(Page page, @Param("name") String name);
}

方法参数须带上 Page 对象

四.Service

@Service
@Slf4j
public class RuleConfigurationServiceImpl implements RuleConfigurationService {

    @Autowired
    private RuleConfigurationMapper RuleConfigurationMapper;

    @Override
    public void page(PageReq req) {

        Page<RuleConfiguration> page = new Page<>(req.getPageNum(), req.getPageSize());
        List<RulePageDTO> RulePage = RuleConfigurationMapper.scheduleRulePage(page, req.getName());
    }
}

声明 Page 对象并且设置 分页数分页大小 属性,分页的方法传入 page 和查询条件即可完成分页。

五.结果

Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@6d5a95f6] was not registered for synchronization because synchronization is not active
//拦截器对sql进行相关拦截
2022-04-08 10:17:30.441  WARN 27080 --- [nio-8300-exec-5] c.b.m.e.p.i.PaginationInnerInterceptor   : optimize this sql to a count sql has exception, sql:"SELECT
            t1.id,
            t1.name
        FROM
            rule_configuration  t1 
        ORDER BY t1.updated_at DESC", exception:
null

//拼接 Limit 参数
2022-04-08 10:17:30.450 DEBUG 27080 --- [nio-8300-exec-5] c.m.s.m.s.I.scheduleRulePage             : ==>  Preparing: SELECT t1.id, t1.name FROM rule_configuration t1 ORDER BY t1.updated_at DESC LIMIT ?
2022-04-08 10:17:30.450 DEBUG 27080 --- [nio-8300-exec-5] c.m.s.m.s.I.scheduleRulePage             : ==> Parameters: 10(Long)
2022-04-08 10:17:30.456 DEBUG 27080 --- [nio-8300-exec-5] c.m.s.m.s.I.scheduleRulePage             : <==      Total: 1

从日志可以看出,PaginationInnerInterceptor会对包含page的参数的sql进行拦截并重新拼接Limit参数
不过手动分页也值得考虑~

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Mybatis-Plus 是 Mybatis 的增强工具,在 Mybatis 的基础上扩展了很多实用的功能,其中包括分页插件。 使用 Mybatis-Plus 分页插件非常简单,只需要按照以下步骤操作即可: 1. 在 pom.xml 文件中添加 Mybatis-Plus 和分页插件的依赖: ```xml <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.0.6</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-extension</artifactId> <version>3.0.6</version> </dependency> ``` 2. 在 Mybatis 的配置文件中添加分页插件的配置: ```xml <plugins> <plugin interceptor="com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor"> <property name="dialec" value="mysql"/> </plugin> </plugins> ``` 3. 在代码中使用分页查询: ```java // 构造分页对象 Page<User> page = new Page<>(1, 10); // 执行分页查询 IPage<User> userPage = userMapper.selectPage(page, new QueryWrapper<User>().lambda().ge(User::getAge, 18)); // 获取分页结果 List<User> userList = userPage.getRecords(); long total = userPage.getTotal(); ``` 以上代码中,首先构造了一个分页对象 `Page<User>`,该对象表示要查询第 1 页,每页 10 条记录。然后使用 `selectPage` 方法执行分页查询,该方法的第一个参数是分页对象,第二个参数是查询条件。最后通过 `userPage` 对象获取分页结果。 注意,在查询条件中使用了 `QueryWrapper`,这是 Mybatis-Plus 提供的一个方便构建查询条件的工具类。 以上就是使用 Mybatis-Plus 分页插件的基本步骤,希望能帮到你。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值