Mybatis xml orderby 排序字段问题

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>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

princeAladdin

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值