避免在Java中构建SQL查询时使用多个if-else。

huangapple go评论73阅读模式
英文:

Avoiding multipe if-else for sql query building java

问题

以下是翻译好的内容:

不确定这个问题是否在论坛上有过回答。问题是如何避免在Java代码中使用多个if-else语句,根据接收到的输入实现渐进式查询构建。

以下是示例:

在UI上有一个搜索表单,必须针对该表单运行查询。UI表单具有多个参数。例如:如果要搜索员工,可以使用姓名(通配符)、薪资组、办公地点等进行搜索。用户可以以任何组合填写此搜索表单。

后端SQL查询涉及多个连接表,例如:员工、薪资组、办公室等。

数据使用JSON传输到服务器代码,类似于以下内容:

    "filterRequest"
     {
         "employeeName": String,
         "salaryGroup": String,
         "offices": String
     }

上面只是一个示例,在实际情况中,我有大约10个可用于搜索的参数。问题是,我目前正在编写多个if else语句来检查是否传递了这些参数,并根据需要构建我的SQL查询,类似于以下内容:

     if(filterRequest.getEmployeeName()!=null){
     sqlQuery.append("and emp.employeeName like (:employeeName)");
     }
     if(filterRequest.getSalaryGroup()!=null){
     sqlQuery.append("and sal.getSalaryGroup like (:salaryGroup)");
     }

     return sqlQuery;
英文:

Not sure if this has been ever answered on the forum. Question is how to avoid multiple if-else statements in a java code for progressive query building based on input received.

Example below:

I have a search form on the UI against which a query has to be run. The UI form has multiple parameters. For example: if searching for a employee/employees, one can search using Name (wild card), salary group, office location etc. User can fill this search form in any combination.

The back end sql query has multiple join tables example: employee, salarygroups, offices etc

The data is being transferred to the server code using an json.. something like

    "filterRequest"
     {
         "employeeName": String,
         "salaryGroup": String,
         "offices": String
     }

Above is just an example and in actuality I have about 10 parameters that can be used for search.
The problem is I am at present writing multiple if else statements to check if these parameters are being passed and building my SQL query accordingly something like

     if(filterRequest.getEmployeeName()!=null){
     sqlQuery.append("and emp.employeeName like (:employeeName)");
     }
     if(filterRequest.getSalaryGroup()!=null){
     sqlQuery.append("and sal.getSalaryGroup like (:salaryGroup)");
     }


     return sqlQuery;

答案1

得分: 1

你可以使用一种方法来检查并添加sqlPart。例如像这样:

addSqlPart(sqlQuery, filterRequest, FilterRequest::getEmployeeName, "and emp.employeeName like (:employeeName)");
addSqlPart(sqlQuery, filterRequest, FilterRequest::getSalaryGroup, "and emp.salaryGroup like (:salaryGroup)");

检查给定的getter方法是否具有值,如果是,则添加sqlPart的方法:

private static void addSqlPart(StringBuffer sqlQuery, FilterRequest filterRequest, Function<FilterRequest, Object> getter, String sqlPart) {
    Object value = getter.apply(filterRequest);
    if (value != null) {
        sqlQuery.append(sqlPart);
    }
}
英文:

You could use a method to check and add the sqlPart. For example like this:

addSqlPart(sqlQuery, filterRequest, FilterRequest::getEmployeeName, &quot;and emp.employeeName like (:employeeName)&quot;);
addSqlPart(sqlQuery, filterRequest, FilterRequest::getSalaryGroup, &quot;and emp.salaryGroup like (:salaryGroup)&quot;);

Method which checks if the given getter has a value and adds the sqlPart if so

    private static void addSqlPart(StringBuffer sqlQuery, FilterRequest filterRequest, Function&lt;FilterRequest, Object&gt; getter, String sqlPart) {
        Object value = getter.apply(filterRequest);
        if (value != null) {
            sqlQuery.append(sqlPart);
        }
    }

huangapple
  • 本文由 发表于 2020年9月7日 15:56:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/63773471.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定