grails中直接使用SQL语句

时间:2014-08-18 18:52:23   收藏:0   阅读:507

    在grails中直接使用SQL语句进行查询,可以直接获取一个新连接,也可以使用默认的数据源

    查询代码:    

      ApplicationContext ctx = (ApplicationContext) ApplicationHolder.getApplication().getMainContext()
      def dataSource = ctx.getBean(‘dataSource‘)
      def sql = new Sql(dataSource)
      String strSql = "select * from company"
      sql.eachRow(strSql) {
          println it
          println it.id
      }
      //需要导包
      import groovy.sql.Sql
      import org.codehaus.groovy.grails.commons.ApplicationHolder
      import org.springframework.context.ApplicationContext

    

grails中直接使用SQL语句,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!