1.我们的xml中需要将 #{} 改为 ${}
错误写法
<if test="order != null and order != ''">
<choose>
<when test='is_desc != null and is_desc == "1"'>
order by #{order} desc
</when>
</choose>
</if>
正确写法
<if test="order != null and order != ''">
<choose>
<when test='is_desc != null and is_desc == "1"'>
order by ${order} desc
</when>
</choose>
</if>
1、#{}引用的值会进行预编译,编译后显示为“?”,sql的输入只是在编译的时候有用,在预编译完成后,#{}的值后期不会参与sql语句的生成,所以对于sql来说,这是一种安全的方式,有效防止sql的恶意注入;
2、${}引用的值不会进行预编译,直接和sql拼接在一起,这就可以通过一些写法去绕过查表得检索字段,从而发生sql恶意注入的情况。
所以#{}比KaTeX parse error: Expected 'EOF', got '#' at position 11: {}要安全,在能使用#̲{}的地方尽量使用#{},不过…{}才能生效
2.choose when应该有一个 防止报错
错误写法
<if test="order != null and order != ''">
<choose>
<when test='is_desc != null and is_desc == "1"'>
order by ${order} desc
</when>
<otherwise>
order by ${order2}
</otherwise>
</choose>
</if>
正常写法: 中不包含替换符
<if test="order != null and order != ''">
<choose>
<when test='is_desc != null and is_desc == "1"'>
order by ${order} desc
</when>
<otherwise>
order by id
</otherwise>
</choose>
</if>