Java 多输入框查询需求实现

💗wei_shuo的个人主页

💫wei_shuo的学习社区

🌐Hello World !


多输入框查询

需求分析

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

代码实现

Controller接口编写

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

MerchantDisputeFacade.java

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

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
java 复制代码
    List<DisputeMerchantManageResponse> merchantDisputeManage(DisputeMerchantManageBO disputeMerchantManageBO);

Mapper.xml

  • 通过联表查询,查询merchant_dispute和transaction_order表对应字段,通过对disputeMerchantManageBO字段传入的条件动态生成查询语句,并按照创建时间进行降序排序,最后返回指定分页范围内的结果
xml 复制代码
    <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层)中更好地封装业务逻辑和数据处理
java 复制代码
@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:字段对应搜索框需要搜索的字段

java 复制代码
@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字段查询数据库返回查询出的数据

java 复制代码
@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类

java 复制代码
@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测试




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


相关推荐
武子康14 分钟前
大数据-258 离线数仓 - Griffin架构 配置安装 Livy 架构设计 解压配置 Hadoop Hive
java·大数据·数据仓库·hive·hadoop·架构
若亦_Royi15 分钟前
C++ 的大括号的用法合集
开发语言·c++
资源补给站1 小时前
大恒相机开发(2)—Python软触发调用采集图像
开发语言·python·数码相机
豪宇刘1 小时前
MyBatis的面试题以及详细解答二
java·servlet·tomcat
秋恬意1 小时前
Mybatis能执行一对一、一对多的关联查询吗?都有哪些实现方式,以及它们之间的区别
java·数据库·mybatis
m0_748247551 小时前
Web 应用项目开发全流程解析与实战经验分享
开发语言·前端·php
6.942 小时前
Scala学习记录 递归调用 练习
开发语言·学习·scala
FF在路上2 小时前
Knife4j调试实体类传参扁平化模式修改:default-flat-param-object: true
java·开发语言
真的很上进2 小时前
如何借助 Babel+TS+ESLint 构建现代 JS 工程环境?
java·前端·javascript·css·react.js·vue·html
众拾达人3 小时前
Android自动化测试实战 Java篇 主流工具 框架 脚本
android·java·开发语言