两种取值符号的异同

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

 <select id="queryStudentByStuno" parameterType="int" resultMap="studentMapping">
    select * from student1 where stuno=#{任意值}
</select> <select id="queryStudentByStuno" parameterType="int" resultMap="studentMapping"> select * from student1 where stuno=${value}
</select>

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

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

delete from student1 where stuno=#{value}
delete from student1 where stuno='${value}'
 <select id="queryStudentOrderByColumn" resultType="student">
    select stuno,stuname,stuage from student1 order by ${value} asc
  </select>//排序成功

 <select id="queryStudentOrderByColumn" resultType="student">
    select stuno,stuname,stuage from student1 order by #{value} asc
   </select>//排序无效

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<Student> 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
<Student> students = studentMapper.queryStudentByStuageOrStuname(student);

ii.嵌套类型对象

<select id="queryStudentByaddress" parameterType="student" resultType="student">
    select stuno,stuname,stuage from student1 where homeaddress=#{address.homeAddress} or schooladdress='${address.schoolAddress}'
 </select>



        
Address address=new Address();
address.setHomeAddress("china");
address.setSchoolAddress("x");
        
Student student=new Student();
student.setAddress(address);
List<Student> students = studentMapper.queryStudentByaddress(student);
原文地址:https://www.cnblogs.com/mayouyou/p/13214814.html