java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Java多输入框查询

Java多输入框查询需求实现方法详解

作者:wei_shuo

这篇文章主要给大家介绍了Java多输入框查询需求实现的相关资料,文中通过代码以及图文介绍的非常详细,对大家学习或者使用Java具有一定的参考借鉴价值,需要的朋友可以参考下

多输入框查询

需求分析

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

代码实现

Controller接口编写

 @PostMapping("merchant/manage")
 public Result<PageResult<DisputeMerchantManageResponse>>
 merchantDisputeManage(@RequestBody DisputeMerchantManageRequest request) {
     return Result.succ(merchantDisputeFacade.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;
    }
 PageResult<DisputeMerchantManageResponse> pageResult = PageResult.		        				           <DisputeMerchantManageResponse>builder()
     			.pageNo(Integer.parseInt(request.getPageNo()))
                .pageSize(Integer.parseInt(request.getPageSize()))
                .total(merchantDisputeService.merchantDisputeManageCount(manageBO))
     .items(list).build();

Service编写

MerchantDisputeService.java

    public List<DisputeMerchantManageResponse> merchantDisputeManage(DisputeMerchantManageBO disputeMerchantManageBO) {
        Merchant merchant = AuthContextHolder.getLoginMerchant();
        disputeMerchantManageBO.setMerchantNo(merchant.getMerchantNo());
        return merchantDisputeMapper.merchantDisputeManage(disputeMerchantManageBO);
    }

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多输入框查询需求实现的文章就介绍到这了,更多相关Java多输入框查询内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

阅读全文