or语句不知道在模板中这样写有什么问题
SELECT a.id, a.title FROM user a WHERE #if(t0 != null) a.tag like '%#(t0)%' #end OR #if(t1 != null) a.tag like '%#(t1)%' #end order by a.see desc limit 8
提示错误:
Caused by: com.alibaba.druid.sql.parser.ParserException: ERROR. token : OR, pos : 49 at com.alibaba.druid.sql.parser.SQLExprParser.primary(SQLExprParser.java:600) at com.alibaba.druid.sql.dialect.mysql.parser.MySqlExprParser.primary(MySqlExprParser.java:172) at com.alibaba.druid.sql.parser.SQLExprParser.expr(SQLExprParser.java:85) at com.alibaba.druid.sql.parser.SQLSelectParser.expr(SQLSelectParser.java:540) at com.alibaba.druid.sql.parser.SQLSelectParser.parseWhere(SQLSelectParser.java:260) at com.alibaba.druid.sql.dialect.mysql.parser.MySqlSelectParser.query(MySqlSelectParser.java:159) at com.alibaba.druid.sql.parser.SQLSelectParser.select(SQLSelectParser.java:59) at com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser.parseSelect(MySqlStatementParser.java:199) at com.alibaba.druid.sql.parser.SQLStatementParser.parseStatementList(SQLStatementParser.java:114) at com.alibaba.druid.sql.parser.SQLStatementParser.parseStatementList(SQLStatementParser.java:79) at com.alibaba.druid.wall.WallProvider.checkInternal(WallProvider.java:620) at com.alibaba.druid.wall.WallProvider.check(WallProvider.java:574) at com.alibaba.druid.wall.WallFilter.check(WallFilter.java:712) ... 49 more
谢谢~
#set( flag = false)
SELECT
a.id,
a.title
FROM
user a
WHERE
#if(t0 != null)
a.tag like '%#(t0)%'
#set(flag = true)
#end
#if(t1 != null)
#(flag ? "OR", "")
a.tag like '%#(t1)%'
#end
order by a.see desc
limit 8
也就是说用一个变量 flag 来表达前一个 #if 是否已经进入过了,才决定后面的 #if 中是否要生成一个 "OR" 出来
其实单步调试看看 getSqlPara 出来得到的 sql 值是什么就能知道问题出在哪里