parameterType的传入参数方式可以是两种形式
1.#{}传参:
传参数为单个参数时#{任意值}
传参数为多个参数时#{属性名}(大括号中必须是和实体类相一致的属性名)
自动为传入的参数添加引号:
<select id="queryStudentBystuNameOrstuAge" parameterType="Student" resultMap="studentMap">
select * from student where stuage = #{stuAge} or stuname like '%${stuName}%'
</select>
上边的例子通过姓名或年龄查询学生:
like模糊查询需要的值得形式为 '% 值 %' 因此在传入值之前必须先对值进行处理,,使之成为 %值% 的形式自动添加引号''后成为 '% 值 %' 的形式
where stuage = #{} //需要一个值 #{}将传递过来的参数自动添加了一个引号
上边的例子查询学生并通过传入的值对学生进行排序 : 此处需要的就是属性名 无需引号 要的就是属性名称的值
<select id="queryStudentOrderByColumn" parameterType="String" resultMap="studentMap">
select * from student order by ${value} desc
</select>
${值} 传入的为属性名 就是原样输出,,,原样的值 就相当于值
#{值} 带有引号的值就相当于 '属性名的值'
2.${}传参:
传参数为单个参数时${value}(大括号重必须是value)
传参数为多个参数时${属性名}(大括号中必须是和实体类相一致的属性名)
不为传入的参数添加引号:
<select id="queryStudentBystuNameOrstuAge" parameterType="Student" resultMap="studentMap">
select * from student where stuage = #{stuAge} or stuname like '%${stuName}%'
</select>
上边的例子通过姓名或年龄查询学生
like模糊查询需要的值得形式为 '% 值 %' 传值之前无需处理,,传过来之后将 ${值} 处理为 '%${stuName}%' 的 '% 值 %' 的形式 ${值} 就是存粹的值
where stuage = #{} //需要一个值 ${}不会将传递过来的参数自动添加了引号 因此需要自己手动添加