Grails 查询数据库

标签 grails

我正在尝试使用以下方法查询 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/

相关文章:

grails - 如何动态创建包含数组语法的参数名称

grails - Grails转发参数列表

hibernate - 使用正确语法的HQL联接查询上的无效查询

grails - 多对多乐观锁定失败

Grails:将 Assets 文件夹中的文件渲染到gsp

spring - 如何为每个 url 配置 grails/spring 身份验证方案?

grails - 如何在传递参数时重定向到 Grails 中的 Controller

grails - 添加新作者到书

grails - 尝试读取文件但找不到路径?

spring - grails自定义验证器:强制无消息