Java多輸入框查詢需求實現(xiàn)方法詳解
多輸入框查詢
需求分析

任意一個輸入框,輸入內(nèi)容點擊搜索都可以精準(zhǔn)搜索到對應(yīng)的內(nèi)容
代碼實現(xiàn)
Controller接口編寫
@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數(shù)據(jù):BOmerchantDisputeFacade.merchantDisputeManage(request):調(diào)用Service的merchantDisputeManage方法,傳遞接受的參數(shù)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 轉(zhuǎn)換為 DisputeMerchantManageBO對象 manageBO
- List<DisputeMerchantManageResponse> list = merchantDisputeService.merchantDisputeManage(manageBO):調(diào)用merchantDisputeService中的merchantDisputeManage方法,傳遞manageBO作為參數(shù),然后獲取一個List類型的結(jié)果列表
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>:泛型類封裝分頁查詢結(jié)果,包含頁面信息、頁碼、每頁條目數(shù)、總記錄數(shù)以及當(dāng)前頁面的數(shù)據(jù)項列表
- PageResult.<DisputeMerchantManageResponse>builder():創(chuàng)建了用于構(gòu)建PageResult<DisputeMerchantManageResponse>對象的構(gòu)造器;后續(xù)代碼可以通過該構(gòu)造器設(shè)置分頁信息、總記錄數(shù)和當(dāng)前頁面的數(shù)據(jù)項列表,最終得到一個完整的PageResult對象
- .pageNo(Integer.parseInt(request.getPageNo())):設(shè)置PageResult對象的當(dāng)前頁碼
- .pageSize(Integer.parseInt(request.getPageSize())):設(shè)置PageResult對象的每頁條目數(shù)
- .total(merchantDisputeService.merchantDisputeManageCount(manageBO)):設(shè)置PageResult對象的總記錄數(shù)
- .items(list).build():設(shè)置PageResult對象的數(shù)據(jù)項列表,并完成構(gòu)建PageResult對象
- .items(list):獲取的數(shù)據(jù)項列表list設(shè)置為PageResult對象的數(shù)據(jù)項列表屬性
- .build():完成PageResult對象的構(gòu)建,最終得到一個完整的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獲取當(dāng)前登錄的商家(Merchant)對象;AuthContextHolder:用于在當(dāng)前線程的上下文中存儲和管理認(rèn)證信息
- disputeMerchantManageBO.setMerchantNo(merchant.getMerchantNo()):獲取merchant中的MerchantNo(商戶號),并存儲在disputeMerchantManageBO中
- merchantDisputeMapper.merchantDisputeManage(disputeMerchantManageBO):調(diào)用Mapper層merchantDisputeManage方法將disputeMerchantManageBO作為參數(shù)傳遞給Mapper
Mapper
List<DisputeMerchantManageResponse> merchantDisputeManage(DisputeMerchantManageBO disputeMerchantManageBO);
Mapper.xml
通過聯(lián)表查詢,查詢merchant_dispute和transaction_order表對應(yīng)字段,通過對disputeMerchantManageBO字段傳入的條件動態(tài)生成查詢語句,并按照創(chuàng)建時間進(jìn)行降序排序,最后返回指定分頁范圍內(nèi)的結(jié)果
<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:對前端傳遞參數(shù)進(jìn)行接收處理
使用DisputeMerchantManageBO處理request的主要目的是為了在業(yè)務(wù)邏輯層(Service層)中更好地封裝業(yè)務(wù)邏輯和數(shù)據(jù)處理
@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 轉(zhuǎn)換成 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:字段對應(yīng)搜索框需要搜索的字段
@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:響應(yīng)類,后端響應(yīng)給前端的字段
DisputeMerchantManageResponse:后端通過DisputeMerchantManageRequest字段查詢數(shù)據(jù)庫返回查詢出的數(shù)據(jù)
@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("總記錄數(shù)")
private Integer total;
private Integer pageNo;
private Integer pageSize;
private Integer totalPage;
@ApiModelProperty("列表數(shù)據(jù)")
@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測試



總結(jié)
到此這篇關(guān)于Java多輸入框查詢需求實現(xiàn)的文章就介紹到這了,更多相關(guān)Java多輸入框查詢內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot對接Twilio實現(xiàn)發(fā)送驗證碼和驗證短信碼
Twilio是一家提供云通信服務(wù)的公司,旨在幫助開發(fā)者和企業(yè)通過簡單的API實現(xiàn)各種通信功能,下面我們來看看如何對接Twilio實現(xiàn)發(fā)送驗證碼和驗證短信碼吧2025-03-03
SpringBoot2.0 整合 SpringSecurity 框架實現(xiàn)用戶權(quán)限安全管理方法
Spring Security是一個能夠為基于Spring的企業(yè)應(yīng)用系統(tǒng)提供聲明式的安全訪問控制解決方案的安全框架。這篇文章主要介紹了SpringBoot2.0 整合 SpringSecurity 框架,實現(xiàn)用戶權(quán)限安全管理 ,需要的朋友可以參考下2019-07-07

