一个加号引发的问题

grails中执行sql操作的代码很简单,如下:
def dataSource
def sql = new Sql(dataSource)
def institutionId = 123
def loanFeeSql = "SELECT NVL(SUM(B.LOAN_FEE),0) AS SUM,COUNT(B.ID) AS NUM FROM FUND_ACCOUNT A,LOAN_FEE B,INSTITUTION C "
+"WHERE A.INSTITUTION_ID = C.ID AND C.ID =? AND A.BANK_ACCOUNT = B.REMIT_BANK_ACCOUNT"
def loanFeeList = sql.rows(loanFeeSql,institutionId)
本来不会觉得有任何问题,但执行之后却发生异常:
No signature of method: java.lang.String.positive() is applicable for argument types: () values: []
Possible solutions: notify(), size(), size(), tokenize(), tokenize(). Stacktrace follows:
groovy.lang.MissingMethodException: No signature of method: java.lang.String.positive() is applicable for argument types: () values: []
Possible solutions: notify(), size(), size(), tokenize(), tokenize()
这个异常还真是百思不得其解啊,和java.lang.String.positive()有什么关系呢,百度之,查了好久,又问了我的一个同事,才发现,groovy中因为没有分号作为每一行的分隔符,所以,如果一个字符串需要使用“+”拼起来,则需要把“+”写在上一行的末尾,如果写到下一行的开头,则可能默认下一行是另外一条语句,晕。。。
所以,正确的写法是:
def loanFeeSql = "SELECT NVL(SUM(B.LOAN_FEE),0) AS SUM,COUNT(B.ID) AS NUM FROM FUND_ACCOUNT A,LOAN_FEE B,INSTITUTION C "+
"WHERE A.INSTITUTION_ID = C.ID AND C.ID =? AND A.BANK_ACCOUNT = B.REMIT_BANK_ACCOUNT"
或者使用三个分号,这样编译器会将上下两行自动识别为一个字符串:
def loanFeeSql = """SELECT NVL(SUM(B.LOAN_FEE),0) AS SUM,COUNT(B.ID) AS NUM FROM FUND_ACCOUNT A,LOAN_FEE B,INSTITUTION C
WHERE A.INSTITUTION_ID = C.ID AND C.ID =? AND A.BANK_ACCOUNT = B.REMIT_BANK_ACCOUNT"""
参考资料:http://heqiang421.blog.163.com/blog/static/232275071201472924551504/

你可能感兴趣的:(一个加号引发的问题)