多条SQL语句
代码
// <update></update>中有两条sql语句
<update id="updateDeptRelations">
DELETE FROM sys_dept_relation
WHERE
descendant IN ( SELECT temp.descendant FROM
( SELECT descendant FROM sys_dept_relation WHERE ancestor = #{descendant} ) temp )
AND ancestor IN ( SELECT temp.ancestor FROM ( SELECT ancestor FROM
sys_dept_relation WHERE descendant = #{descendant} AND ancestor != descendant ) temp );
INSERT INTO sys_dept_relation (ancestor,descendant)
(SELECT a.ancestor, b.descendant
FROM sys_dept_relation a
CROSS JOIN sys_dept_relation b
WHERE a.descendant = #{ancestor}
AND b.ancestor = #{descendant})
</update>
错误
异常信息 ex=org.springframework.jdbc.BadSqlGrammarException:
解决:
一开始以为是因为sql语句中是使用
#{descendant}
补充:
使用${param}传递的参数会被当成sql语句的一部分,比如传递表,字段名;#{param}传入的数据都当成一个字符串,会对自动传入的数据加一个双引号
例如:(传入的值为 id)
order by ${param} 解析为:order by id
select * from table where name=#{param} 解析为 select * from table where name=”id”
解决方法:
此处为多条query语句,报错信息指向分号后的第二条语句。
判断可能是连接的datasource默认只能允许执行单个的query语句。因此在连接的datasource的url后面增添参数allowMultiQueries=true,添加时为&allowMultiQueries=true
url: jdbc:mysql://${MYSQL_HOST:XXX.XXX.XXX.XX}:${MYSQL_PORT:3306}/wb_property?characterEncoding=utf8&useSSL=false&allowMultiQueries=true