原文网址:MyBatis--#与$的区别_IT利刃出鞘的博客-CSDN博客
简介
本文介绍MyBatis中#与$的区别。
区别概述
项 | #{xxx} | ${xxx) |
编译器行为 | 在预编译期,生成?,作为占位符 | 在预编译期,会直接显示拼接的字符 |
拼接行为 | 如果字段是字符串类型,拼接时会在字符串两侧添加单引号 | 无论是什么类型,拼接时不会在两侧添加单引号 |
使用场景 | 因为会加双引号,所以一般用于字段的值。 | 需要动态参数的时候。 例如:传入数据库对象(例如:表名、列名); ORDER BY; |
情况一:只用 #{}
<select id="getUserByNameAndPsw" resultType="com.hotel3.model.User">
select * from USER where userName=#{userName} and userPassword =#{userPassword};
</select>
结果:
==> Preparing: select * from USER where userName=? and userPassword =?;
==> Parameters: mww(String), 123(String)
<== Total: 1
结论:
#{}会在预编译期,生成两个 ?,作为占位符。
上边只是部分内容,为便于维护,本文已迁移到此地址:MyBatis-#与$的区别 - 自学精灵