Mybatis-Plus实现分页查询功能Demo

一、Mybatis实现方式如下:

@Data
public class PagingResult<E> implements Serializable {
    private static final long serialVersionUID = -4925664861534366008L;
    private int count;
    private int current;
    private int pageSize;
    private List<E> results;
}    

PushScheduleTaskReq 返回值实体类

@Data
public class PushScheduleTaskReq extends MthPaging {
    @ApiModelProperty("用户组名称/任务名称")
    private String userGroupOrTaskName;
    @ApiModelProperty("推送状态")
    private Integer pushStatus;
}

PushTaskService

 /**
  * 分页查询定时推送任务,推送类型:1-定时
 */
PagingResult<PushScheduleTaskResp> getScheduleTaskList(PushScheduleTaskReq req);

PushTaskServiceImpl

/**
* 分页查询定时推送任务,推送类型:1-定时
*/
@Override
public PagingResult<PushScheduleTaskResp> getScheduleTaskList(PushScheduleTaskReq req) {
        int count = pushTaskMapper.countScheduleByCondition(req);
        Paging paging = new Paging(req.getPage(), req.getPageSize());
        PagingResult<PushScheduleTaskResp> pagingResult = new PagingResult<>(paging,count);
        if (count < 0) {
            return pagingResult;
        }
        List<PushScheduleTaskResp> userPushList = pushTaskMapper.pageScheduleList(req,paging);
        pagingResult.setResults(userPushList);
        return pagingResult;
    }

PushTaskMapper

 /**
 * 根据条件计算定时任务
 */
    int countScheduleByCondition(@Param(value="pushScheduleTaskReq") PushScheduleTaskReq pushScheduleTaskReq);
 /**
 * 分页查询定时任务
 */
    List<PushScheduleTaskResp> pageScheduleList(@Param(value="pushScheduleTaskReq") PushScheduleTaskReq pushScheduleTaskReq, @Param(value="paging") Paging paging);

PushTaskMapper.xml

 <select id="countScheduleByCondition" resultType="java.lang.Integer">
        select
            count(*)
        from a
            where
            1= 1
            <if test="pushScheduleTaskReq.userGroupOrTaskName != null and pushScheduleTaskReq.userGroupOrTaskName != ''">
                and (
                <bind name="userGroupOrTaskName" value="'%'+pushScheduleTaskReq.userGroupOrTaskName+'%'"/>
                a.pushTaskName like #{userGroupOrTaskName}
                or a.pushTarget like #{userGroupOrTaskName}
                )
            </if>
    </select>

<select id="pageScheduleList"  resultType="com.service.api.response.pushScheduleTaskResp">
      select *
        from a
      where
        1= 1
        <if test="pushScheduleTaskReq.userGroupOrTaskName != null and pushScheduleTaskReq.userGroupOrTaskName != ''">
            and (
                <bind name="userGroupOrTaskName" value="'%'+pushScheduleTaskReq.userGroupOrTaskName+'%'"/>
                a.pushTaskName like #{userGroupOrTaskName}
                or a.pushTarget like #{userGroupOrTaskName}
            )
        </if>
</select>

二、利用mybatis-plus改造

PushTaskServiceImpl

/**
 * 分页查询定时推送任务,推送类型:1-定时
*/
    @Override
    public PagingResult<PushScheduleTaskResp> getScheduleTaskList(PushScheduleTaskRespreq) {
        IPage<PushScheduleTaskResp> page = new Page<>(req.getPage(),req.getPageSize());
        Page<PushScheduleTaskResp> tasks = baseMapper.pageScheduleList(page,req);
        return PagingUtils.getPagingResult(tasks);
    }	

PushTaskMapper

   /**
   *分页查询定时任务
   */
    Page<PushScheduleTaskResp> pageScheduleList(IPage<PushScheduleTaskResp> page, @Param("pushScheduleTaskReq") PushScheduleTaskReq pushScheduleTaskReq);

SQL实现语句不变即PushTaskMapper.xml

参考文章
https://www.jianshu.com/p/3fc4abf2cd31
https://www.cnblogs.com/rrong/p/13629550.html

上一篇:uni-app实现uniPush推送功能(Android)


下一篇:Mybatis-Plus对比Mybatis优点