gpt4 book ai didi

Grails 查询数据库

转载 作者:行者123 更新时间:2023-12-02 13:58:00 24 4
gpt4 key购买 nike

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com