在grails中创建多个记录

时间:2011-12-20 13:56:31

标签: grails groovy gsp

任何人都可以告诉我如何在grails中创建多个记录。

此课程在我的域名(OperationLog.groovy)

class OperationLog {

    int x, y
    String text
    Validator validator;
    Date oDate = new Date();
    static optionals = ["oDate" ];

    static belongsTo = [Validator]
}

我只想点击创建按钮创建1000个对象,当我点击 OperationLog List 按钮时,我想看到这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

1 个答案:

答案 0 :(得分:4)

与多次执行任何操作的方式相同,即使用循环或多次执行的闭包,例如:

  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")
  }