在这篇文章中,我们将为您详细介绍nestedexceptionisorg.apache.ibatis.type.TypeException:Couldnotsetparametersformappin
在这篇文章中,我们将为您详细介绍nested exception is org.apache.ibatis.type.TypeException: Could not set parameters for mapping的内容。此外,我们还会涉及一些关于Cause: org.apache.ibatis.reflection.ReflectionException: Could not set property ''orders'' ...、Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias ''GeneratorConfig''. Cause:、Caused by: java.lang.IllegalArgumentException: No enum constant org.apache.ibatis.type.JdbcType.inte、Caused by: org.apache.ibatis.reflection.ReflectionException: There is no getter for property name...的知识,以帮助您更全面地了解这个主题。
本文目录一览:- nested exception is org.apache.ibatis.type.TypeException: Could not set parameters for mapping
- Cause: org.apache.ibatis.reflection.ReflectionException: Could not set property ''orders'' ...
- Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias ''GeneratorConfig''. Cause:
- Caused by: java.lang.IllegalArgumentException: No enum constant org.apache.ibatis.type.JdbcType.inte
- Caused by: org.apache.ibatis.reflection.ReflectionException: There is no getter for property name...
nested exception is org.apache.ibatis.type.TypeException: Could not set parameters for mapping
今天mybatis报了个错误
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.type.TypeException: Could not set parameters for mapping: ParameterMapping{property=''InfoId'', mode=IN, javaType=class java.lang.Long, jdbcType=null, numericScale=null, resultMapId=''null'', jdbcTypeName=''null'', expression=''null''}. Cause: org.apache.ibatis.type.TypeException: Error setting non null for parameter #1 with JdbcType null . Try setting a different JdbcType for this parameter or a different configuration property. Cause: java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long
好像是说转型错误。
把mapper.java
mapper.xml
和实体查了一下。
最后找到原因
mapper.java里入参为int型
XXXDTO getXXXInfoId(int xxxInfoId);
到mybatis的mapper.xml入参定义成了long,导致了上面的错误。
<select id="getXXXInfoId" parameterType="long" resultMap="XXXResultMap">
把long改成int型,问题解决。
<select id="getXXXInfoId" parameterType="int" resultMap="XXXResultMap">
Cause: org.apache.ibatis.reflection.ReflectionException: Could not set property ''orders'' ...
Java代码持久类关联关系
//这里一个用户关联多个订单
public class User implements Serializable {
private int id;
private String username;// 用户姓名
private String sex;// 性别
private Date birthday;// 生日
private String address;// 地址
private List<Orders> orders;//订单
}
mapper.xml配置
<!-- 一对多关联查询 -->
<resultMap type="cn.isme.pojo.User" id="userOrdersResultMap">
<id property="id" column="id"/>
<result property="username" column="username"/>
<result property="sex" column="sex"/>
<result property="birthday" column="birthday"/>
<result property="address" column="address"/>
<!-- <collection property="orders" ofType="cn.isme.pojo.Orders"> --><!--正确-->
<collection property="orders" javaType="cn.isme.pojo.Orders"><!--错误-->
<id column="oid" property="id"/>
<!-- <result column="user_id" property="userId"/> -->
<result column="number" property="number"/>
<result column="user_id" property="userId"/>
<result column="createtime" property="createtime"/>
</collection>
</resultMap>
<select id="findUserAssociateOrders" resultMap="userOrdersResultMap">
SELECT u.*, o.id oid, o.user_id, o.number, o.createtime
FROM USER u, orders o WHERE u.id = o.user_id
</select>
错误描述
org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: org.apache.ibatis.reflection.ReflectionException: Could not set property ''orders'' of ''class cn.isme.pojo.User'' with value ''Orders [id=3, userId=1, number=1000010, createtime=Wed Feb 04 13:22:35 CST 2015, note=null, user=null]'' Cause: java.lang.IllegalArgumentException: argument type mismatch
### The error may exist in cn/isme/mapper/UserMapper.xml
### The error may involve defaultParameterMap
### The error occurred while setting parameters
### SQL: SELECT u.*, o.id oid, o.user_id, o.number, o.createtime FROM USER u, orders o WHERE u.id = o.user_id
### Cause: org.apache.ibatis.reflection.ReflectionException: Could not set property ''orders'' of ''class cn.isme.pojo.User'' with value ''Orders [id=3, userId=1, number=1000010, createtime=Wed Feb 04 13:22:35 CST 2015, note=null, user=null]'' Cause: java.lang.IllegalArgumentException: argument type mismatch
错误原因
在UserMapper.xml中涉及到defaultParameterMap发生错误,在设置参数时,Orders对象通过反射无法注入到User.orders属性当中
解决方法
collection部分定义了用户关联的订单信息。表示关联查询结果集
property="orders":关联查询的结果集存储在User对象的上哪个属性。
javaType="orders":指定pojo中属性类型
ofType="orders":指定的为映射的集合中泛型属性即List<Orders> orders中的泛型Orders
<collection property="orders" javaType="cn.isme.pojo.Orders">
由于上述写的javaType="orders",当给User.orders集合属性注入时,javaType表示User.orders类型为Orders类型,而我们实际为List<Orders>集合类型,所以导致出现类型不匹配的情况,这里我们需要使用ofType="orders"指定orders集合中的泛型
Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias ''GeneratorConfig''. Cause:
Caused by: org.apache.ibatis.builder.BuilderException: Error resolving class. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias ''GeneratorConfig''. Cause: java.lang.ClassNotFoundException: Cannot find class: GeneratorConfig
at org.apache.ibatis.builder.BaseBuilder.resolveClass(BaseBuilder.java:118)
at org.apache.ibatis.builder.xml.XMLMapperBuilder.resultMapElement(XMLMapperBuilder.java:265)
at org.apache.ibatis.builder.xml.XMLMapperBuilder.resultMapElement(XMLMapperBuilder.java:252)
at org.apache.ibatis.builder.xml.XMLMapperBuilder.resultMapElements(XMLMapperBuilder.java:244)
at org.apache.ibatis.builder.xml.XMLMapperBuilder.configurationElement(XMLMapperBuilder.java:116)
... 131 common frames omitted
Caused by: org.apache.ibatis.type.TypeException: Could not resolve type alias ''GeneratorConfig''. Cause: java.lang.ClassNotFoundException: Cannot find class: GeneratorConfig
at org.apache.ibatis.type.TypeAliasRegistry.resolveAlias(TypeAliasRegistry.java:120)
at org.apache.ibatis.builder.BaseBuilder.resolveAlias(BaseBuilder.java:149)
at org.apache.ibatis.builder.BaseBuilder.resolveClass(BaseBuilder.java:116)
... 135 common frames omitted
Caused by: java.lang.ClassNotFoundException: Cannot find class: GeneratorConfig
at org.apache.ibatis.io.ClassLoaderWrapper.classForName(ClassLoaderWrapper.java:200)
at org.apache.ibatis.io.ClassLoaderWrapper.classForName(ClassLoaderWrapper.java:89)
at org.apache.ibatis.io.Resources.classForName(Resources.java:261)
at org.apache.ibatis.type.TypeAliasRegistry.resolveAlias(TypeAliasRegistry.java:116)
... 137 common frames omitted
Caused by: java.lang.IllegalArgumentException: No enum constant org.apache.ibatis.type.JdbcType.inte
Mybatis 没有 JdbcType ="INTEGER" 只能大写,针对这类型
Caused by: java.lang.IllegalArgumentException: No enum constant org.apache.ibatis.type.JdbcType.integer
at java.lang.Enum.valueOf(Enum.java:238)
at org.apache.ibatis.type.JdbcType.valueOf(JdbcType.java:25)
at org.apache.ibatis.builder.BaseBuilder.resolveJdbcType(BaseBuilder.java:71)
... 76 common frames omitted
Disconnected from the target VM, address: ''127.0.0.1:63075'', transport: ''socket''
Caused by: org.apache.ibatis.reflection.ReflectionException: There is no getter for property name...
本文为博主原创,未经允许不得而转载:
异常展示:
dao 层定义的接口为:
public int getClientTotal();
在 mybatis 中的 sql 为:
<select id="getClientTotal" parameterType="String" resultType="Integer">
SELECT COUNT(*) AS oldNum FROM tbl__client_info
<where>
<if test="zoneId!=null and !"".equals(zoneId.trim())">
and zone1Id = #{zoneId}
</if>
</where>
</select>
在运行的时候控制台报一下异常:
Caused by: org.apache.ibatis.reflection.ReflectionException: There is no getter for property named ''zoneId'' in ''class java.lang.String''
at org.apache.ibatis.reflection.Reflector.getGetInvoker(Reflector.java:422) ~[mybatis-3.4.2.jar:3.4.2]
at org.apache.ibatis.reflection.MetaClass.getGetInvoker(MetaClass.java:164) ~[mybatis-3.4.2.jar:3.4.2]
at org.apache.ibatis.reflection.wrapper.BeanWrapper.getBeanProperty(BeanWrapper.java:162) ~[mybatis-3.4.2.jar:3.4.2]
at org.apache.ibatis.reflection.wrapper.BeanWrapper.get(BeanWrapper.java:49) ~[mybatis-3.4.2.jar:3.4.2]
at org.apache.ibatis.reflection.MetaObject.getValue(MetaObject.java:122) ~[mybatis-3.4.2.jar:3.4.2]
at org.apache.ibatis.scripting.xmltags.DynamicContext$ContextMap.get(DynamicContext.java:94) ~[mybatis-3.4.2.jar:3.4.2]
at org.apache.ibatis.scripting.xmltags.DynamicContext$ContextAccessor.getProperty(DynamicContext.java:108) ~[mybatis-3.4.2.jar:3.4.2]
at org.apache.ibatis.ognl.OgnlRuntime.getProperty(OgnlRuntime.java:2666) ~[mybatis-3.4.2.jar:3.4.2]
at org.apache.ibatis.ognl.ASTProperty.getValueBody(ASTProperty.java:114) ~[mybatis-3.4.2.jar:3.4.2]
at org.apache.ibatis.ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:212) ~[mybatis-3.4.2.jar:3.4.2]
at org.apache.ibatis.ognl.SimpleNode.getValue(SimpleNode.java:258) ~[mybatis-3.4.2.jar:3.4.2]
at org.apache.ibatis.ognl.ASTNotEq.getValueBody(ASTNotEq.java:50) ~[mybatis-3.4.2.jar:3.4.2]
at org.apache.ibatis.ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:212) ~[mybatis-3.4.2.jar:3.4.2]
at org.apache.ibatis.ognl.SimpleNode.getValue(SimpleNode.java:258) ~[mybatis-3.4.2.jar:3.4.2]
at org.apache.ibatis.ognl.ASTAnd.getValueBody(ASTAnd.java:61) ~[mybatis-3.4.2.jar:3.4.2]
at org.apache.ibatis.ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:212) ~[mybatis-3.4.2.jar:3.4.2]
at org.apache.ibatis.ognl.SimpleNode.getValue(SimpleNode.java:258) ~[mybatis-3.4.2.jar:3.4.2]
at org.apache.ibatis.ognl.Ognl.getValue(Ognl.java:467) ~[mybatis-3.4.2.jar:3.4.2]
at org.apache.ibatis.ognl.Ognl.getValue(Ognl.java:431) ~[mybatis-3.4.2.jar:3.4.2]
at org.apache.ibatis.scripting.xmltags.OgnlCache.getValue(OgnlCache.java:44) ~[mybatis-3.4.2.jar:3.4.2]
at org.apache.ibatis.scripting.xmltags.ExpressionEvaluator.evaluateBoolean(ExpressionEvaluator.java:32) ~[mybatis-3.4.2.jar:3.4.2]
at org.apache.ibatis.scripting.xmltags.IfSqlNode.apply(IfSqlNode.java:34) ~[mybatis-3.4.2.jar:3.4.2]
at org.apache.ibatis.scripting.xmltags.MixedSqlNode.apply(MixedSqlNode.java:33) ~[mybatis-3.4.2.jar:3.4.2]
at org.apache.ibatis.scripting.xmltags.TrimSqlNode.apply(TrimSqlNode.java:55) ~[mybatis-3.4.2.jar:3.4.2]
at org.apache.ibatis.scripting.xmltags.MixedSqlNode.apply(MixedSqlNode.java:33) ~[mybatis-3.4.2.jar:3.4.2]
at org.apache.ibatis.scripting.xmltags.DynamicSqlSource.getBoundSql(DynamicSqlSource.java:41) ~[mybatis-3.4.2.jar:3.4.2]
at org.apache.ibatis.mapping.MappedStatement.getBoundSql(MappedStatement.java:292) ~[mybatis-3.4.2.jar:3.4.2]
at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:81) ~[mybatis-3.4.2.jar:3.4.2]
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:148) ~[mybatis-3.4.2.jar:3.4.2]
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:141) ~[mybatis-3.4.2.jar:3.4.2]
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectOne(DefaultSqlSession.java:77) ~[mybatis-3.4.2.jar:3.4.2]
at sun.reflect.GeneratedMethodAccessor90.invoke(Unknown Source) ~[na:na]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_121]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_121]
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:433) ~[mybatis-spring-1.3.1.jar:1.3.1]
... 52 common frames omitted
错误分析及解决方法:
错误原因:在于 sql 中 test="zoneId!=null and !"".equals (zoneId.trim ())" 条件判断,在 mybatis 解析 sql 的时候,会将 test 中的属性解析为 bean 的
属性,mybatis 解析获取值的时候通过 getProperty 的方法获取,同理,当 sql 从数据库查到数值,将属性值赋值给属性的时候通过 setProperty 方法。由于
mybatis 将接口中的 String 参数解析为 bean 属性,就需要有 set,get 方法,set,get 方法一般都写在实体类中,接口中的属性可以通过 @Param () 注解来实现
set,get 方法。
修改方法:
public int getClientTotal(@Param("zoneId")String zoneId);
然后重新启动就 ok 了。
今天关于nested exception is org.apache.ibatis.type.TypeException: Could not set parameters for mapping的介绍到此结束,谢谢您的阅读,有关Cause: org.apache.ibatis.reflection.ReflectionException: Could not set property ''orders'' ...、Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias ''GeneratorConfig''. Cause:、Caused by: java.lang.IllegalArgumentException: No enum constant org.apache.ibatis.type.JdbcType.inte、Caused by: org.apache.ibatis.reflection.ReflectionException: There is no getter for property name...等更多相关知识的信息可以在本站进行查询。
本文标签: