Sql query for insert in grails

夙愿已清 提交于 2020-01-10 10:17:06

问题


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


回答1:


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




回答2:


You can do this by calling the Hibernate Session.createSQLQuery() method. First you need to get the Hibernate session then use that session to execute SQL. See this link to see how to get a Hibernate session from your grails app. Then see this link for info on using Hibernate to execute SQL.



来源:https://stackoverflow.com/questions/2244811/sql-query-for-insert-in-grails

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!