谁能告诉我如何在grails中创建多个记录?
这个类在我的域中(OperationLog.groovy)
class OperationLog {
int x, y
String text
Validator validator;
Date oDate = new Date();
static optionals = ["oDate" ];
static belongsTo = [Validator]
}
我只想能够点击创建按钮来创建1000个对象,当我点击操作日志列表按钮时,我想看到这1000条记录。
,这段代码属于Controllers (OperationLogController.groovy)
def list = {
params.max = Math.min(params.max ? params.int('max') : 10, 100)
[operationLogInstanceList: OperationLog.list(params), operationLogInstanceTotal: OperationLog.count()]
}
def create = {
def operationLogInstance = new OperationLog()
operationLogInstance.properties = params
operationLogInstance.validator = Validator.get(params.validatorId)
operationLogInstance.operation = Operation.get(params.operationId)
return [operationLogInstance: operationLogInstance]
}
def save = {
def operationLogInstance = new OperationLog(params)
println(params.validator)
operationLogInstance.validator = Validator.get(params.validator.id);
if (operationLogInstance.save(flush: true)) {
flash.message = "${message(code: 'default.created.message', args: [message(code: 'operationLog.label', default: 'OperationLog'), operationLogInstance.id])}"
redirect(action: "show", id: operationLogInstance.id)
}
else {
render(view: "create", model: [operationLogInstance: operationLogInstance])
}
}
这段代码一次只创建一个视图,这就是视图的样子
http://dl.dropbox.com/u/38860715/scr.png与多次执行任何事情相同,即使用多次执行的循环或闭包,例如:
def save = {
1000.times {
def operationLogInstance = new OperationLog(params)
println(params.validator)
operationLogInstance.validator = Validator.get(params.validator.id);
operationLogInstance.save(flush: true)
}
redirect(action: "list")
}