Grails 查询不使用 GORM [英] Grails query not using GORM
问题描述
在 grails 中不使用 GORM 进行查询的最佳方法是什么?
What is the best way to query for something without using GORM in grails?
我有一个似乎不适合 GORM 模型的查询,该查询有一个子查询和一个计算字段.我已经在 stackoverflow 上发帖了,但没有任何回应,所以我决定采取不同的方法.我想在 grails 应用程序中查询不使用 GORM 的内容.有没有一种简单的方法来获取连接并通过结果集?
I have query that doesn't seem to fit in the GORM model, the query has a subquery and a computed field. I posted on stackoverflow already with no response so I decided to take a different approach. I want to query for something not using GORM within a grails application. Is there an easy way to get the connection and go through the result set?
推荐答案
在服务或控制器中,可以为 dataSource
bean 添加依赖注入,直接使用 groovy.sql.Sql 或 JDBC如果你是个受虐狂.
In a service or controller, you can add a dependency injection for the dataSource
bean and use groovy.sql.Sql or JDBC directly if you're a masochist.
import groovy.sql.Sql
class DataService {
def dataSource
void runQuery(...) {
def sql = new Sql(dataSource)
sql.eachRow('select * from foo') { row ->
...
}
}
}
这篇关于Grails 查询不使用 GORM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!