问题描述
四月 16, 2017 12:27:26 上午 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet [springmvc] in context with path [/trip-mall] threw exception [Request processing failed; nested exception is org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'id' in 'class java.lang.String'] with root cause
org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'id' in 'class java.lang.String'
at org.apache.ibatis.reflection.Reflector.getGetInvoker(Reflector.java:409)
at org.apache.ibatis.reflection.MetaClass.getGetInvoker(MetaClass.java:164)
at org.apache.ibatis.reflection.wrapper.BeanWrapper.getBeanProperty(BeanWrapper.java:162)
at org.apache.ibatis.reflection.wrapper.BeanWrapper.get(BeanWrapper.java:49)
at org.apache.ibatis.reflection.MetaObject.getValue(MetaObject.java:122)
at org.apache.ibatis.scripting.xmltags.DynamicContext$ContextMap.get(DynamicContext.java:94)
at org.apache.ibatis.scripting.xmltags.DynamicContext$ContextAccessor.getProperty(DynamicContext.java:108)
at org.apache.ibatis.ognl.OgnlRuntime.getProperty(OgnlRuntime.java:2420)
at org.apache.ibatis.ognl.ASTProperty.getValueBody(ASTProperty.java:114)
at org.apache.ibatis.ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:212)
at org.apache.ibatis.ognl.SimpleNode.getValue(SimpleNode.java:258)
at org.apache.ibatis.ognl.ASTNotEq.getValueBody(ASTNotEq.java:50)
at org.apache.ibatis.ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:212)
at org.apache.ibatis.ognl.SimpleNode.getValue(SimpleNode.java:258)
...
错误代码:
java:
List<Map<String,String>> getUserGo(@Param("id")String id);
xml:
<select id="getUserGo" parameterType="java.lang.String" resultType="java.util.Map">
SELECT cd.NICKNAME as userName,cd.HEAD_IMG as userImg,mwg.DEST_ID as destId from MEM_WANT_GO mwg ,t_mp_client_user cd
where mwg.USER_ID=cd.ID
<if test='id !=null and id !="" '>
and mwg.DEST_ID = #{id}
</if>
<if test='id ==null or id =="" '>
and mwg.DEST_TYPE = 2
</if>
</select>
问题原因
mybatis会默认从String类中调用getXxx(如果传的id就会调用getId)方法,显然String类没有此方法,所以报错。
解决方法
修改xml为:
<select id="getUserGo" parameterType="java.lang.String" resultType="java.util.Map">
SELECT cd.NICKNAME as userName,cd.HEAD_IMG as userImg,mwg.DEST_ID as destId from MEM_WANT_GO mwg ,t_mp_client_user cd
where mwg.USER_ID=cd.ID
<if test="_parameter != null">
and mwg.DEST_ID = #{id}
</if>
<if test="_parameter == null">
and mwg.DEST_TYPE = 2
</if>
</select>
把字段用_parameter
就行了。