我正在尝试使用以下方法查询 grails 中的数据库:
def per = User.get(springSecurityService.principal.id)
def query = Post.whereAny {
author { username == per.username }
}.order 'dateCreated', 'desc'
messages = query.list(max: 10)
我的用户位于用户域中,并且位于我的帖子域中:
String message
User author
Date dateCreated
当我运行此查询时,每次 Controller 中用于填充数据库的方法都是空的:
def updateStatus() {
def status = new Post(message: params.message)
System.out.println("test " + params.message)
status.author = User.get(springSecurityService.principal.id)
status.save()
}
我对 grails 中的数据库查询还很陌生,所以如果有人能推荐一些读物,这也会很好。
谢谢
最佳答案
新版本的 spring-security-core 插件向您的 Controller 添加了方法,因此您可以替换:
def per = User.get(springSecurityService.principal.id)
def query = Post.whereAny {
author { username == per.username }
}.order 'dateCreated', 'desc'
messages = query.list(max: 10)
与
messages = Post.findAllByAuthor(principal, [sort: 'dateCreated', order 'desc', max: 10])
没有声明 springSecurityService
您可以将 updateStatus 操作替换为:
def updateStatus(){
def status = new Post(message: params.message, author: principal)
status.save()
}
关于Grails 查询数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12080515/