我正在使用Grails Audit Logging插件,该插件使用 grails 域类中的更改填充数据库表。
现在,我需要将插件保存的那些数据(行)显示到视图中。
如何在 grails 中使用数据库表(不是域类)中的数据?
我正在使用Grails Audit Logging插件,该插件使用 grails 域类中的更改填充数据库表。
现在,我需要将插件保存的那些数据(行)显示到视图中。
如何在 grails 中使用数据库表(不是域类)中的数据?
如果不知道“使用信息”是什么意思,很难说。但是,如果您想查询该表,使用 Groovy 中的 sql 包非常简单。控制器可能如下所示:
package com.example
import groovy.sql.GroovyRowResult
import groovy.sql.Sql
class MyController {
def dataSource // injected data source from application
def index() {
def db = new Sql(dataSource)
def results = db.rows("SELECT col1, col2, col3 FROM my_table")
render view: 'index', model: results
}
然后,您当然可以在您认为合适的情况下在您的 GSP 中显示结果。
我创建了一个 grails 域,它可以制作与制作相同的数据库表grails-audit-plugin
。
然后我使用这个新创建的域来检索由插件填充的数据库表中的数据。
我做的域如下。
class AuditLog {
String actor
String className
Date dateCreated
String eventName
Date lastUpdated
String newValue
String oldValue
String persistedObjectId
Long persistedObjectVersion
String propertyName
String uri
static constraints = {
dateCreated nullable: false
lastUpdated nullable: false
newValue nullable: true
oldValue nullable: true
persistedObjectVersion nullable: true
}
static mapping = {
version false
}
}