基于mybatis-plus自定义sql的分页查询返回

概述

mybatis-plus中为我们提供了Page对象进行分页,你可以是BaseMapper中的selectPage方法进行分页返回,进行自定义分页对象返回,减少代码量。

Service类

    public PageResult<TbProblem> hotlistByReply(int labelid,int currentPage,int size){

            Page<TbProblem> page = new Page<>(currentPage,size);
            IPage<TbProblem> tbProblemIPage = tbProblemMapper.hotlistByReply(page, labelid + "");
            return new PageResult<TbProblem>(tbProblemIPage.getTotal(),tbProblemIPage.getRecords());


    }

对参数进行简要说明:

Page page = new Page<>(currentPage,size);

currentPage为当前页,size为每页显示的条数

IPage tbProblemIPage = tbProblemMapper.hotlistByReply(page, labelid + “”);
IPage中的getTotal方法返回总记录数,getRecords方法返回分页查询数据

需要注意的是受mybatis-plus影响,page必须为第一个参数,它的返回会是IPage对象。

配置类

使用Page对象必须要在Spring中配置mybatis-plus的分页插件

@Configuration
public class MyConfigRationBean {
    

    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }



}

Mapper接口

public interface TbProblemMapper extends BaseMapper<TbProblem> {

    IPage<TbProblem> hotlistByReply(Page<?> page,String labelid);

}

mapper文件中的sql语句

    <select id="hotlistByReply" resultType="com.tensquare.qa.entity.TbProblem">

        SELECT * from tb_problem,tb_pl WHERE problemid=id and labelid=#{labelid}

    </select>

结果

2020-09-07 14:09:45.750  INFO 10492 --- [nio-9003-exec-2] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2020-09-07 14:09:45.795 DEBUG 10492 --- [nio-9003-exec-2] c.t.q.d.TbProblemMapper.hotlistByReply   : ==>  Preparing: SELECT COUNT(1) FROM tb_problem, tb_pl WHERE problemid = id AND labelid = ?
2020-09-07 14:09:45.811 DEBUG 10492 --- [nio-9003-exec-2] c.t.q.d.TbProblemMapper.hotlistByReply   : ==> Parameters: 1(String)
2020-09-07 14:09:45.826 DEBUG 10492 --- [nio-9003-exec-2] c.t.q.d.TbProblemMapper.hotlistByReply   : ==>  Preparing: SELECT * from tb_problem,tb_pl WHERE problemid=id and labelid=? LIMIT ?
2020-09-07 14:09:45.827 DEBUG 10492 --- [nio-9003-exec-2] c.t.q.d.TbProblemMapper.hotlistByReply   : ==> Parameters: 1(String), 2(Long)
2020-09-07 14:09:45.833 DEBUG 10492 --- [nio-9003-exec-2] c.t.q.d.TbProblemMapper.hotlistByReply   : <==      Total: 2

多个参数

其实有时候,我们会需要按开始时间和结束时间查询数据,同时又要求分页返回,怎么办?同样可以使用mybatis-plus的自定义sql的分页查询返回。

但是我们需要往mybatis中传多个参数,这该怎么办呐?

有两个办法?
1.一个是使用@Param注解指定参数名,往里传
2.另一个是将其封装在对象中,用@Param注解指定参数名,通过参数名.startTime获取

先说第一个

接口如下:

IPage<OpeLiveDataSituation> findLiveDataSituation(Page<?> page,@Param("startTime") Date startTime,@Param("endTime") Date endTime)

重点在@Param("startTime) Date startTime,@Param(“endTime”) Date endTime这句上

<select id="test" resultType="com.test">
SELECT
NAME,
SEX
WHERE
CREATETIME
BETWEEN
TO_DATE(TO_CHAR(#{startTime},'yyyy-MM-dd hh24:mi:ss'),'yyyy-MM-dd hh24:mi:ss)
AND
TO_DATE(TO_CHAR(#{endTime},'yyyy-MM-dd hh24:mi:ss'),'yyyy-MM-dd hh24:mi:ss)
</select>

如果是封装在对象中呐?

IPage<OpeLiveDataSituation> findLiveDataSituation(Page<?> page,@Param("ope") OpeVo)
SELECT NAME, SEX WHERE CREATETIME BETWEEN TO_DATE(TO_CHAR(#{ope.startTime},'yyyy-MM-dd hh24:mi:ss'),'yyyy-MM-dd hh24:mi:ss) AND TO_DATE(TO_CHAR(#{ope.endTime},'yyyy-MM-dd hh24:mi:ss'),'yyyy-MM-dd hh24:mi:ss) ``` 重点在呐? 在@Param("ope") OpeVo和#{ope.startTime}上,有人说直接用#{startTime}不就行了?

它封装在了对象里,实际上,不行,你用mybatis-plus自定义sql的分页查询,它会报不能找到参数错误。

至于为什么,未探究

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值