如果您有一个服务方法save(User user)可以简单地调用user.save(),如果用户说过编辑了自己的密码,那么如何停止使用空值覆盖密码(如果密码为null则无法验证)地址?密码不会发送到更新视图。在过去,我们曾经有过这样的事情:
def update(Map params) {
params.remove("password")
player.properties = params
player.save()
}
现在我们有了这个
interface IUserService {
User get(Serializable id)
List<User> list(Map args)
Long count()
void delete(Serializable id)
User save(User user)
User update(User user)
}
@Service(User)
@Transactional
abstract class UserService implements IUserService {
@Override
User update(User user) {
user.save() // this will overwrite the password, or fail if the password is null.
}
params.remove的新功能是什么?我们如何在更新对象时不更新密码?我想我们可以从数据库中读取当前密码,然后在保存之前将其分配给传入的对象,但这是另一种读取方式。或者我们可以编写一些自定义sql来更新每个字段,但不更新我们要屏蔽的字段?
答案 0 :(得分:1)
您可以使用bindable
约束将属性配置为不参与整体属性绑定。
class User {
String username
String password
static constraints = {
password bindable: false
}
}
现在,数据绑定器将在数据绑定过程中排除password
属性,您可以决定何时/是否更新该属性。每当您想更新密码时,都可以进行类似per.password = params.password
(或类似操作)的操作。