Grails 中的重要数据查询
想象一下 grails 中的以下问题,
您有某种带有数字属性的审计跟踪域类。例如,写入 Scrum 项目当前燃尽值的类:
class burndown {
Date createDate
int value
}
每次更新任务时,您的项目都使用此类来存储当前燃尽值 - 这意味着每天多次。
现在您想要绘制一个图表,其中包含每天最后存储的值。
一个 SQL 语句可能看起来像
select
*
from
table
where
id in (
select
max(id)
from
table
group by
TO_CHAR(create_date,'yyyyddmm')
)
现在我的问题:如何在 grails 中执行这样的查询?
如果我必须使用这样的SQL语句,如何避免将表名和列名硬编码在语句中?
PS:此代码尚未经过测试。只是从我的脑海里写下来......但我想你明白我想问的是什么
imagine the following problem in grails
you have some kind of audit trail domain class with numeric properties. For instance a class in which the current burndown value of your scrum project is written:
class burndown {
Date createDate
int value
}
Your projects uses this class to store the current burndown value each time you update a task - this means several times a day.
Now you want to plot a diagram with the last stored value for each day.
An SQL statement for this could look something like
select
*
from
table
where
id in (
select
max(id)
from
table
group by
TO_CHAR(create_date,'yyyyddmm')
)
Now my question: how do you do such a query in grails?
If I have to use such an SQL statement, how to I avoid to put the table and column names hard coded in the statement?
PS: this code hasn't been tested. just written down from my mind... but I guess you feel what I want to ask
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于初学者,您很可能希望将
createDate
重命名为dateCreated
,因为如果您使用该名称,Grails 会自动为您设置它,因此您只需指定类的“真实”属性。不过这对这个问题来说并不重要。如果您想要创建日期之前的最新项目,可以通过几种不同的方式来执行此操作,但我认为这是最有意义的:
或者如果您保留您的姓名,
这在 http://grails.org/doc/latest/ - 这是一本很好的手册,值得一读。
For starters you'll most likely want to rename
createDate
todateCreated
since it's automatically set for you by Grails if you use that name, so you only need to specify values for the 'real' properties of the class. It's not important to this issue though.If you want the most recent item by created date, there are a few different ways you could do this but I think this makes the most sense:
or if you retain your name
This is described at http://grails.org/doc/latest/ - it's a fine manual, worthy of reading.
不是 Grails 用户,因此应由 Grails'ist 相应更正以下内容。
如果域相关,请尝试以下操作:
Not a Grails user, so below should be corrected accordingly by a Grails'ist
If domains are related, try something like: