老司机学习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 


=======欢迎大家拍砖,小手一抖,多多点赞哟!=======

版权声明:本文为博主原创文章,允许转载,但转载必须标明出处。


猜你喜欢

转载自blog.csdn.net/gaomb_1990/article/details/80638154