开发者

Grails query not using GORM

开发者 https://www.devze.com 2023-02-02 18:53 出处:网络
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 fiel开发者_Python百科d.I po

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 fiel开发者_Python百科d. 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 ->
         ...
      }
   }
}


In the moste cases I use criteria queries.

def c = Account.createCriteria()
def results = c {
    between("balance", 500, 1000)
    eq("branch", "London")
    or {
        like("holderFirstName", "Fred%")
        like("holderFirstName", "Barney%")
    }
    maxResults(10)
    order("holderLastName", "desc")
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号