gpt4 book ai didi

java - COALESCE SQL 与 Speedment

转载 作者:行者123 更新时间:2023-11-30 10:27:19 33 4
gpt4 key购买 nike

评估可能参数的 Hibernate 代码示例:

  String query = "SELECT * FROM instances";
String where = "";
if(userName!=null) {
where+="AND username = '" + userName + "'";
}
if(componentName!=null) {
where+="AND componentname = '" + componentName + "'";
}
if(componentAlias!=null) {
where+="AND componentalias = '" + componentAlias + "'";
}
if(!where.equalsIgnoreCase("")) {
where = where.substring(3);
where = " WHERE " + where;
}
query = query + where;
LOGGER.info("Query: " + query);

Statement s = (Statement) conn.createStatement();
ResultSet rs = s.executeQuery(query);

我如何使用 Speedment ORM 过滤器做到这一点?

最佳答案

在 Speedment 中非常相似。每次添加过滤器时,Stream 接口(interface)都会返回一个新的 Stream。您可以简单地将其存储在一个变量中,并像在您的代码中一样使用 if 语句。

Stream stream = instances.stream();
if (userName != null) {
stream = stream.filter(Instance.USERNAME.equal(userName));
}

if (componentName != null) {
stream = stream.filter(Instance.USERNAME.equal(componentName));
}

if (componentAlias != null) {
stream = stream.filter(Instance.USERNAME.equal(componentAlias));
}

List<Instance> result = stream.collect(toList());

关于java - COALESCE SQL 与 Speedment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45348995/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com