grails - 使用Groovy/Grails将结果集存储在单个数组中

标签 grails groovy

如何使用Groovy将以下查询的结果存储在同一数组中?

select id from employee where employee_id=100
select id from employee where employee_id=200
select id from employee where employee_id=300

例:
def calculate(employee_ids)
{
def result
Sql sql = new Sql(dataSource)

for (employee_id in employee_ids) {    
result = sql.rows("select id from employee where employee_id="+employee_id)
}

sql.close()

return result
}

现在,当我将100、200、300作为'employee_id'传递时,'calculate()'函数仅返回300。 (“employee_id”为数组形式)

将100、200、300等作为'employee_id'传递到单个数组(如[100,200,300])时,如何获得合计结果?

提前致谢!

最佳答案

您尚未真正提供足够的信息。尚不清楚您如何将这些查询发送到数据库,最终可能是相关的。如果您确实需要单独发送它们,则可以将结果与类似的内容组合在一起...

def firstResult = // send query to db...
def secondResult = // send query to db...
def thirdResult = // send query to db...
def combinedResults = firstResult + secondResult + thirdResult

但是也许您真正想要的是执行类似这样的查询...
select id from employee where employee_id in (100, 200, 300)

编辑:

你可以做这样的事情...
def result = []

for (employee_id in employee_ids) {
    result += sql.rows("select id from employee where employee_id=$employee_id").id
}

关于grails - 使用Groovy/Grails将结果集存储在单个数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31930944/

相关文章:

grails - 将关联添加到SpringSecurity User类

java - Gradle 'exclude' 本地 `fileTree` 来自子项目的传递依赖

Java/Groovy double for 语句问题

jenkins - 如何调试 "Problems occurs on injecting env vars as a build wrap: null"

grails - 如何增加每页的默认行数?

尝试运行 Grails 时出现 Java 异常

grails - 在Grails的GORM中添加更改监听器

grails - 从表单到sendMails的调用方法

android-studio - Android Studio 3 不在 Spock 中运行测试

gradle - 什么是Gradle中的文件分类器?