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
评论(0)