代码间接放在Github仓库【https://github.com/Damaer/Myb... 】,可间接运行,就不占篇幅了。
[TOC]
1.#{}占位符
1.#{}占位符能够用来设置参数,如果传进来的是根本类型,也就是(string
,long
,double
,int
,boolean
,float
等),那么#{}
外面的变量名能够随便写,什么abc
,xxx
等等,这个名字和传进来的参数名能够不统一。
2.如果传进来的是pojo
类型,那么#{}
中的变量名必须是pojo
的属性名,能够写成属性名
,也能够写属性名.属性名
。
参数是int
,不须要设置parameterType
:
<delete id="deleteStudentById" > delete from student where id=#{XXXdoukeyi}</delete>
parameterType
是pojo
类,如果应用pojo
类型作为参数,那么必须提供get
办法,也就是框架在运行的时候须要通过反射依据#{}
中的名字,拿到这个值放到sql
语句中,如果占位符中的名称和属性不统一,那么报ReflectionException
。
<insert id="insertStudent" parameterType="Student"> insert into student(name,age,score) values(#{name},#{age},#{score})</insert>
3.#{}占位符不能解决的三类问题:
动静表名不能够用#{} :Select * from #{table}
动静列名不能够用#{} : select #{column} from table
动静排序列不能够用#{} : select * from table order by #{column}
留神:不能这样写:
<insert id="insertStudent" parameterType="Student"> insert into student(name,age,score) values(#{Student.name},#{Student.age},#{Student.score})</insert>
否则会报一个谬误(会将Student
当成一个属性),所以咱们类名就间接省略不写就能够了:
### Cause: org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'Student' in 'class bean.Student'
2.${}拼接符
1.如果传进来的是根本类型,也就是(string
,long
,double
,int
,boolean
,float
等),那么#{}
外面的变量名必须写value
。
<delete id="deleteStudentById" > delete from student where id=${value}</delete>
2.如果传进来的是pojo
类型,那么#{}
中的变量名必须是pojo
的属性名,能够写成属性名
,也能够写属性名.属性名
。然而因为是拼接的形式,对于字符串咱们须要本人加引号。
<insert id="insertStudent" parameterType="Student"> insert into student(name,age,score) values('${name}',${age},${score})</insert>
与下面一样,不能将类名写进来:
<!--这是谬误的--><insert id="insertStudent" parameterType="Student"> insert into student(name,age,score) values('${Student.name}',${Student.age},${Student.score})</insert>
3.${}占位符是字符串连接符,能够用来动静设置表名,列名,排序名
动静表名 :Select * from ${table}
动静列名 : select ${column} from table
动静排序 : select * from table order by ${column}
4.${}能够作为连接符应用,然而这样的形式是不平安的,很容易产生sql注入问题,sql注入问题能够参考https://blog.csdn.net/aphysia...:
<select id="selectStudentsByName" resultType="Student"> select id,name,age,score from student where name like '%${value}%'</select>
3.#{}与${}区别
- 1.能应用
#{}
的时候尽量应用#{}
,不应用${}
。- 2.
#{}
相当于jdbc
中的preparedstatement
(预编译),${}
是间接应用外面的值进行拼接,如果解释预编译和间接拼接,我想能够这么了解预编译:比方将一个#{name}
传进来,预编译是先将sql
语句编译成为模板,也就是我晓得你要干什么,假如这个sql
是要查问名字为xxx
的学生信息,那无论这个xxx
外面是什么信息,我都只会去依据名字这一列查问,外面无论写的是什么,都只会当做一个字符串,这个类型在预编译的时候曾经定义好了。- 3.
${}
就不一样,是将语句拼接之后才确定查问条件/类型的,那么就会有被注入的可能性,有些人成心将名字设置为删除条件,这时候sql
就变成删除操作了。- 所以咱们个别相似含糊查问都是用
#{}
拼接
<select id="selectStudentsByName" resultType="Student"> select id,name,age,score from student where name like '%' #{name} '%'</select>
- 然而对于
order by
咱们是用不了#{}
的,因为用了这个就会被主动转换成字符串,主动加引号,这样语句就不失效了。
<select id="selectStudentsByName" resultType="Student"> select id,name,age,score from student order by #{column}</select><!--编译进去的后果如下:-->select * from table order by 'column'
那咱们须要怎么解决呢?咱们只能应用${}
,MyBatis
不会批改或本义字符串。这样是不平安的,会导致潜在的SQL
注入攻打,咱们须要本人限度,不容许用户输出这些字段,或者通常自行本义并查看。所以这必须过滤输出的内容。
【作者简介】:
秦怀,公众号【秦怀杂货店】作者,技术之路不在一时,山高水长,纵使迟缓,驰而不息。这个世界心愿所有都很快,更快,然而我心愿本人能走好每一步,写好每一篇文章,期待和你们一起交换。
此文章仅代表本人(本菜鸟)学习积攒记录,或者学习笔记,如有侵权,请分割作者核实删除。人无完人,文章也一样,文笔稚嫩,在下不才,勿喷,如果有谬误之处,还望指出,感激不尽~