这是我的代码,我需要知道是否有任何方法可以减少程序中的行数。

由于if语句涉及方法调用,因此我不能使用enum来替换它们。

公共类AgreementQueryBuilder {

    @SuppressWarnings(“未选中”)
    公共静态字符串searchQueryBuilder(FetchAgreementsModel AgreementModel,
            @SuppressWarnings(“ rawtypes”)列表obj){

        StringBuilder selectQuery = new StringBuilder(
                “从eglams_agreement e中左* e.rent_increment_method = t.id的外部连接eglams_rentincrementtype t中选择*,e.id作为conceptid,”);

        // if语句检查参数并建立where条件

        如果(agreementsModel.getAgreementId()!= null || AgreementModel.getAgreementNumber()!= null
                || AgreementModel.getStatus()!= null || AgreementModel.getTenantId()!=空
                || (agreementsModel.getFromDate()!=空&& AgreementModel.getToDate()!=空)
                || AgreementModel.getTenderNumber()!= null || AgreementModel.getTinNumber()!=空
                || AgreementModel.getTradeLicense_number()!= null)
            返回null;

        boolean isAllFieldsNull = true;

        如果(agreementsModel.getAgreementId()!= null){
            如果(isAllFieldsNull == true){
                selectQuery.append(“ e.id =?”);
                isAllFieldsNull = false;
                obj.add(agreementsModel.getAgreementId());
            }
        }

        如果(agreementsModel.getAgreementNumber()!= null){
            如果(isAllFieldsNull == true){
                selectQuery.append(“ e.agreement_number =?”);
                isAllFieldsNull = false;
            }其他
                selectQuery.append(“和e.agreement_number =?”);
            obj.add(agreementsModel.getAgreementNumber());
        }

        如果(agreementsModel.getStatus()!= null){

            如果(isAllFieldsNull == true){
                selectQuery.append(“ e.status =?”);
                isAllFieldsNull = false;
            }其他
                selectQuery.append(“和e.status =?”);

            obj.add(agreementsModel.getStatus());
        }

        如果(agreementsModel.getTenantId()!= null){
            如果(isAllFieldsNull == true){
                selectQuery.append(“ e.tenant_id =?”);
                isAllFieldsNull = false;
            }其他
                selectQuery.append(“和e.tenant_id =?”);

            obj.add(agreementsModel.getTenantId());
        }

        如果(agreementsModel.getTenderNumber()!= null){
            如果(isAllFieldsNull == true){
                selectQuery.append(“ e.tender_number =?”);
                isAllFieldsNull = false;
            }其他
                selectQuery.append(“和e.tender_number =?”);
            obj.add(agreementsModel.getTenderNumber());
        }
        如果(agreementsModel.getTinNumber()!= null){
            如果(isAllFieldsNull == true){
                selectQuery.append(“ e.tin_number =?”);
                isAllFieldsNull = false;
            }其他
                selectQuery.append(“和e.tin_number =?”);
            obj.add(agreementsModel.getTinNumber());
        }

        如果(agreementsModel.getTradeLicense_number()!= null){
            如果(isAllFieldsNull == true){
                selectQuery.append(“ e.TradeLicense_number =?”);
                isAllFieldsNull = false;
            }其他
                selectQuery.append(“和e.TradeLicense_number =?”);
            obj.add(agreementsModel.getTradeLicense_number());
        }

        如果(agreementsModel.getFromDate()!= null){
            如果(agreementsModel.getToDate()!= null){
                如果(isAllFieldsNull == true){
                    selectQuery.append(“ t.FromDate =?”);
                    isAllFieldsNull = false;
                }其他
                    selectQuery.append(“和t.FromDate =?”);

                obj.add(agreementsModel.getFromDate());
                selectQuery.append(“和t.ToDate =?”);
                obj.add(agreementsModel.getToDate());
            }
        }
        System.err.println(selectQuery);
        返回selectQuery.toString();
    }
}

最佳答案

查看您的代码:

if (agreementsModel.getAgreementId() != null ||  ... )
    return null;


因此,我们知道,如果getAgreementId() != null,此方法将返回null,并且不会继续进行下去。但是在那之后你写:

if (agreementsModel.getAgreementId() != null) {
        selectQuery.append(" e.id=?");
        isAllFieldsNull = false;
        obj.add(agreementsModel.getAgreementId());
    }
}


我们知道无法输入整个块,因为如果getAgreementId() != null为true,则该方法将已经返回。

因此,对您的问题的简短答案是肯定的,您可以通过删除所有永远不会被调用的不相关的代码块来使此代码更短。

长的答案是,在担心是否可以缩短代码长度之前,应该修复并调试代码以确保它确实完成了预期的工作。我怀疑这意味着可以删除此if,因为它会使以下所有块无效:

if (agreementsModel.getAgreementId() != null || agreementsModel.getAgreementNumber() != null
            || agreementsModel.getStatus() != null || agreementsModel.getTenantId() != null
            || (agreementsModel.getFromDate() != null && agreementsModel.getToDate() != null)
            || agreementsModel.getTenderNumber() != null || agreementsModel.getTinNumber() != null
            || agreementsModel.getTradeLicense_number() != null)
        return null;


但是只有您知道代码应该如何工作。我建议先测试一下,以确保它能正常工作,然后再问如何使其更短。

09-28 07:49