Ibatis:There is no READABLE property named 'userName' in class 'java.lang.String

ibatis-xx.xml出现如下配置:

 

 

  1. <selectid="selectAllUsers"resultClass="User"parameterClass="java.lang.String">
  2. select * from t_user
  3. where
  4. 1=1
  5. <dynamic>
  6. <isNotEmptyprepend="and"property="userName">
  7. instr(name,#userName#)>0
  8. </isNotEmpty>
  9. </dynamic>
  10. </select>
<select id="selectAllUsers" resultClass="User" parameterClass="java.lang.String">
		select * from t_user
		where
			1=1
		<dynamic>
			<isNotEmpty prepend="and" property="userName">
				  instr(name,#userName#)>0
			</isNotEmpty>
		</dynamic>
</select>

 

 

此时执行此模糊查询,出现如下异常:exception: com.ibatis.common.beans.ProbeException: There is no READABLE property named 'userName' in class 'java.lang.String';

 

对比如下配置:

 

  1. <selectid="selectAllUsers"resultClass="User"parameterClass="User">
  2. select * from t_user
  3. where
  4. 1=1
  5. <dynamic>
  6. <isNotEmptyprepend="and"property="userName">
  7. instr(name,#userName#)>0
  8. </isNotEmpty>
  9. </dynamic>
  10. </select>
<select id="selectAllUsers" resultClass="User" parameterClass="User">
		select * from t_user
		where
			1=1
		<dynamic>
			<isNotEmpty prepend="and" property="userName">
				 instr(name,#userName#)>0
			</isNotEmpty>
		</dynamic>
</select>


上述配置是能正常执行查询的,故猜测ibatis在解析xml成sql时,会寻找parameterClass的getter property方法,第二种case中,userName property正是parameterClass User的属性,故能正常解析为sql,进而执行查询操作,以此类推,第一种case中,parameterClass 是String类,而property是userName,String寻找不到userName的getter方法,故会出现上述红色处的异常信息。

 

 

以上纯属个人臆想,如有深知个中原理的麻烦不吝赐教。

 

注:如果parameterClass确实只想设置为String,而不是一个User对象,那么也可以采用下面的配置(将property省去或设置为value即可)

 

  1. <selectid="selectAllUsers"resultClass="User"parameterClass="java.lang.String">
  2. select * from t_user
  3. where
  4. 1=1
  5. <dynamic>
  6. <isNotEmptyprepend="and"property="value">
  7. instr(name,#userName#)>0
  8. </isNotEmpty>
  9. </dynamic>
  10. </select>

你可能感兴趣的:(property)