天天看點

mybatis測試動态SQL時出現There is no getter for property named ... in 'class java.lang.String'

出現錯誤的代碼:

想測試的方法:

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>
           

以上就是我在網上收集的兩種解決方法。