crud in grails code example

Example: simple crud in grails

//Grails Laguage

    //index : list all data of object from domain Object
    def index() {
        def domainName = DomainName.list()
        render domainName as JSON
    }
    
    //show : get data of object from domain Object base on id
    def show() {
        def domainName = DomainName.get(params.id as Long)
        render domainName as JSON
    }

    //create
    def save(){
        def objectJSON = request.JSON
        def domainName = new DomainName()
        domainName.properties = objectJSON
        domainName.save(flush: true)
        render domainName as JSON
    }

    //update base on Id
    def update() {
        def objectJSON = request.JSON
        def domainName = DomainName.get(params.id as Long)
        domainName.properties = objectJSON
        domainName.save(flush: true)
        render domainName as JSON
    }

    //delete
    def  delete(){
        def domainName = DomainName.get(params.id as Long)
        domainName.delete(flush: true)
        render domainName as JSON
    }

Tags:

Java Example