mybatis中 ${}和#取值小记(Parameter index out of range)


mybatis mapperxml文件中有两种取值法.${}和#{}   

$的是原样,#的是取值并转成指定?#{ele1,jdbcType=VARCHAR}  

有个坑,

错误的写法

<if test="searchName!=null">
<if test="searchName!=''">
and p.name like CONCAT('%','#{searchName,jdbcType=VARCHAR}','%' )
</if>
</if>

正确的写法1

<if test="searchName!=null">
<if test="searchName!=''">
and p.name like CONCAT('%','${searchName}','%' )
</if>
</if>

 

正确的写法2

<if test="searchName!=null">
<if test="searchName!=''">
and p.name like CONCAT('%',#{searchName,jdbcType=VARCHAR},'%' )
</if>
</if>

 #取值时会把值转成'值',这时如果外面又加‘’  也就是 '#{ele,jdbcType=VARCHAR}'    就会变成 ''ele''  2个引号    '${ele}' 是原样 不会自己加引号 所以外面包引号


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM