MyBatis/Ibatis中#和$的区别

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.一般能用#的就别用$.

ps:在使用mybatis中还遇到<![CDATA[]]>的用法,在该符号内的语句,将不会被当成字符串来处理,而是直接当成sql语句,比如要执行一个存储过程。

补充:

比如写:name like '%${name}%' 可以执行

    name like '%#{name}%' 执行会报错 

org.apache.ibatis.exceptions.PersistenceException:
### Error querying database.  Cause: java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0).
### The error may exist in cn/itcast/dao/UserMapper.xml
### The error may involve cn.itcast.dao.UserMapper.findUser-Inline
### The error occurred while setting parameters
### SQL: select * from user       WHERE name like '%?%'
### Cause: java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0).
Caused by: java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0).

#{}表示一个占位符号,#{}接收输入参数,类型可以是简单类型,pojo、hashmap。

如果接收简单类型,#{}中可以写成value或其它名称。

#{}接收pojo对象值,通过OGNL读取对象中的属性值,通过属性.属性.属性...的方式获取对象属性值。

${}表示一个拼接符号,会引用sql注入,所以不建议使用${}。

${}接收输入参数,类型可以是简单类型,pojo、hashmap。

如果接收简单类型,${}中只能写成value。

${}接收pojo对象值,通过OGNL读取对象中的属性值,通过属性.属性.属性...的方式获取对象属性值。

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. spring中@param和mybatis中@param使用区别

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. 【JNI】C分支

    public class MainActivity extends Activity { static{ System.loadLibrary("hello"); } @Overr ...

  2. 错误详情:CL : fatal error C1033: cannot open program database ''

    在网上找了好多方法都不行,最后用下面方法解决了. 修改VS目录下的\VC\bin\cl.exe 的兼容性设置,取消兼容模式和以管理员身份运行即可.

  3. VSPM虚拟串口使用

    (1)打开虚拟串口工具,当你设置好你程序中的串口信息后,打开程序中的串口,然后虚拟串口中所显示的就是程序的所提供的串口信息 (2)选中其中一个串口,修改管理信息,点击”重新连接“ , 直接在管理那里, ...

  4. CentoS 下安装gitlab

    curl https://raw.github.com/mattias-ohlsson/gitlab-installer/master/gitlab-install-el6.sh | bash 报错 ...

  5. CentOS 实现自动登陆

    1. 在ssh-client一边使用ssh-keygen生成一对rsa key $ssh-keygen -t rsa 2. 在ssh-client一边使用ssh-add将刚生成的private key ...

  6. android BroadcastReceiver ACTION_TIME_TICK 系统时间监听不到

    android BroadcastReceiver ACTION_TIME_TICK 系统时间监听不到 今天做android上的消息推送,启动了一个独立service,然后在里面监听系统的ACTION ...

  7. SVN提交提示:working copy is not up-to-date解决方法

    解决方法: 在相应文件上,单击选择team,然后选择先更新,然后再提交.这样就好了.

  8. Solve Error Debug Assertion Failed Expression vector iterators incompatible Using PCL in Release Mode of VS2010

    When using PCL 1.4.0 in the release mode building under VS2010, we might sometime get the error &quo ...

  9. ThinkPHP几个配置文件的位置

    1.常用的ThinkPHP\Conf\convention.php 2.ThinkPHP/Lib/Behavior/ParseTemplateBehavior.class.php模板引擎相关配置

  10. HTML&CSS----练习(运算符)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...