mybatis-plus 分页插件很好用

  1. 导入依赖包
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.2.0</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-generator</artifactId>
            <version>3.3.1.tmp</version>
        </dependency>

        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-extension</artifactId>
            <version>3.4.1</version>
        </dependency>

2.在mapper.xml中编写sql语句

    <select id="getKeyword" parameterType="com.creaditx.adnetworkbackend.model.req.KeywordReq" resultType="com.creaditx.adnetworkbackend.model.res.KeywordRes">

        SELECT bb.account,plan,unit,keyword,display,click,round(cost,2) as cost,if(opp_cnt is null,0,opp_cnt) as businessNum,round(if(opp_cnt is null,0,cost/opp_cnt),2) as businessCost
        FROM
        ( SELECT account,plan,unit,keyword,utm_medium,SUM(display) AS display,SUM(click) AS click ,SUM(cost) AS cost,type,date
        FROM detail_data_media_hour b
        WHERE  cost>0 AND b.date = date_format(curdate(),'%Y-%m-%d')
        <if test="keywordReq.accounts != null and keywordReq.accounts.size() > 0">
            and b.account in
            <foreach collection="keywordReq.accounts" index="index" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="keywordReq.channel != null and keywordReq.channel != ''   ">
            <if test="keywordReq.channel == 1 ">
                and b.type = '百度_搜索'
            </if>
            <if test="keywordReq.channel == 2 ">
                and b.type = '头条_信息流'
            </if>
        </if>
        GROUP BY account,plan,unit,keyword,utm_medium) bb
        left join opp_cnt aa on aa.utm_medium = bb.utm_medium and aa.date = bb.date
        order by bb.date

    </select>
    

3.在mapper.java中编写方法

    IPage<KeywordRes> getKeyword(@Param("page") Page<KeywordRes> page, @Param("keywordReq") KeywordReq keywordReq);

4.在实现类impl中实现方法

    private RestfulResult<ResponseEntity> getKeywords(KeywordReq keywordReq,Integer index, Integer pageSize) {
        RestfulResult<ResponseEntity> restfulResult = new RestfulResult<>();
        Page<KeywordRes> page = new Page<>(index, pageSize);
        IPage<KeywordRes> iPage = realTimeBoardDatacollectMapper.getKeyword(page, keywordReq);
        List<KeywordExcel> KeywordExcels = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(iPage.getRecords())) {
            for (KeywordRes keywordRes : iPage.getRecords()) {
                KeywordExcel keywordExcel = new KeywordExcel();
                BeanUtils.copyProperties(keywordRes, keywordExcel);
                KeywordExcels.add(keywordExcel);
            }
        }
        ResponseEntity responseEntity = new ResponseEntity();
        responseEntity.setList(KeywordExcels);
        responseEntity.setTotal(iPage.getTotal());
        restfulResult.setInstall(ResCode.SUCCESS, responseEntity);
        return restfulResult;
    }

分页就这样完成了,很好用吧

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

辞暮尔尔-烟火年年

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值