Grails query not using GORM

后端 未结 2 1001
青春惊慌失措
青春惊慌失措 2020-12-08 22:36

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

相关标签:
2条回答
  • 2020-12-08 23:07

    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 ->
             ...
          }
       }
    }
    
    0 讨论(0)
  • 2020-12-08 23:16

    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 讨论(0)
提交回复
热议问题