欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

自定义分页查询入参与返回

程序员文章站 2022-03-21 11:35:50
在后端开发在编写接口向前端提供列表数据的时候,往往需要进行分页处理。下面以ssm框架整合为例。1、编写接口分页请求基类入参基类PageQuery ,定义当前页码pageNo、每页记录大小pageSize。请求对象继承基类即可import java.io.Serializable;import io.swagger.annotations.ApiModel;import io.sw......

在后端开发在编写接口向前端提供列表数据的时候,往往需要进行分页处理。下面以ssm框架整合为例。

1、编写接口分页请求基类

入参基类PageQuery ,定义当前页码pageNo、每页记录大小pageSize。请求对象继承基类即可

import java.io.Serializable;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
@ApiModel(value = "分页查询参数")
@Data
@ToString
@AllArgsConstructor
@NoArgsConstructor
public class PageQuery implements Serializable{
   private static final long serialVersionUID = -7219221162786108170L;
   @ApiModelProperty(value="当前页面")
   private Integer pageNo = 1;
   @ApiModelProperty(value="每页几行")
   private Integer pageSize = 10;
   public static PageQuery def() {
      PageQuery query = new PageQuery();
      query.setPageNo(1);
      query.setPageSize(10);
      return query;
   }
}

2、定义返回结果集

返回对象结果集封装在PageResult中

public class PageResult<T> implements Serializable {
   private static final long serialVersionUID = 2626585127466370060L;
   /**当前页面 */
   private Integer pageNo = 1;
   /**每页几行*/
   private Integer pageSize = 10;
   /**数据总数*/
   private Integer totalCount;
   /**行总数*/
   private Integer totalPage;
   private List<T> list;
   public PageResult(){
   }
   public Integer getPageNo() {
      return pageNo != null && pageNo>0?pageNo:1;
   }
   public void setPageNo(Integer pageNo) {
      this.pageNo = pageNo;
   }
   public Integer getPageSize() {
      return pageSize == null ? 10 : pageSize;
   }
   public void setPageSize(Integer pageSize) {
      this.pageSize = pageSize;
   }
   public Integer getTotalCount() {
      return totalCount;
   }
   public void setTotalCount(Integer totalCount) {
      this.totalCount = totalCount;
   }
   public Integer getTotalPage() {
      if(pageSize ==0 ) return 0;
      return totalCount/pageSize+(totalCount%pageSize==0?0:1);
   }
   public void setTotalPage(Integer totalPage) {
      this.totalPage = totalPage;
   }
   public List<T> getList() {
      return list;
   }
   public void setList(List<T> list) {
      this.list = list;
   }
   public Integer startPageNo(PageQuery query){
      this.pageNo = query.getPageNo();
      this.pageSize = query.getPageSize();
      return (getPageNo()-1)*pageSize;
   }
}

3、mapper.xml文件中的sql语句。其中getDrawInfoCreditOrNon为根据条件获取数据,getCountDrawInfoCreditOrNon为获取总记录数

<select id="getDrawInfoCreditOrNon"
        parameterType="com.shitou.huishi.contract.datacontract.request.web.loan.QueryLoanApproveListRequest"
        resultType="com.shitou.huishi.contract.datacontract.response.web.loan.DrawInfoListResponse">
  select
  c.draw_id drawId,
  d.credit_id creditId,
  a.product_type productName,
  a.org_source orgSource,
  e.subject_no subjectNo,
  e.subject_no_type subjectNoType,
  CASE a.loan_type
  WHEN 1 THEN a.main_subject
  WHEN 2 THEN a.borrower_name
  END subjectName,
  c.draw_amount drawAmount,
  b.apply_period LoanLimit,
  c.draw_status drawStatus,
  c.loan_platform LoanPlatForm,
  c.approve_status approveStatus,
  c.create_user_id approveUserName,
  c.process_instance_id processInstanceId,
  DATE_FORMAT(c.create_time,'%Y-%m-%d %H:%i:%s') applyTime
  from hs_draw_info c
  INNER JOIN hs_loan_order a ON a.`loan_order_id` = c.`loan_order_id`
  INNER JOIN hs_loan_order_detail b ON a.`loan_order_id` = b.`loan_order_id`
  INNER JOIN hs_user_credit_info d ON d.`loan_order_id` = c.`loan_order_id`
  INNER JOIN hs_archive_info e ON e.archive_id =
  (CASE a.loan_type
  WHEN  1 THEN  a.main_subject_archive_id
  WHEN  2 THEN  a.borrower_archive_id
  END
  )
  WHERE 1=1
  <if test="null!=isCredit and isCredit!='' ">
    and b.is_credit = #{isCredit,jdbcType=VARCHAR}
  </if>
  <if test="null!=drawId">
    and c.draw_id=#{drawId,jdbcType=BIGINT}
  </if>
  <if test="null!=orgSource and orgSource!='' ">
    and a.org_source=#{orgSource,jdbcType=VARCHAR}
  </if>
  <if test="null!=productType">
    and a.product_type=#{productType,jdbcType=BIGINT}
  </if>
  <if test="null!=subjectName and ''!=subjectName">
    and e.subject_name=#{subjectName,jdbcType=VARCHAR}
  </if>
  <if test="null!=subjectNo and ''!=subjectNo">
    and e.subject_no=#{subjectNo,jdbcType=VARCHAR}
  </if>
  <if test="null!=creditId and creditId!='' ">
    and d.credit_id=#{creditId,jdbcType=VARCHAR}
  </if>
  <if test="null!=drawStatus and drawStatus!='' ">
    and c.draw_status=#{drawStatus,jdbcType=Integer}
  </if>
  ORDER by c.draw_id DESC
  limit #{pageNo}, #{pageSize}
</select>
<select id="getCountDrawInfoCreditOrNon"
        parameterType="com.shitou.huishi.contract.datacontract.request.web.loan.QueryLoanApproveListRequest"
        resultType="java.lang.Integer">
  select count(1)
  from hs_draw_info c
  INNER JOIN hs_loan_order a ON a.`loan_order_id` = c.`loan_order_id`
  INNER JOIN hs_loan_order_detail b ON a.`loan_order_id` = b.`loan_order_id`
  INNER JOIN hs_user_credit_info d ON d.`loan_order_id` = c.`loan_order_id`
  INNER JOIN hs_archive_info e ON e.archive_id =
  (CASE a.loan_type
  WHEN  1 THEN  a.main_subject_archive_id
  WHEN  2 THEN  a.borrower_archive_id
  END
  )
  WHERE 1=1
  <if test="null!=isCredit and isCredit!='' ">
    and b.is_credit = #{isCredit,jdbcType=VARCHAR}
  </if>
  <if test="null!=drawId">
    and c.draw_id=#{drawId,jdbcType=BIGINT}
  </if>
  <if test="null!=orgSource and orgSource!='' ">
    and a.org_source=#{orgSource,jdbcType=VARCHAR}
  </if>
  <if test="null!=productType">
    and a.product_type=#{productType,jdbcType=BIGINT}
  </if>
  <if test="null!=subjectName and ''!=subjectName">
    and e.subject_name=#{subjectName,jdbcType=VARCHAR}
  </if>
  <if test="null!=subjectNo and ''!=subjectNo">
    and e.subject_no=#{subjectNo,jdbcType=VARCHAR}
  </if>
  <if test="null!=creditId and creditId!='' ">
    and d.credit_id=#{creditId,jdbcType=VARCHAR}
  </if>
  <if test="null!=drawStatus and drawStatus!='' ">
    and c.draw_status=#{drawStatus,jdbcType=Integer}
  </if>
</select>

4、domain类

@MyBatisMapper
public interface DrawInfoMapper extends BaseMapper<DrawInfo, Long> {
   List<DrawInfoListResponse> getDrawInfoCreditOrNon(QueryLoanApproveListRequest request);
   int getCountDrawInfoCreditOrNon(QueryLoanApproveListRequest request);
}

5、Impl类

@Service
public class DrawInfoBizFacadeImpl implements DrawInfoBizFacade {
    @Resource
    private DrawInfoMapper mapper;
    public int insert(DrawInfo drawinfo) {
        return mapper.insert(drawinfo);
    }
    public int insertSelective(DrawInfo drawinfo) {
        return mapper.insertSelective(drawinfo);
    }
    public DrawInfo selectByPrimaryKey(Long drawId) {
        return mapper.selectByPrimaryKey(drawId);
    }
    public int updateByPrimaryKeySelective(DrawInfo drawinfo) {
        return mapper.updateByPrimaryKeySelective(drawinfo);
    }
    public int updateByPrimaryKey(DrawInfo drawinfo) {
        return mapper.updateByPrimaryKey(drawinfo);
    }
    @Override
    public PageResult<DrawInfoListResponse> getDrawInfoCreditOrNon(QueryLoanApproveListRequest request) {
        PageResult<DrawInfoListResponse> page = new PageResult<>();
        request.setPageNo(page.startPageNo(new PageQuery(request.getPageNo(),request.getPageSize())));
        List<DrawInfoListResponse> data = mapper.getDrawInfoCreditOrNon(request);
        page.setList(data);
        page.setTotalCount(mapper.getCountDrawInfoCreditOrNon(request));
        return page;
    }
}

6、BizzFacade接口类

public interface DrawInfoBizFacade {
    int insert(DrawInfo drawinfo);
    int insertSelective(DrawInfo drawinfo);
    DrawInfo selectByPrimaryKey(Long drawId);
    int updateByPrimaryKeySelective(DrawInfo drawinfo);
    int updateByPrimaryKey(DrawInfo drawinfo);
    PageResult<DrawInfoListResponse> getDrawInfoCreditOrNon(QueryLoanApproveListRequest request);
}

7、业务调用,红色部分

/**
 * 授信/非授信项下订单提款列表
 * @param query
 * @return
 */
public PageResult<DrawInfoListResponse> queryCreditOrNonDrawInfoList(QueryLoanApproveListRequest query){
   PageResult<DrawInfoListResponse> result=new PageResult<>();
   PageResult<DrawInfoListResponse> drawInfoList = drawInfoBizFacade.getDrawInfoCreditOrNon(query);
   if(null==drawInfoList || null==drawInfoList.getList() || drawInfoList.getList().size()<1){
      return result;
   }
   List<DataDict> dataDictList=dataDictManage.getDataDictByParentKey(DataDictConstant.productType);
   drawInfoList.getList().forEach(x->{
      x.setProductName(dataDictList.stream().filter(y->y.getDictId().equals(x.getProductName())).map(y->y.getDictValue()).findFirst().orElse(null));
      x.setDrawStatus(DrawStatusEnum.getEnum(Integer.parseInt(x.getDrawStatus())).getDesc());
      TaskEntity currentTaskEntity=getCurrentTaskEntity(x.getProcessInstanceId());
      x.setApproveStatus(currentTaskEntity==null?null:currentTaskEntity.getTaskName());
      x.setApproveUserName(currentTaskEntity==null?null:currentTaskEntity.getUserName());   x.setLoanPlatform(LoanPlatformEnum.getEnum(Integer.parseInt(x.getLoanPlatform())).getDesc());
   });
   return drawInfoList;
}

本文地址:https://blog.csdn.net/u013252149/article/details/52788805

相关标签: java