简体   繁体   中英

Grail Using GGTS on ORM / GORM

I just start using Grails with GGTS (Groovy/Grails Tool Suite) and I trying to test the Object-Relational Mapping on it.

Does anyone know how it works? To write a code and be able to save it in database!?

Could you give me an example on Code? (Doesn't matter if it's simple)

Thank you

I strongly recommend you checking Grails documentation , but you can use a simple dataObj.save() method or a more complete method in your service, like:

class DataTypeService {
    def saveDataObj(DataType dataObj) {
        if(!dataObj.hasErrors() && dataObj.save(failOnError: true)) return dataObj.id

        return false
    }
}

Then in your controller:

class ExampleController {
    def DataTypeService

    def saving(){
        def dataObj = new DateType(params)
        // ...
        def saved = DataTypeService.saveDataObj(dataObj),

        response = saved ? "object id:${saved} was saved" : 'it fails! try again'

        render response
    }
}

You can put it in a try catch or evaluate its response as you want.

You can find information and tutorials in the official grails documentation . Follow the quick start guide to quickly understand how to use GORM.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM