Java 多输入框查询需求实现

在这里插入图片描述

wei_shuo的个人主页

wei_shuo的学习社区

Hello World !


多输入框查询

需求分析

在这里插入图片描述

任意一个输入框,输入内容点击搜索都可以精准搜索到对应的内容

代码实现

Controller接口编写

 @PostMapping("merchant/manage")
 public Result<PageResult<DisputeMerchantManageResponse>>
 merchantDisputeManage(@RequestBody DisputeMerchantManageRequest request) {
     return Result.succ(merchantDisputeFacade.merchantDisputeManage(request));
 }
  • Result>:返回给前端的字段:VO
  • @RequestBody DisputeMerchantManageRequest request:接收前端传递的JSON数据:BO
  • merchantDisputeFacade.merchantDisputeManage(request):调用Service的merchantDisputeManage方法,传递接受的参数request

Service编写

MerchantDisputeFacade.java

public PageResult<DisputeMerchantManageResponse> merchantDisputeManage(DisputeMerchantManageRequest request) {
       DisputeMerchantManageBO manageBO = DisputeMerchantManageBO.convert(request);
        
       List<DisputeMerchantManageResponse> list = merchantDisputeService.merchantDisputeManage(manageBO);
    
       PageResult<DisputeMerchantManageResponse> pageResult = PageResult.		        				           <DisputeMerchantManageResponse>builder().pageNo(Integer.parseInt(request.getPageNo()))
                .pageSize(Integer.parseInt(request.getPageSize()))
                .total(merchantDisputeService.merchantDisputeManageCount(manageBO)).items(list).build();
        
        return pageResult;
    }
  • DisputeMerchantManageBO manageBO = DisputeMerchantManageBO.convert(request):将DisputeMerchantManageRequest对象 request 转换为 DisputeMerchantManageBO对象 manageBO
  • List list = merchantDisputeService.merchantDisputeManage(manageBO):调用merchantDisputeService中的merchantDisputeManage方法,传递manageBO作为参数,然后获取一个List类型的结果列表
 PageResult<DisputeMerchantManageResponse> pageResult = PageResult.		        				           <DisputeMerchantManageResponse>builder()
     			.pageNo(Integer.parseInt(request.getPageNo()))
                .pageSize(Integer.parseInt(request.getPageSize()))
                .total(merchantDisputeService.merchantDisputeManageCount(manageBO))
     .items(list).build();
  • PageResult:泛型类封装分页查询结果,包含页面信息、页码、每页条目数、总记录数以及当前页面的数据项列表
  • PageResult.builder():创建了用于构建PageResult对象的构造器;后续代码可以通过该构造器设置分页信息、总记录数和当前页面的数据项列表,最终得到一个完整的PageResult对象
  • .pageNo(Integer.parseInt(request.getPageNo())):设置PageResult对象的当前页码
  • .pageSize(Integer.parseInt(request.getPageSize())):设置PageResult对象的每页条目数
  • .total(merchantDisputeService.merchantDisputeManageCount(manageBO)):设置PageResult对象的总记录数
  • .items(list).build():设置PageResult对象的数据项列表,并完成构建PageResult对象
  • .items(list):获取的数据项列表list设置为PageResult对象的数据项列表属性
  • .build():完成PageResult对象的构建,最终得到一个完整的PageResult对象

Service编写

MerchantDisputeService.java

    public List<DisputeMerchantManageResponse> merchantDisputeManage(DisputeMerchantManageBO disputeMerchantManageBO) {
        Merchant merchant = AuthContextHolder.getLoginMerchant();
        disputeMerchantManageBO.setMerchantNo(merchant.getMerchantNo());
        return merchantDisputeMapper.merchantDisputeManage(disputeMerchantManageBO);
    }
  • Merchant merchant = AuthContextHolder.getLoginMerchant():通过AuthContextHolder获取当前登录的商家(Merchant)对象;AuthContextHolder:用于在当前线程的上下文中存储和管理认证信息
  • disputeMerchantManageBO.setMerchantNo(merchant.getMerchantNo()):获取merchant中的MerchantNo(商户号),并存储在disputeMerchantManageBO中
  • merchantDisputeMapper.merchantDisputeManage(disputeMerchantManageBO):调用Mapper层merchantDisputeManage方法将disputeMerchantManageBO作为参数传递给Mapper

Mapper

    List<DisputeMerchantManageResponse> merchantDisputeManage(DisputeMerchantManageBO disputeMerchantManageBO);

Mapper.xml

  • 通过联表查询,查询merchant_dispute和transaction_order表对应字段,通过对disputeMerchantManageBO字段传入的条件动态生成查询语句,并按照创建时间进行降序排序,最后返回指定分页范围内的结果
    <select id="merchantDisputeManage" resultType="com.moozumi.bean.response.dispute.DisputeMerchantManageResponse">
        select md.id disputeId,md.status disputeStatus,md.type disputeType,o.merchant_no merchantNo,o.unique_id
        uniqueId,md.content disputeContent,
        o.transaction_id transactionId,md.is_reply isReply,md.created_at disputeCreatedAt,o.is_chargeback isChargeback,
        o.billing_email billingEmail,o.create_at paymentCreatedAt,
        o.application_domain,md.order_id
        from merchant_dispute md,transaction_order o
        where md.order_id = o.unique_id
        <if test="merchantNo != null and merchantNo != ''">
            and o.merchant_no = #{merchantNo}
        if>
        <if test="uniqueId != null and uniqueId != ''">
            and o.unique_id = #{uniqueId}
        if>
        <if test="disputeStatus != null">
            and md.status = #{disputeStatus}
        if>
        <if test="disputeType != null">
            and md.type = #{disputeType}
        if>
        <if test="isReply != null">
            and md.is_reply = #{isReply}
        if>
        <if test="isChargeback != null">
            and o.is_chargeback = #{isChargeback}
        if>
        <if test="applicationDomain != null and applicationDomain != ''">
            and o.application_domain = #{applicationDomain}
        if>
        <if test="orderId != null and orderId != ''">
            and md.order_id = #{orderId}
        if>
        order by md.created_at desc
        limit #{limit} offset #{offset}
    select>

BO:对前端传递参数进行接收处理

  • 使用DisputeMerchantManageBO处理request的主要目的是为了在业务逻辑层(Service层)中更好地封装业务逻辑和数据处理
@Data
public class DisputeMerchantManageBO {
    private String merchantNo;
    private String uniqueId;
    private Integer disputeStatus;
    private Integer disputeType;
    private Boolean isReply;
    private Boolean isChargeback;
    private Integer offset;
    private Integer limit;
    private String orderId;
    private String applicationDomain;

    public void setOffset(String pageNo, String pageSize) {
        this.offset = (Integer.parseInt(pageNo) - 1) * Integer.parseInt(pageSize);
    }

    public void setLimit(String pageSize) {
        this.limit = Integer.parseInt(pageSize);
    }

    //DisputeMerchantManageRequest 转换成 BO
    public static DisputeMerchantManageBO convert(DisputeMerchantManageRequest disputeMerchantManageRequest) {
        DisputeMerchantManageBO disputeMerchantManageBO = new DisputeMerchantManageBO();
        disputeMerchantManageBO.setMerchantNo(AuthContextHolder.getLoginMerchant().getMerchantNo());
        disputeMerchantManageBO.setUniqueId(disputeMerchantManageRequest.getUniqueId());
        disputeMerchantManageBO.setDisputeStatus(disputeMerchantManageRequest.getDisputeStatus());
        disputeMerchantManageBO.setDisputeType(disputeMerchantManageRequest.getDisputeType());
        disputeMerchantManageBO.setIsReply(disputeMerchantManageRequest.getIsReply());
        disputeMerchantManageBO.setIsChargeback(disputeMerchantManageRequest.getIsChargeback());
        disputeMerchantManageBO.setOffset(disputeMerchantManageRequest.getPageNo(), disputeMerchantManageRequest.getPageSize());
        disputeMerchantManageBO.setLimit(disputeMerchantManageRequest.getPageSize());
        disputeMerchantManageBO.setOrderId(disputeMerchantManageRequest.getOrderId());
        disputeMerchantManageBO.setApplicationDomain(disputeMerchantManageRequest.getApplicationDomain());
        return disputeMerchantManageBO;
    }
}

request:请求类,前端向后端请求的字段

DisputeMerchantManageRequest.java

DisputeMerchantManageRequest:字段对应搜索框需要搜索的字段

@Data
public class DisputeMerchantManageRequest extends PageQuery {
    private String merchantNo;
    private String uniqueId;
    private Integer disputeStatus;
    private Integer disputeType;
    private Boolean isReply;
    private Boolean isChargeback;
    private String orderId;
    private String applicationDomain;

}

response:响应类,后端响应给前端的字段

DisputeMerchantManageResponse:后端通过DisputeMerchantManageRequest字段查询数据库返回查询出的数据

@Data
public class DisputeMerchantManageResponse {
    private Long disputeId;
    private Integer disputeStatus;
    private Integer disputeType;
    private String disputeContent;
    private String merchantNo;
    private String uniqueId;
    private String transactionId;
    private Boolean isReply;
    private Boolean isChargeback;
    private String billingEmail;
    private Date disputeCreatedAt;
    private Date paymentCreatedAt;
    private String orderId;
    private String applicationDomain;
}

PageResult类

@Data
@SuperBuilder
@NoArgsConstructor
@AllArgsConstructor
public class PageResult<T> implements Serializable {

    @ApiModelProperty("总记录数")
    private Integer total;
    private Integer pageNo;
    private Integer pageSize;
    private Integer totalPage;

    @ApiModelProperty("列表数据")
    @Builder.Default
    private List<T> items = Collections.emptyList();


    public static <T> PageResult<T> toPage(IPage<T> page) {
        PageResult<T> result = new PageResult<>();
        result.setItems(page.getRecords());
        result.setTotal((int) page.getTotal());
        result.setPageNo((int) page.getCurrent());
        result.setPageSize((int) page.getSize());
        result.setTotalPage(page.getTotal() % page.getSize() == 0 ?
                (int) (page.getTotal() / page.getSize()) : (int) (page.getTotal() / page.getSize() + 1));
        return result;
    }

    public static <T> PageResult<T> toPage(IPage<?> page, List<T> list) {
        PageResult<T> result = new PageResult<>();
        result.setItems(list);
        result.setTotal((int) page.getTotal());
        result.setPageNo((int) page.getCurrent());
        result.setPageSize((int) page.getSize());
        result.setTotalPage(page.getTotal() % page.getSize() == 0 ?
                (int) (page.getTotal() / page.getSize()) : (int) (page.getTotal() / page.getSize() + 1));
        return result;
    }

    public static <T> PageResult<T> toPage(PageResult<?> res, List<T> list) {
        PageResult<T> result = new PageResult<>();
        BeanUtils.copyProperties(res, result);
        result.setItems(list);
        return result;
    }
}

postman测试

Java 多输入框查询需求实现_第1张图片

Java 多输入框查询需求实现_第2张图片

Java 多输入框查询需求实现_第3张图片


结语:创作不易,如果觉得博主的文章赏心悦目,还请——点赞收藏⭐️评论


你可能感兴趣的:(java,开发语言)