简体   繁体   English

Grail 在 ORM / GORM 上使用 GGTS

[英]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.我刚开始将 Grails 与 GGTS(Groovy/Grails 工具套件)一起使用,并尝试在其上测试对象关系映射。

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:我强烈建议您查看Grails 文档,但您可以在服务中使用简单的dataObj.save()方法或更完整的方法,例如:

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.您可以根据需要将其放入 try catch 或评估其响应。

You can find information and tutorials in the official grails documentation .您可以在官方grails 文档中找到信息和教程。 Follow the quick start guide to quickly understand how to use GORM.按照快速入门指南快速了解如何使用 GORM。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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