mybatis入门学习(四)-#{}与${}取值符号的区别及详细概括

#{}与${}取值符号的区别

示例一:
如下所示,若传入参数 id=1 则:
select * from person where id = #{id} 传入参数后可以看成 select * from person where id =‘1’;
select * from person where id = ${value} 传入参数后可以看成 select * from person where id = 1
所以
#{id}: 所带的参数自动加上单引号

${id}: 所带的参数无单引号

<select id="selectPersonById1" resultType="person" parameterType="int">
        select * from person where id = #{id}
</select>
<select id="selectPersonById2" resultType="person" parameterType="int">
        select * from person where id = ${value}
</select>

示例二:
根据 #{}与 $ {} 的区别,当条件为 like 条件时:
对于 #{name} ,假设参数为 ,那么在参入该参数时应为 mapper.selectPersonAll(“%王%”)
对于 %${value}% ,传入参数无需加入%,直接传入 mapper.selectPersonAll(“王”)

<select id="selectPersonById1" resultType="person" parameterType="int">
        select * from person where name like #{name}
</select>
<select id="selectPersonById2" resultType="person" parameterType="int">
        select * from person where name= '%${value}%'
</select>

#{}与${}取值符号的取值

1、当 parameterType 的类型为基本类型与 String

#{任意值}:大括号中的值可以为任意值
${value}:大括号中的值只能为 value

2、当 parameterType 的类型为对象类型时

#{属性值}:大括号中的值必须为该对象包含的属性
${属性值}:大括号中的值必须为该对象包含的属性
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值