Grails查询不使用GORM [英] Grails query not using GORM
问题描述
我有一个查询,它不会显示以适应在GORM模型中,查询有一个子查询和一个计算字段。我发布了 stackoverflow 已经没有回应,所以我决定采取不同的方法。我想在grails应用程序中查询不使用GORM的东西。是否有一种简单的方法来获取连接并遍历结果集?
在服务或控制器中,您可以添加为$ dataSource
bean提供依赖注入,如果你是受虐狂者,则直接使用groovy.sql.Sql或JDBC。
import groovy.sql.Sql
$ b $ class DataService {
def dataSource
void runQuery(。 ..){
def sql = new Sql(dataSource)
sql.eachRow('select * from foo'){row - >
...
}
}
}
What is the best way to query for something without using GORM in grails?
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?
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屋!