SQL/Database Views in Grails

前端 未结 3 907
野趣味
野趣味 2020-12-04 10:14

Does anybody know what is the best approach to accessing a sql view through Grails (or if this is even possible)? It seems an obvious way of doing this would be to use execu

3条回答
  •  被撕碎了的回忆
    2020-12-04 10:54

    You can use plain SQL in Grails which is in the case of accessing a view the preferable way (IMO):

    For example in your controller:

    import groovy.sql.Sql
    
    class MyFancySqlController {
    
        def dataSource // the Spring-Bean "dataSource" is auto-injected
    
        def list = {
            def db = new Sql(dataSource) // Create a new instance of groovy.sql.Sql with the DB of the Grails app
    
            def result = db.rows("SELECT foo, bar FROM my_view") // Perform the query
    
            [ result: result ] // return the results as model
        }
    
    }
    

    and the view part:

    
        
            ${it.foo}
            ${it.bar}
        
    
    

    I hope the source is self-explanatory. The Documentation can be found here

提交回复
热议问题