==>底层用PreparedStatement对#{xxx}处使用?占位符然后进行参数设置
$ ==>底层用Statement把参数用字符串拼接到sql语句中
1.单个入参时
- 使用 p a r a m e t e r 或 者 { _parameter }或者 parameter或者{ value}取值
<select id="select1" resultType="com.zsq.po.User">
select * from user where id > ${_parameter}
</select>
<select id="select2" resultType="com.zsq.po.User"> select * from user where id > ${value}
</select>
- 使用#{任意字符}取值
select * from user where id > #{乱七八糟字符}
2.多个入参时
- 按照传入顺序使用 0 、 {0}、 0、{1}…或#{0}、#{1}…可取到参数
<select id="selectEmps1" resultType="com.zsq.po.Employee">
select * from employee where id > ${0} and age > ${1} </select>
<select id="selectEmps2" resultType="com.zsq.po.Employee"> select * from employee where id > #{0} and age > #{1}
</select>
- 按照传入顺序使用 p a r a m 1 、 {param1}、 param1、{param2}…或#{param1}、#{param2}…可取到参数
<select id="selectEmps1" resultType="com.zsq.po.Employee">
select * from employee where id > ${param1} and age > ${param2}
</select>
<select id="selectEmps2"resultType="com.zsq.po.Employee">
select * from employee where id > #{param1} and age > #{param2}
</select>
- 使用@Param(value = “xxx”)注解在Mapper接口形参处进行绑定,xml中可以使用#或$直接获取xxx绑定的形参对应的值
public interface EmployeeMapper {
public List<Employee> selectEmps(@Param(value = "id")int id,@Param(value = "age")int age);
}
<select id="selectEmps1" resultType="com.zsq.po.Employee">
select * from employee where id > ${id} and age > ${age}
</select>
<select id="selectEmps2" resultType="com.zsq.po.Employee">
select * from employee where id > #{id} and age > #{age}
</select>
3.单个入参包含多个值时
- 传入javaBean对象时,直接使用属性名
<select id="selectEmps1" resultType="com.zsq.po.Employee">
select * from employee where id > ${id} and age > ${age}
</select>
<select id="selectEmps2" resultType="com.zsq.po.Employee">
select * from employee where id > #{id} and age > #{age}
</select>
- 传入属性中包含其他对象的javaBean时,属性名再 . 那个对象的属性
<select id="selectPersons" resultType="com.zsq.po.Person">
select * from person where addrId = #{addr.id}
</select>
<select id="selectPersons" resultType="com.zsq.po.Person">
select * from person where addrId = ${addr.id}
</select>
- 传入Map时,直接使用key获取,例如{id=1,age=5}
<select id="selectEmps2" resultType="com.zsq.po.Employee">
select * from employee where id > #{id} and age > #{age}
</select>
- 传入数组时,使用array[index]获取,index是索引下标
<select id="selectEmps" resultType="com.zsq.po.Employee">
select * from employee where id in(#{array[0]},#{array[1]})
</select>
- 传入List时,使用list[index]或collection[index]获取,index是索引下标
<select id="selectEmps1" resultType="com.zsq.po.Employee">
select * from employee where id in(#{list[0]})
</select>
<select id="selectEmps2" resultType="com.zsq.po.Employee">
select * from employee where id in(#{collection[0]})
</select>