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

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

问题描述

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

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

我们如何在不使用 HQL 和 gorm 关系的情况下实现这一点.

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

谢谢

推荐答案

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

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/教程+6+-+Groovy+SQL

http://www.ibm.com/developerworks/java/图书馆/j-pg01115.html

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

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