最近,把各应用的jdbc连接池统一从dbcp2改成了druid,运行时druid报sql解析错误,如下:
select * from test
where 1=1
<if test="info1!=null and info1!=''">
and info1 like "%"#{info1}"%"
</if>
23:28:59.112 [25591043@qtp-33385450-2] ERROR c.a.druid.filter.stat.StatFilter - merge sql error, dbType mysql, sql :
select * from test
where 1=1
and info1 like "%"?"%" limit 0,10
com.alibaba.druid.sql.parser.ParserException: syntax error, QUES %, pos 80
at com.alibaba.druid.sql.parser.SQLStatementParser.parseStatementList(SQLStatementParser.java:237) ~[druid-0.2.21.jar:0.2.21]
at com.alibaba.druid.sql.parser.SQLStatementParser.parseStatementList(SQLStatementParser.java:76) ~[druid-0.2.21.jar:0.2.21]
at com.alibaba.druid.sql.visitor.ParameterizedOutputVisitorUtils.parameterize(ParameterizedOutputVisitorUtils.java:42) ~[druid-0.2.21.jar:0.2.21]
at com.alibaba.druid.filter.stat.StatFilter.mergeSql(StatFilter.java:145) [druid-0.2.21.jar:0.2.21]
at com.alibaba.druid.filter.stat.StatFilter.createSqlStat(StatFilter.java:629) [druid-0.2.21.jar:0.2.21]
at com.alibaba.druid.filter.stat.StatFilter.statementPrepareAfter(StatFilter.java:305) [druid-0.2.21.jar:0.2.21]
at com.alibaba.druid.filter.FilterEventAdapter.connection_prepareStatement(FilterEventAdapter.java:124) [druid-0.2.21.jar:0.2.21]
at com.alibaba.druid.filter.FilterChainImpl.connection_prepareStatement(FilterChainImpl.java:446) [druid-0.2.21.jar:0.2.21]
at com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl.prepareStatement(ConnectionProxyImpl.java:342) [druid-0.2.21.jar:0.2.21]
而之前dbcp2是不报错的,经查,可以通过下列方式解决:
'%${ info1}%' 或者CONCAT(CONCAT('%',#{info1}), '%'),而不要使用 "%"#{info1}"%"。