首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将SQL与Speedment相结合

将SQL与Speedment相结合
EN

Stack Overflow用户
提问于 2017-07-27 19:04:58
回答 1查看 65关注 0票数 2

使用Hibernate计算可能的参数的代码示例:

代码语言:javascript
复制
  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筛选器做到这一点?

EN

回答 1

Stack Overflow用户

发布于 2017-08-05 00:10:33

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

代码语言:javascript
复制
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());
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45348995

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档