使用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筛选器做到这一点?
发布于 2017-08-05 00:10:33
这在Speedment中非常相似。每次添加过滤器时,Stream接口都会返回一个新的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());https://stackoverflow.com/questions/45348995
复制相似问题