Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java NamedParameterJdbcTemplate使用OR运算符设置参数并检查是否为null_Java_Spring_Spring Jdbc_Named Parameters - Fatal编程技术网

Java NamedParameterJdbcTemplate使用OR运算符设置参数并检查是否为null

Java NamedParameterJdbcTemplate使用OR运算符设置参数并检查是否为null,java,spring,spring-jdbc,named-parameters,Java,Spring,Spring Jdbc,Named Parameters,我有以下疑问 class Request{ private string f1; private string f2; private string f3; } Request中的字段可以是null。我需要创建NamedParameterJdbcTemplate查询并检查每个字段的null SELECT * FROM tbl WHERE f1 = 'val' OR f2 = 'val2' OR f3 = 'va3' 这是一个非常糟糕的解决方案 我可以创建类似的东

我有以下疑问

class Request{
    private string f1;
    private string f2;
    private string f3;
}
Request
中的字段可以是
null
。我需要创建
NamedParameterJdbcTemplate
查询并检查每个字段的
null

SELECT * FROM tbl WHERE f1  = 'val' OR f2  = 'val2' OR f3  = 'va3'
这是一个非常糟糕的解决方案

我可以创建类似的东西吗

String query = "SELECT * FROM tbl"; 
if (request.getF1() !=null && !request.getF1().isEmpty()){
            query += ....
        } 
Map namedParameters=new HashMap();

要检查
null
的所有字段,如果不是
null
则添加到映射并设置为查询?并在jdbc模板中设置运算符
,您不能在开箱即用的情况下动态查询生成器(如果值为null,则将其从where节中排除)。您应该创建自己的生成器

如果您需要在一到两个位置执行动态查询,您可以这样做:

只需重构
request.getF1()=空&!request.getF1().isEmpty()
转换为可读和可支持的内容,例如:

 Map<String, Object> namedParameters = new HashMap<>();
并对其进行测试

String select = SqlDynamicWhereSectionWithOrHelper.getInstance().
                applyIfPresentValue(fieldName1 , value1).
                applyIfPresentValue("" , null).
                applyIfPresentValue(fieldName2 , value2)
                .buildSqlWithWhere(GIVEN_SELECT);



public final class SqlDynamicWhereSectionWithOrHelper {
    private StringBuffer whereSqlQuery = new StringBuffer();
    public static final String WHERE = " where ";
    public static final String OR = " or ";
    private boolean whereNotPresent = true;

    private SqlDynamicWhereSectionWithOrHelper() {
    }

    public static SqlDynamicWhereSectionWithOrHelper getInstance() {
        return new SqlDynamicWhereSectionWithOrHelper();
    }

    public SqlDynamicWhereSectionWithOrHelper applyIfPresentValue(String fieldName, String fieldValue) {
        if (isNotEmpty(fieldValue) && isNotEmpty(fieldName)) {
            applyWhereOrOr(fieldName + "=" + wrapValue(fieldValue));
        }
        return this;
    }

    public String buildSqlWithWhere(String mainQueryWithoutWhere) {
        return mainQueryWithoutWhere + whereSqlQuery.toString();
    }

    //private section
    private String wrapValue(String fieldValue) {
        return "'" + fieldValue + "'";
    }

    private void applyWhereOrOr(String predicate) {
        String prefix = OR;
        if (whereNotPresent) {
            whereNotPresent = false;
            prefix = WHERE;
        }
        whereSqlQuery.append(prefix);
        whereSqlQuery.append(predicate);
    }

    //if you have apache common(or some other implementation) use  method from lib StringUtils.isEmpty(String value)
    private boolean isNotEmpty(String value) {
        return value != null && value.trim().length() > 0;
    }

}

您也可以尝试在SQL中通过检查empty来修复它,但是如果您有大量数据要处理,这种技术会降低您的请求速度

 private static final String GIVEN_SELECT = "select * from table";

 @Test
    public void testForWithParamsWithNull(){
        //set-up data
        String fieldName1 = "field1";
        String value1 = "value for field1";

        String fieldName2 = "field2";
        String value2 = "value for field2";

        StringBuffer expectedSelectBuffer = new StringBuffer(GIVEN_SELECT);
        expectedSelectBuffer.append(SqlDynamicWhereSectionWithOrHelper.WHERE);
        expectedSelectBuffer.append(fieldName1+"='"+value1+"'");
        expectedSelectBuffer.append(SqlDynamicWhereSectionWithOrHelper.OR);
        expectedSelectBuffer.append(fieldName2+"='"+value2+"'");
        String expectedSelect = expectedSelectBuffer.toString();

        //execution

        String resultSelect = SqlDynamicWhereSectionWithOrHelper.getInstance().
                applyIfPresentValue(fieldName1 , value1).
                applyIfPresentValue("" , null).
                applyIfPresentValue(fieldName2 , value2)
                .buildSqlWithWhere(GIVEN_SELECT);

        //verification
        assertThat(resultSelect , is(notNullValue()));
        assertThat(resultSelect ,equalTo(expectedSelect));
    }
SELECT * FROM tbl WHERE (f1  = 'val' or f1 = null) OR (f2  = 'val2' or f2 = null ) OR (f3  = 'va3' or f3=null)