String interpolation in a multi-line Groovy string

喜你入骨 提交于 2021-01-29 10:31:01

问题


I have a multi-line string in which I dynamically populate an SQL query from a map. I am getting a MissingPropertyException that is a result of the query not recognising the map key or values. Is there a way around this?

def multiString = """
def person = ['John': 'Builder']

person.each{ key, value ->

String query = """ UPDATE person SET value = '${value}' 
                   WHERE name = '${key}' """ } 
 """

回答1:


I figured it out. I needed to escape the $ using a backslash.

def multiString = """
  def person = ['John': 'Builder']

  person.each{ key, value ->

  String query = """ UPDATE person SET value = '\${value}'
                     WHERE name = '\${value}' """ } 
"""


来源:https://stackoverflow.com/questions/50889996/string-interpolation-in-a-multi-line-groovy-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!