0

我有这段代码,非常标准的东西。它采用 java 对象中的一些过滤器,并返回与过滤器匹配的所有记录:

class MarkFilters {
   Integer idFilter;
   Integer userIDFilter;
   Integer claimIDFilter;
}

List<Integer> findMarksMatchingFilters(MarkFilters filters) {
    String sql = "select id from Mark where 1";

    // This has the values to substitute in for the ?'s in the SQL
    List<Object> parameters = new LinkedList<Object>();

    if (filters.idFilter != null) {
        sql += " and id = ?";
        parameters.add(filters.idFilter);
    }

    if (filters.userIDFilter != null) {
        sql += " and userID = ?";
        parameters.add(filters.userIDFilter);
    }

    if (filters.claimIDFilter != null) {
        sql += " and claimID = ?";
        parameters.add(filters.claimIDFilter);
    }

    return executeSQLWithParameters(sql, parameters);
}

我找到了一种应该做同样事情的方法:

List<Integer> findMarksMatchingFilters(MarkFilters filters) {
    return executeSQLWithParameters(
            "select * from Mark " +
            "where (? = NULL or id = ?) " +
            "and (? = NULL or userID = ?) " +
            "and (? = NULL or claimID = ?) ",
        filters.idFilter, filters.idFilter,
        filters.userIDFilter, filters.userIDFilter,
        filters.claimIDFilter, filters.claimIDFilter);
}

例如,如果我输入一个新的 MarkFilters(null, null, 3),则 sql 语句将是:

select * from Mark
where (NULL = NULL or id = NULL)
and (NULL = NULL or userID = NULL)
and (3 = NULL or claimID = 3)

理论上将优化为:

select * from Mark where claimID = 3;

我的具体问题:这种优化真的会发生吗?

我更广泛的问题:这是一个很好的模式,还是有我看不到的缺点?

谢谢!

4

2 回答 2

1

不,表达式的NULL = NULL计算结果为假。

你要:

        "select * from Mark " +
        "where (? is NULL or id = ?) " +
        "and (? is NULL or userID = ?) " +
        "and (? is NULL or claimID = ?) ",

我希望 MySQL 在编译时评估常量表达式。这意味着该where条款在评估之前被简化。但是,我在文档中找不到可以保证这一点的特定参考。

于 2013-04-23T02:38:19.043 回答
0

不完全是一个答案,但我只是找到了我建议的两种方法的一个很好的替代方法:

http://mybatis.github.io/mybatis-3/dynamic-sql.html#chooseWhenOtherwise

于 2013-04-23T02:48:17.303 回答