在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
原文地址:http://6367966.blog.51cto.com/6357966/1541633