Sql query for insert in grails

前端 未结 2 1357
陌清茗
陌清茗 2020-12-30 08:32

How to execute plain sql in grails . I need to use sql query for inserting new record in database .

How can we achieve this with out using HQL and gorm relations .

2条回答
  •  悲&欢浪女
    2020-12-30 08:49

    groovy.sql.Sql simplifies the details of doing JDBC queries. In a Grails app you'd use the constructor that takes a DataSource:

    import groovy.sql.Sql
    ...
    class FooService {
    
       def dataSource
       ...
       def runSqlQuery(...) {
          Sql sql = new Sql(dataSource)
          sql.executeInsert("insert into ...")
          ...
       }
    }
    

    See these links for usage tips:

    http://docs.codehaus.org/display/GROOVY/Tutorial+6+-+Groovy+SQL

    http://www.ibm.com/developerworks/java/library/j-pg01115.html

提交回复
热议问题