博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Mybatis 中#{} 和${}的区别
阅读量:3944 次
发布时间:2019-05-24

本文共 1280 字,大约阅读时间需要 4 分钟。

1.在mybatis中#{}和 KaTeX parse error: Expected 'EOF', got '#' at position 10: {}的区别 (1)#̲{} 占位符 (2){} 拼接符

2.
#{} 为参数占位符 即sql预编译
${} 为 字符串替换,即sql拼接
3.
#{}:动态解析->预编译->执行
${}:动态解析->编译->执行
4.
#{} 的变量替换是在DBMS中
${}的变量替换是在 DBMS外
5
#{}能防止sql注入
${}不能防止sql注入
3、#{} 和 ${} 的实例:假设传入参数为 1

(1)开始

1)#{}:select * from t_user where uid=#{uid}

2) : s e l e c t ∗ f r o m t u s e r w h e r e u i d = ′ {}:select * from t_user where uid= ' selectfromtuserwhereuid={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’

4、#{} 和 ${} 的大括号中的值

(1)单个参数的情形

1)#{}

无MyBatis 默认值,可任意,且与参数名无关

2)${}

<1>使用 MyBatis 默认值 value,即 ${value}

<2>使用自定义参数名,前提:在映射器接口方法的参数前加注解@Param("")

(2)多个参数的情形

1)#{}

<1>使用MyBatis 默认值 arg0、arg1、arg2 … 或 param1、param2、param3 …

<2>使用自定义参数名,前提:在映射器接口方法的参数前加注解@Param("")

2)${}

<1>使用MyBatis 默认值 arg0、arg1、arg2 … 或 param1、param2、param3 …

<2>使用自定义参数名,前提:在映射器接口方法的参数前加注解@Param("")

注:@Param("") 是 @Param(value="") 的简写

5、#{} 和 ${} 在使用中的技巧和建议

(1)不论是单个参数,还是多个参数,一律都建议使用注解@Param("")

(2)能用 #{} 的地方就用 #{},不用或少用 ${}

(3)表名作参数时,必须用 ${}。如:select * from ${tableName}

(4)order by 时,必须用 ${}。如:select * from t_user order by ${columnName}

(5)使用 ${} 时,要注意何时加或不加单引号,即 和 ′ {} 和 ' {}’

转载地址:http://polwi.baihongyu.com/

你可能感兴趣的文章
用python中htmlParser实现的spider(python spider)
查看>>
在线测速网址
查看>>
mysql中GROUP_CONCAT的应用
查看>>
研发人员的绩效考核
查看>>
Python 3 之多线程研究
查看>>
Python 3中的多线程文件下载类
查看>>
Python库之MySQLdb介绍
查看>>
Python3中利用Urllib进行表单数据提交(Get,Post)
查看>>
Python开发之扩展库的安装指南及Suds(Webservice)的使用简介
查看>>
软件项目管理一点分享
查看>>
iphone程序打包ipa格式
查看>>
Ios开发之Apns功能介绍(应用程序通知)及PHP/Python代码
查看>>
iphone开发的几个Apple官方中文教程地址
查看>>
Algorithms: Kruskal's algorithm and Prim's algorithm for Minimum-spanning-tree
查看>>
Algorithm : Dijkstra's algorithm and Bellmon-Ford Paths algorithm
查看>>
Algorithm: k-nearest neighbors and decison boundary(Cross Validation)
查看>>
Algorithm: Principle Component Analysis for High Dimension Reduction Data
查看>>
Naive Bayesian for Text Classification (MLE, Gaussian Naive Bayesian)
查看>>
Algorithm: Decision Tree, Entropy, Information Gain and Continues features
查看>>
FastDFS 架构分析
查看>>