MyBatis Error:org.apache.ibatis.executor.ExecutorException: No constructor found in pojo
2017-12-15 00:00
639 查看
摘要: 当创建的pojo中有枚举类型时发生的错误。
错误描述:
学习MyBatis的枚举类型转换器org.apache.ibatis.type.EnumOrdinalTypeHandler时,需要再pojo中添加枚举类型的属性,也就是说构造函数和建表的属性并不匹配,需要类型转换器来处理。为了插入数据方便,所以创建了如下构造函数:
插入的时候没有问题,但是在查询的时候出现了如下问题:
解决方法:
根据问题描述,我先把自定义的构造方法注释掉,运行查询,正常返回结果。于是我取消注释,又Student类中添加了空参构造方法。再次运行,数据库返回了正确的结果。
问题分析?:
因为MyBatis运行中需要调用构造方法,而Student类中没有默认构造方法,而有参构造方法又因为枚举类而不符合数据库返回的结果对应的字段的类型(此时typeHandler还没工作),所以才发生了Cause: org.apache.ibatis.executor.ExecutorException: No constructor found in pojo.Student matching [java.lang.Integer, java.lang.String, java.lang.String, java.lang.String, java.lang.Integer]异常。
具体原因等我功力再深厚一点时探究。
问题总结:
由于添加了有参构造方法所以编译器不再为Student类中添加无参构造方法,以后创建pojo类要养成添加无惨构造方法的习惯。但是具体问题的原由我没搞懂,我看了一下源码,但是没看懂,如果你知道具体原因请留言,非常感谢~
------------------------分割线------------------------2017.12.18
最近又学习了MyBatis映射器的一个新知识那就是<resultMap>中的<constructor>元素,<constructor>元素用于配置构造方法。pojo中可能没有无参构造函数,那就会产生上文中的问题,这时可以使用<constructor>进行配置。
现在我的Student类中没有无参构造函数,
<resultMap>中也没有配置<constructor>元素
运行一个简单的查询后出现异常
取消注释后得到了正确的结果
<constructor>元素映射了pojo中的
,解决pojo中没有无参构造函数会出现异常的问题,不过如果养成写无参构造函数的习惯后这个问题就显得鸡肋了。
<idArg>是主键,且允许多个主键,<arg>则是pojo的属性,column对应的是表中的字段,javaType对应的是pojo属性的类型,要注意的是<constructor>中的元素顺序要与构造函数的参数列表相同。否则会有NoSuchMethodException如下图:
---------------------------------------------------
很高兴通过学习能完善从前的知识,然后就是坐在我旁边学英语的妹纸好可爱,这是第二次遇见她,希望还能遇见她!
错误描述:
学习MyBatis的枚举类型转换器org.apache.ibatis.type.EnumOrdinalTypeHandler时,需要再pojo中添加枚举类型的属性,也就是说构造函数和建表的属性并不匹配,需要类型转换器来处理。为了插入数据方便,所以创建了如下构造函数:
插入的时候没有问题,但是在查询的时候出现了如下问题:
解决方法:
根据问题描述,我先把自定义的构造方法注释掉,运行查询,正常返回结果。于是我取消注释,又Student类中添加了空参构造方法。再次运行,数据库返回了正确的结果。
问题分析?:
因为MyBatis运行中需要调用构造方法,而Student类中没有默认构造方法,而有参构造方法又因为枚举类而不符合数据库返回的结果对应的字段的类型(此时typeHandler还没工作),所以才发生了Cause: org.apache.ibatis.executor.ExecutorException: No constructor found in pojo.Student matching [java.lang.Integer, java.lang.String, java.lang.String, java.lang.String, java.lang.Integer]异常。
具体原因等我功力再深厚一点时探究。
问题总结:
由于添加了有参构造方法所以编译器不再为Student类中添加无参构造方法,以后创建pojo类要养成添加无惨构造方法的习惯。但是具体问题的原由我没搞懂,我看了一下源码,但是没看懂,如果你知道具体原因请留言,非常感谢~
------------------------分割线------------------------2017.12.18
最近又学习了MyBatis映射器的一个新知识那就是<resultMap>中的<constructor>元素,<constructor>元素用于配置构造方法。pojo中可能没有无参构造函数,那就会产生上文中的问题,这时可以使用<constructor>进行配置。
现在我的Student类中没有无参构造函数,
<resultMap>中也没有配置<constructor>元素
运行一个简单的查询后出现异常
取消注释后得到了正确的结果
<constructor>元素映射了pojo中的
,解决pojo中没有无参构造函数会出现异常的问题,不过如果养成写无参构造函数的习惯后这个问题就显得鸡肋了。
<idArg>是主键,且允许多个主键,<arg>则是pojo的属性,column对应的是表中的字段,javaType对应的是pojo属性的类型,要注意的是<constructor>中的元素顺序要与构造函数的参数列表相同。否则会有NoSuchMethodException如下图:
---------------------------------------------------
很高兴通过学习能完善从前的知识,然后就是坐在我旁边学英语的妹纸好可爱,这是第二次遇见她,希望还能遇见她!
相关文章推荐
- SpringBoot问题之org.apache.ibatis.executor.ExecutorException: No constructor found in...的问题
- org.apache.ibatis.executor.ExecutorException: No constructor found in com.contentsales.meta.User
- 异常org.apache.ibatis.executor.ExecutorException: There was no TypeHandler found for parameter..
- org.apache.ibatis.executor.ExecutorException: A query was run and no Result Maps were found for the
- org.apache.ibatis.executor.ExecutorException: A query was run and no Result Maps were found for the
- org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'xxx' in
- org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'id' in 'cla
- org.apache.ibatis.reflection.ReflectionException:There is no getter for property named 'parentId' in
- org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'userIds' in
- org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'funcSetId' in 'class java.lang.Integer'
- org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'agoTime' in
- org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'id' in 'cla
- org.apache.ibatis.binding.BindingException: Parameter 'nickname' not found. Available parameters are
- org.apache.ibatis.builder.IncompleteElementException : Could not find parameter map com.vialove.ais
- Caused by: org.apache.ibatis.binding.BindingException: Parameter 'stuId' not found. Available parame
- org.apache.ibatis.executor.ExecutorException: Cannot get Configuration as configuration factory was
- Mybatis问题org.apache.ibatis.binding.BindingException: Parameter 'title' not found.
- FAQ(30):org.apache.ibatis.builder.IncompleteElementException: Could not find find result map
- mybatis使用bug:org.apache.ibatis.builder.IncompleteElementException: Could not find parameter map java
- org.apache.ibatis.builder.IncompleteElementException: Could not find result map com.szh.bean.product