问题
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