Sql查询在grails中插入 [英] Sql query for insert in grails

查看:92
本文介绍了Sql查询在grails中插入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在grails中执行普通的sql。我需要使用sql查询来在数据库中插入新记录。



我们如何通过使用HQL和gorm关系实现这一点。



感谢

解决方案

groovy.sql.Sql简化了执行JDBC查询的细节。在Grails应用程序中,您将使用带有DataSource的构造函数:
$ b $ pre $ import $ groovy.sql.Sql
。 ..
class FooService {

def dataSource
...
def runSqlQuery(...){
Sql sql = new Sql(dataSource)
sql.executeInsert(insert into ...)
...
}
}

请参阅以下链接以获取使用提示:

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



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


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 .

thanks

解决方案

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

这篇关于Sql查询在grails中插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆