数据库 Mysql ,版本 5.5.17.JDBC 驱动程序版本 5.1.28 使用 Gstring 的代码:
def p = "id"
def row = sql.firstRow("select $p from Model")
print row
结果:
[id:id] **!**
没有 Gstring 的代码:
def row = sql.firstRow("select id from Model")
print row
结果:
[id:1]
为什么?
最佳答案
Groovy 正在转义 p
,因为它将其视为 Sql 参数
尝试使用Sql.expand
像这样:
def p = 'id'
def row = sql.firstRow( "select ${Sql.expand( p )} from Model" )
关于mysql - 使用 Groovy 字符串作为 SQL select 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21576852/