MyBatis中paramType中参数为"String"类型时报错异常。

 

现在我们有一个dao层接口方法,

public Class findAllByName(String name);

那么对应Mapper.xml文件

<select id="getAllByName" parameterType="string" resultType="Class">
	select * from class
	    <where> 
    		<if test="name != null">
         		f_name like #{name}"%"
    		</if>
    	</where>
</select>

一般来说,我们这样写对于其他类型来说是没有错的。但是如果传入参数为String的话就会抛出异常:

There is no getter for property named 'name' in 'class java.lang.Sting'

因为MyBatis要求如果参数为String的话,不管接口方法的形参是什么,在Mapper.xml中引用时需要改变为_parameter才能被识别:

<select id="getAllByName" parameterType="string" resultType="Class">
	select * from class
	    <where> 
    		<if test="_parameter != null">
         		f_name like #{_parameter}"%"
    		</if>
    	</where>
</select>

记住:这里的传入参数的两个都要修改为_paramter。 且传入参数有且仅有一个String的参数时才会有这种情况。好比说下面的这种情况你传入的是两个Sring参数,那么也就没有这种情况发生。

<select id="getAllByName" resultType="Class">
	select * from class
	    <where> 
    		<if test="param1 != null">
         		f_name like #{param1}"%"
    		</if>
            <if test="param2 != null">
         		f_teacherName like #{param2}"%"
    		</if>
    	</where>
        
</select>
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值