快捷搜索:

在Grails console中执行SQL语句

1.

import groovy.sql.Sql

def dataSource= ctx.getBean('dataSource')

def sql = new Sql(dataSource)

//def ls = sql.rows(sb.toString());

sql.eachRow("select * from role"){

println "Name is ${it.name}"

}

结果:

Name is 111

Name is 222

....

.....

================================================================

2.

import groovy.sql.Sql

def dataSource= ctx.getBean('dataSource')

def sql = new Sql(dataSource)

def sb = new StringBuffer("SELECT a.cardid, trim(a.account), trim(a.sno), trim(a.name), ")

sb

结果:

Name is 男

Name is 女

..........

=====================================================

3.

import groovy.sql.Sql

def dataSource= ctx.getBean('dataSource')

def sql = new Sql(dataSource)

def sb = new StringBuffer("SELECT a.cardid, trim(a.account), trim(a.sno), trim(a.name), ")

sb

结果:

性别是男

性别是女

...........

您可能还会对下面的文章感兴趣: