如何使用数组列表创建一个动态在其中查询子句

发布于 2025-01-25 21:56:14 字数 316 浏览 5 评论 0原文

我有一个列表< string>类别和每个类别的类别,我想通过结合和操作员将它们添加到我的Where子句中,例如:



>

由于类别列表的大小正在发生变化,因此我不能做这样的事情:

String sql = "SELECT question_id FROM question WHERE category = ? AND category = ?";
jdbcTemplate.query(sql, stringMapper, "categ1", "categ2");

如何实现自己想要的?

I have a List<String> of categories and for each category, I want to add them to my WHERE clause by combining with AND operator like:
SELECT question_id FROM question WHERE category = categ1 AND category = categ2 AND category = ...

Since the size of the categories list is changing, I cannot do something like this:

String sql = "SELECT question_id FROM question WHERE category = ? AND category = ?";
jdbcTemplate.query(sql, stringMapper, "categ1", "categ2");

How can I achieve what I want?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

没︽人懂的悲伤 2025-02-01 21:56:14

要么检查Spring句柄的JDBC模板是否使用语法为您使用,该语法可能是类似的(来自,我认为确实如此)

SELECT question_id FROM question WHERE category in (?...)

或写出可能出现的问题的查询:

List<Object> parameters = new ArrayList<>(categories.size());
StringBuilder sb = new StringBuilde("SELECT question_id FROM question WHERE 1=1");
if (!categories.isEmpty()) {
  if (categories.size() == 1) {
    sb.append(" and category = ?");
  } else {
    sb.append(" and category in ");
    sb.append(categories.stream()
                        .map(ignored -> "?")
                        .collect(joining(", ", "(", ")")));                      
    sb.append(")"); 
  }
  parameters.addAll(categories);
}
Object[] paramArray = parameters.toArray();
jdbcTemplate.query(sb.toString(), stringMapper, paramArray);

注意:

  1. 某些安全/质量工具可能会报告SQL问题,因为您正在编写动态SQL。
  2. Oracle对每个英寸的1000个元素限制。您必须将每组1000(或更少)分类。
  3. 我以或多或少的方式使用了stream(),以生成“?”。如果您使用 commons-lang3 ,您可以通过”(“ + strignutils.repeat(“?”?'“,”,“,”,categories.size())替换它。 +“)”(Javadoc中的示例可能是使用这种使用进行的)。
  4. 如果您只有类别为单个标准,则可以删除1 = 1以及

Either check if JDBC Template from Spring handle that for you using a syntax which could be something like (from the doc, I don't think it does)

SELECT question_id FROM question WHERE category in (?...)

Or write your own query with the problems that may arise:

List<Object> parameters = new ArrayList<>(categories.size());
StringBuilder sb = new StringBuilde("SELECT question_id FROM question WHERE 1=1");
if (!categories.isEmpty()) {
  if (categories.size() == 1) {
    sb.append(" and category = ?");
  } else {
    sb.append(" and category in ");
    sb.append(categories.stream()
                        .map(ignored -> "?")
                        .collect(joining(", ", "(", ")")));                      
    sb.append(")"); 
  }
  parameters.addAll(categories);
}
Object[] paramArray = parameters.toArray();
jdbcTemplate.query(sb.toString(), stringMapper, paramArray);

Notes:

  1. some security/quality tool may report SQL issues because you are writing a dynamic SQL.
  2. Oracle put a limit on 1000 elements per IN. You would have to partition categories per group of 1000 (or less).
  3. I used a stream() in a more or less strange fashion in order to generate the "?". If you use commons-lang3, you can replace it by "(" + StringUtils.repeat("?", ", ", categories.size()) + ")" (the example in the javadoc was probably done with this kind of use).
  4. if you only have category as single criteria, you may probably remove the 1=1 as well as the and.
能怎样 2025-02-01 21:56:14

我相信这可能对您有用:

// The SQL Query
String sql = "SELECT question_id FROM question";
    
// Create the WHERE clause based on the number of items in List...
StringBuilder whereClause = new StringBuilder(" WHERE ");
StringBuilder ps = new StringBuilder("");
for (int i = 0; i < categories.size(); i++) {
    if (!ps.toString().isEmpty()) {
        ps.append(" AND ");
    }
    ps.append("category = ?");
}
whereClause.append(ps.toString()).append(";");
    
//Append the WHERE clause string to the SQL query string
sql = sql + whereClause.toString();
//System.out.println(sql);
    
/* Convert the categories List to an Object[] Array so as to
   pass in as varArgs to the jdbcTemplate.query() method. */
Object[] psArgs = categories.toArray(new Object[categories.size()]);
jdbcTemplate.query(sql, stringMapper, psArgs);

I believe this may work for you:

// The SQL Query
String sql = "SELECT question_id FROM question";
    
// Create the WHERE clause based on the number of items in List...
StringBuilder whereClause = new StringBuilder(" WHERE ");
StringBuilder ps = new StringBuilder("");
for (int i = 0; i < categories.size(); i++) {
    if (!ps.toString().isEmpty()) {
        ps.append(" AND ");
    }
    ps.append("category = ?");
}
whereClause.append(ps.toString()).append(";");
    
//Append the WHERE clause string to the SQL query string
sql = sql + whereClause.toString();
//System.out.println(sql);
    
/* Convert the categories List to an Object[] Array so as to
   pass in as varArgs to the jdbcTemplate.query() method. */
Object[] psArgs = categories.toArray(new Object[categories.size()]);
jdbcTemplate.query(sql, stringMapper, psArgs);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文