mybatis-plus 分页插件很好用
- 导入依赖包
<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; }
分页就这样完成了,很好用吧 `` `
下一篇:
Java使用IO流实现文件的拷贝