温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

MyBatis中#与$取值有什么区别

发布时间:2021-06-16 14:29:20 来源:亿速云 阅读:210 作者:小新 栏目:大数据

这篇文章给大家分享的是有关MyBatis中#与$取值有什么区别的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

一、前言
动态SQL是MyBatis的主要特性之一,在mapper中定义的参数传到XML中之后,在查询之前 mybatis 会对其进行动态解析。MyBatis 为我们提供了两种支持动态SQL的语法:#{} 以及 ${}。

二、案例
下面我们通过相关案例来演示一下$与#的用法。

UserMapper.xml文件中查询语句最初写法

<select id="findUserByMapParam"  parameterType="com.queen.mybatis.bean.User"  resultType="com.queen.mybatis.bean.User">
    select id, loginId, userName, role, note from t_user where id = #{id} and userName=#{userName}
</select>

测试控制台打印SQL如下:

2017-08-06 18:23:24,390 [main] [com.queen.mybatis.mapper.UserMapper.findUserByMapParam]-[DEBUG] ==>  Preparing: select id, loginId, userName, role, note from t_user where id = ? and userName=?

现在我们修改一下语句,将where id = #{id} 修改成 ${id},如下:

<select id="findUserByMapParam"  parameterType="com.queen.mybatis.bean.User"  resultType="com.queen.mybatis.bean.User">
    select id, loginId, userName, role, note from t_user where id = ${id} and userName=#{userName}
</select>

再次测试,控制台打印SQL如下:

2017-08-06 18:27:58,887 [main] [com.queen.mybatis.mapper.UserMapper.findUserByMapParam]-[DEBUG] ==>  Preparing: select id, loginId, userName, role, note from t_user where id = 1 and userName=?

可以观察到两条SQL语句的不同,下面一条以${}语句获取参数值时,直接将数据取出来拼接到了SQL语句中;而我们用#{}取得值是以占位符的形式出现。
#{}:是以预编译的方式,将参数设置到SQL语句中,跟我们原来学JDBC时一样,能够很大程度防止sql注入
${}:取出来的值直接拼接到SQL语句中,会有安全问题,无法防止Sql注入。

大多数情况下我们取参数的值都是使用的#{},但是有些情况像表名,排序时使用order by 动态参数时需要注意,用$而不是#。

修改UserMapper.xml:

<select id="findUserByMapParam"  parameterType="com.queen.mybatis.bean.User"  resultType="com.queen.mybatis.bean.User">
    select id, loginId, userName, role, note from t_user where id = #{id} and userName=#{userName} order by userName #{orderDesc}
</select>

测试控制台打印如下:

org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''desc'' at line 1
### The error may exist in UserMapper.xml
### The error may involve com.queen.mybatis.mapper.UserMapper.findUserByMapParam-Inline
### The error occurred while setting parameters
### SQL: select id, loginId, userName, role, note from t_user where id = ? and userName=? order by userName ?
### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''desc'' at line 1
    at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:26)

以#{}号方式取值,控制台报错。
那我们修改一下UserMapper.xml,修改成${}取值方式

<select id="findUserByMapParam"  parameterType="com.queen.mybatis.bean.User"  resultType="com.queen.mybatis.bean.User">
    select id, loginId, userName, role, note from t_user where id = #{id} and userName=#{userName} order by userName ${orderDesc}
</select>

再次测试,控制台打印SQL正常:

2017-08-06 19:17:02,331 [main] [com.queen.mybatis.mapper.UserMapper.findUserByMapParam]-[DEBUG] ==>  Preparing: select id, loginId, userName, role, note from t_user where id = ? and userName=? order by userName desc

感谢各位的阅读!关于“MyBatis中#与$取值有什么区别”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI