1. #将传入的数据都当成一个字符串,会对自动传入的数据加一个双引号。如:order by #user_id#,如果传入的值是111,那么解析成sql时的值为order by "111", 如果传入的值是id,则解析成的sql为order by "id".
  
2. $将传入的数据直接显示生成在sql中。如:order by $user_id$,如果传入的值是111,那么解析成sql时的值为order by user_id,  如果传入的值是id,则解析成的sql为order by id.
  
3. #方式能够很大程度防止sql注入。
  
4.$方式无法防止Sql注入。

5.$方式一般用于传入数据库对象,例如传入表名.
  
6.一般能用#的就别用$.

MyBatis排序时使用order by 动态参数时需要注意,用$而不是#

字符串替换
默认情况下,使用#{}格式的语法会导致MyBatis创建预处理语句属性并以它为背景设置安全的值(比如?)。这样做很安全,很迅速也是首选做法,有时你只是想直接在SQL语句中插入一个不改变的字符串。比如,像ORDER BY,你可以这样来使用:
ORDER BY ${columnName}
这里MyBatis不会修改或转义字符串。

重要:接受从用户输出的内容并提供给语句中不变的字符串,这样做是不安全的。这会导致潜在的SQL注入攻击,因此你不应该允许用户输入这些字段,或者通常自行转义并检查。

mybatis本身的说明:

String Substitution

By default, using the #{} syntax will cause MyBatis to generate PreparedStatement properties and set the values safely against the PreparedStatement parameters (e.g. ?). While this is safer, faster and almost always preferred, sometimes you just want to directly inject a string unmodified into the SQL Statement. For example, for ORDER BY, you might use something like this:

ORDER BY ${columnName}
Here MyBatis won't modify or escape the string. NOTE It's not safe to accept input from a user and supply it to a statement unmodified in this way. This leads to potential SQL Injection attacks and therefore you should either disallow user input in these fields, or always perform your own escapes and checks.

从上文可以看出:

1. 使用#{}格式的语法在mybatis中使用Preparement语句来安全的设置值,执行sql类似下面的:

PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1,id);

这样做的好处是:更安全,更迅速,通常也是首选做法。

2. 不过有时你只是想直接在 SQL 语句中插入一个不改变的字符串。比如,像 ORDER BY,你可以这样来使用:

ORDER BY ${columnName}

此时MyBatis 不会修改或转义字符串。

这种方式类似于:

    Statement st = conn.createStatement();

      ResultSet rs = st.executeQuery(sql);

这种方式的缺点是: 以这种方式接受从用户输出的内容并提供给语句中不变的字符串是不安全的,会导致潜在的 SQL 注入攻击,因此要么不允许用户输入这些字段,要么自行转义并检验。

mybatis中#{}和${}的区别的更多相关文章

  1. MyBatis中#{ }和${ }的区别,数据库优化遵循层次和查询方法

    MyBatis中#{ }和${ }的区别详解 1.#将传入的数据当成一个字符串,会对自动传入的数据加一个 双引号. 例如order by #id#,如果传入的值是111,那么解析成sql时变为orde ...

  2. mybatis中的#{}和${}区别

    mybatis中的#{}和${}区别 2017年05月19日 13:59:24 阅读数:16165 1. #将传入的数据都当成一个字符串,会对自动传入的数据加一个双引号.如:order by #use ...

  3. Mybatis笔记八:MyBatis中#{}和${}的区别

    先给大家介绍下MyBatis中#{}和${}的区别,具体介绍如下: 1. $将传入的数据直接显示生成在sql中 2. #方式能够很大程度防止sql注入. 3.$方式无法防止Sql注入. 4.$方式一般 ...

  4. mybatis 中#{}与${}的区别 (面试题)

    MyBatis/Ibatis中#和$的区别 1. #将传入的数据都当成一个字符串,会对自动传入的数据加一个双引号. 如:order by #user_id#,如果传入的值是111,那么解析成sql时的 ...

  5. spring中@param和mybatis中@param使用区别

    spring中@param /** * 查询指定用户和企业关联有没有配置角色 * @param businessId memberId * @return */ int selectRoleCount ...

  6. [入坑系列] Mybatis 中$与#的区别

    1.理解 1 #是将传入的值当做字符串的形式,eg:select id,name,age from student where id =#{id},当前端把id值1,传入到后台的时候,就相当于 sel ...

  7. MyBatis中#{}和${}的区别详解

    首先看一下下面两个sql语句的区别: <select id="selectByNameAndPassword" parameterType="java.util.M ...

  8. spring中@Param和mybatis中@Param使用区别(暂时还没接触)

    1.spring中@Param(org.springframework.data.repository.query.Param) int selectRoleCount(@Param("bu ...

  9. 从Mybatis中#和$的区别到SQL预编译

    #和$的区别 Mybatis中参数传递可以通过#和$设置.它们的区别是什么呢? # Mybatis在解析SQL语句时,sql语句中的参数会被预编译为占位符问号? $ Mybatis在解析SQL语句时, ...

  10. mybatis中#{}和${}的区别及order by的sql注入问题

    mybatis的#{}和${}的区别以及order by注入问题 原文  http://www.cnblogs.com/chyu/p/4389701.html   前言略,直奔主题.. #{}相当于j ...

随机推荐

  1. runtime获取类名,遍历变量,遍历对象,遍历方法

  2. UIControl事件

    1.UIControlEventTouchDown 单点触摸按下事件:用户点触屏幕,或者又有新手指落下的时候. 2.UIControlEventTouchDownRepeat 多点触摸按下事件,点触计 ...

  3. Hint

    select  /*+  first_rows(20)  */  *  from  t  where  id<20 --分页 select  /*+  all_rows  */  *  from ...

  4. 算法-KMP串匹配

    字符串匹配 http://www.cnblogs.com/jingmoxukong/p/4343770.html 模式匹配是数据结构中字符串的一种基本运算,给定一个子串,要求在某个字符串中找出与该子串 ...

  5. 并发调用get请求

    http://zeusami.iteye.com/blog/1172864 package com.alibaba.xteam.web.travel.module.rpc; import java.i ...

  6. notpad++安装python插件

    1.安装python并添加到环境变量 2.在notpad++ 运行工具下点击运行,输入如下命令: cmd /k python "$(FULL_CURRENT_PATH)" & ...

  7. TIJ——Chapter Two:Everything Is an Object

    If we spoke a different language, we would perceive a somewhat different world. Ludwig Wittgenstein( ...

  8. PostgreSQL数据库系统的进程结构

    PostgreSQL数据库系统的主要功能都集中于Postgres程序,其入口是Main模块中的main函数,在初始化数据集簇,启动数据库服务器是,都将从这里开始执行.Main模块主要的工作时确定当前的 ...

  9. URAL 1876 Centipede's Morning(数学)

    A centipede has 40 left feet and 40 right feet. It keeps a left slippers and b right slippers under ...

  10. (第九周)视频发布及git统计报告

    项目名:食物链教学工具 组名:奋斗吧兄弟 组长:黄兴 组员:李俞寰.杜桥.栾骄阳.王东涵 代码地址:HTTPS: https://git.coding.net/li_yuhuan/FoodChain. ...