【发布时间】:2014-06-16 16:09:23
【问题描述】:
我对 Grails(和 Groovy)还是有点陌生,所以如果这个问题看起来很愚蠢,我们深表歉意。
我正在尝试访问一个 SQL 数据库,并且似乎可以在 Controller 中使用 SQL 命令(取自 this StackOverflow question):
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
}
}
我知道如果我要创建一个包含一些变量的域类,它会在 SQL 中创建一个数据库表:
package projecttracker2
class ListProject {
String name
String description
Date dueDate
static constraints = {
}
}
但这会创建名为“list_projects”的表。如果我没有这样做,只是在 Grails 之外创建了 SQL 表,并且如果 follow-up question 说您可以断开 Domain 类与数据库的连接,那么 Domain 类的用途是什么?我正在寻找一些 sql 查询来插入、更新、删除等数据,所以我想知道最好的方法是什么。
【问题讨论】:
标签: mysql grails groovy grails-domain-class