美文网首页
mybatis #{} ${}的区别

mybatis #{} ${}的区别

作者: 屎倒淋头还嚼便 | 来源:发表于2020-04-14 16:50 被阅读0次

出自:https://blog.csdn.net/siwuxie095/article/details/79190856
1、在MyBatis 的映射配置文件中,动态传递参数有两种方式:
(1)#{} 占位符
(2){} 拼接符 2、#{} 和{} 的区别
(1)
1)#{} 为参数占位符 ?,即sql 预编译
2){} 为字符串替换,即 sql 拼接 (2) 1)#{}:动态解析 -> 预编译 -> 执行 2){}:动态解析 -> 编译 -> 执行
(3)
1)#{} 的变量替换是在DBMS 中
2){} 的变量替换是在 DBMS 外 (4) 1)变量替换后,#{} 对应的变量自动加上单引号 '' 2)变量替换后,{} 对应的变量不会加上单引号 ''
(5)
1)#{} 能防止sql 注入
2)${} 不能防止sql 注入

3、#{} 和 {} 的实例:假设传入参数为 1 (1)开始 1)#{}:select * from t_user where uid=#{uid} 2){}:select * from t_user where uid= '{uid}' (2)然后 1)#{}:select * from t_user where uid= ? 2){}:select * from t_user where uid= '1'
(3)最后
1)#{}:select * from t_user where uid= '1'
2)${}:select * from t_user where uid= '1'

// 博客原文内容没有全部复制

5、#{} 和 {} 在使用中的技巧和建议 (1)不论是单个参数,还是多个参数,一律都建议使用注解@Param("") (2)能用 #{} 的地方就用 #{},不用或少用{}
(3)表名作参数时,必须用 {}。如:select * from{tableName}
(4)order by 时,必须用 {}。如:select * from t_user order by{columnName}
(5)使用 {} 时,要注意何时加或不加单引号,即{} 和 '${}'

相关文章

  • mybatis的xml文件的标签详解

    Mybatis #{}和${}和区别 mybatis获取方法参数 动态SQL

  • MyBatis实用篇之Hello,World

    本文Demo地址 什么是MyBatis? MyBatis和Hibernate的区别? 去哪里找MyBatis? 编...

  • Mybatis

    mybatis 与 hibernate 区别 : mybatis 相比于hibernate 需要关心很多细节 ...

  • Mybatis,hibernate相关

    Mybatis相关 1.Mybatis是什么? 2.为什么选择Mybatis? 3、#{}和${}的区别是什么? ...

  • Mybatis.hibernate相关

    Mybatis相关 1.Mybatis是什么? 2.为什么选择Mybatis? 3、#{}和${}的区别是什么? ...

  • Q&A-06 Mybatis

    Mybatis中#{}与${}的区别 #{}1、#{} 是 sql 的参数占位符,Mybatis 会将 sql 中...

  • Mybatis常见面试题

    Mybatis常见面试题 #{}和${}的区别是什么? #{}和${}的区别是什么? 在Mybatis中,有两种占...

  • mybatis中#与$的区别

    mybatis中#与$的区别 MyBatis中使用parameterType向SQL语句传参,parameterT...

  • MyBatis学习笔记

    Mybatis架构图 图中可能有错误,欢迎评论指正。 1.# Mybatis 中$与#的区别 Mybatis中#是...

  • Mybatis 详解

    [mybatis中"#"和"$"的区别]动态 sql 是 mybatis 的主要特性之一,在 mapper 中定义...

网友评论

      本文标题:mybatis #{} ${}的区别

      本文链接:https://www.haomeiwen.com/subject/dxutvhtx.html