Chr*_*ing 26 sql database grails view
有没有人知道通过Grails访问sql视图的最佳方法是什么(或者如果可能的话)?这样做的一个显而易见的方法是对视图使用executeQuery从视图中选择一组行,我们不会将其视为域对象列表.但是,即使在这种情况下,运行executeQuery的域类也不明显,因为实际上我们只是使用该域类来对完全不相关的实体(视图)运行查询.
是否首选创建表示视图的域类,然后我们可以对该域类使用list()?看起来这会有问题,因为Grails可能希望能够插入,更新,删除和修改任何域类的表模式.
[编辑:
此处跟进问题:Grails Domain Class没有ID字段或部分为NULL复合字段
Sie*_*uer 34
您可以在Grails中使用纯SQL,这是访问视图的最佳方式(IMO):
例如在您的控制器中:
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
}
}
Run Code Online (Sandbox Code Playgroud)
和视图部分:
<g:each in="${result}">
<tr>
<td>${it.foo}</td>
<td>${it.bar}</td>
</tr>
</g:each>
Run Code Online (Sandbox Code Playgroud)
我希望来源不言自明.该文件可以在这里找到
归档时间: |
|
查看次数: |
23589 次 |
最近记录: |