grails - Grails/Groovy CRUD服务

标签 grails gorm

嗨,我是一名学生,目前正在学习Grails和Groovy。我想使用服务, Controller 和GSP来提供Crud Service。我将Grails应用程序与PostgreSQL数据库相连。我的问题是我无法进行CRUD服务。我用脚手架做的,然后在 Controller 中手动尝试,现在我想将其放入服务中。我的代码是这样的:

我的服务

import groovy.sql.Sql
import grails.transaction.Transactional

@Transactional
class ContactListService {
    def DataSource

    def listAction(){
        def sql = new Sql(DataSource)
        return sql.rows ("SELECT * FROM  mn")
    }

    def insertAction(){
        def sql = new Sql(DataSource)
        sql.execute("INSERT INTO  mn (id, name) VALUES ($Id,$Name)")
    }

我收到此消息:消息:没有这样的属性:类的ID:contactlist.ContactListService

最佳答案

首先,没有名为DataService的bean(奇怪的名字是btw)。而是有一个名为dataService的bean

2,要解决编译错误,必须在Id方法的上下文中的某个地方声明NameinsertAction()(我的眼睛现在正在流血!)。既可以作为参数,也可以作为字段或局部变量,例如:

def insertAction( String Id, String Name ){ // aaarrrgggghhhh
    def sql = new Sql(DataSource)
    sql.execute("INSERT INTO  mn (id, name) VALUES ($Id,$Name)")
}

关于grails - Grails/Groovy CRUD服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36567123/

相关文章:

exception - 如何在事务中从 Grails 中的数据库错误中恢复

grails - Grails Bootstrap 中的Shiro权限

grails - 在Grails中实现 self 引用的一对多关系的最佳方法是什么?

hibernate - Grails:Author.withTransaction {}和Book.withTransaction {}之间的区别(如果Author和Book均应保存在该事务中)

grails - 具有1:N关系的Grails GORM MissingMethodException

grails - Grails V2.3.7在测试应用程序运行期间会自动启动应用程序:

grails - 将分页选项应用于 Grails 中的一对多列表

grails - Grails GORM HQL按查询分组返回的行数

oracle - Grails-使用VARCHAR2字段作为表标识符-外键关系失败

Grails 服务 : can these be configured to run on a scheduled basis?