2

我有一个域类对象列表:

def books = Books.findAllByTitleLike("%Hobbit%")

现在我想更新此列表中所有书籍中的“可用”属性。通常,我会做

books.each {
   it.available = false;
   it.save()
}

有没有更好的方法来做到这一点?我应该在一笔交易中完成吗?喜欢:

Book.withTransaction { ... }
4

1 回答 1

4

您可以使用 executeUpdate 进行批量更新

def updatedRecords = Book.executeUpdate("update Book set available = 'false' where title like'%Hobbit%' ")

每条评论:它的 hql 和它的类似于 sql。

这是您可以做到的另一种方式(内部查询):

def sql = """update Domain
set $fieldName = '$fieldValue'
where id in (select id from Domain1 where seqId = '$myid')"""
def updatedRecords = Domain.executeUpdate(sql)

或此行中的某些内容:

"... where id in ('123','132','111')

我还没有测试过,但你可能会说

def list= ['123','132','111']
" ... where id in ($list)"
于 2013-04-10T17:37:50.337 回答