mybatis测试动态SQL时出现There is no getter for property named ... in 'class java.lang.String'

本文详细解析了在MyBatis中使用动态SQL时遇到的findUsersByName方法错误,并提供了两种有效的解决方案。一是通过在接口方法上添加@Param注解,二是更改XML文件中if语句的test条件,使用_parameter替代参数名。

出现错误的代码:
想测试的方法:

public List findUsersByName(String name);

在xml文件中编写的代码

<resultMap type="user3" id="userMap">
		<id property="id" column="id" />
		<result property="name" column="name"/>
		<result property="age" column="age"/>
	</resultMap>
	
	<sql id="userCols">
		id, name, age
	</sql>
	<select id="findUsersByName" resultMap="userMap">
		select <include refid="userCols"/> from user
		<where>
			<if test="name != null and name != ''">
				and name like concat(#{name}, '%')
			</if>
		</where>
	</select>

本想借此测试一下mybatis中的动态SQL,结果出现这种错误,先给出两种解决方案

1.在接口中给方法加上一个注解(推荐使用)

public List findUsersByName(@Param(“name”) String name);

2.在if语句的test中把name改成_parameter(仅传入一个类型为String的参数,那么在 xml文件中应该使用_parameter来代替参数名)

<where>
	<if test="_parameter != null and _parameter != ''">
		and name like concat(#{name}, '%')
	</if>
</where>

以上就是我在网上收集的两种解决方法。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值