• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Mybatis xml orderby 排序字段问题

武飞扬头像
myprincess003
帮助1

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>

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgcakea
系列文章
更多 icon
同类精品
更多 icon
继续加载