两种取值符号的异同

输入参数:parameterType
1.输入为简单类型(八个基本类型+String)
a.#{}  ${}
#{任意值}
${value},其中的标识符只能是value

 


 

b.#{}自动给String类型加上单引号(自动类型转换)

${}原样输出,但是适合于动态排序

delete from student1 where stuno=#{value}
delete from student1 where stuno='${value}'
 //排序成功

 //排序无效

c.#{}可以防止sql注入

${}不可以

 

#{}${]相同之处

a.都可以获得对象的值(嵌套类型对象)

i.获取对象值:

模糊查询方式一:

select stuno,stuname,stuage from student1 where stuage=#{stuAge} or stuname like #{stuName}

 

Student student=new Student();

student.setStuAge(23);
student.setStuName("%m%");
List students = studentMapper.queryStudentByStuageOrStuname(student);

模糊查询方式二:

select stuno,stuname,stuage from student1 where stuage=#{stuAge} or stuname like '%${stuName}%'


Student student=new Student();
student.setStuAge(23);
student.setStuName("%m%");
List
students = studentMapper.queryStudentByStuageOrStuname(student);

ii.嵌套类型对象





        
Address address=new Address();
address.setHomeAddress("china");
address.setSchoolAddress("x");
        
Student student=new Student();
student.setAddress(address);
List students = studentMapper.queryStudentByaddress(student);

 

你可能感兴趣的:(两种取值符号的异同)