Mybatis中#和$的区别

==>底层用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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值