在使用配置mybatis时出现了with invalid types () or values ()错误:
Exception in thread "main" org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: org.apache.ibatis.reflection.ReflectionException: Error instantiating class com.gem.demo.entity.Book with invalid types (int,int,String,String,String,String,float) or values (16,1,null,null,null,null,null). Cause: java.lang.IllegalArgumentException
### The error may exist in sqlmap/bookMapper.xml
### The error may involve com.gem.demo.dao.BookDao.findAll
### The error occurred while handling results
### SQL: select book_id,statu,book_name,author,publish,introduction,price from books
### Cause: org.apache.ibatis.reflection.ReflectionException: Error instantiating class com.gem.demo.entity.Book with invalid types (int,int,String,String,String,String,float) or values (16,1,null,null,null,null,null). Cause: java.lang.IllegalArgumentException
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:149)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:140)
at org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:147)
at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:80)
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:93)
at com.sun.proxy.$Proxy0.findAll(Unknown Source)
at com.gem.demo.client.Client.main(Client.java:17)
Caused by: org.apache.ibatis.reflection.ReflectionException: Error instantiating class com.gem.demo.entity.Book with invalid types (int,int,String,String,String,String,float) or values (16,1,null,null,null,null,null). Cause: java.lang.IllegalArgumentException
at org.apache.ibatis.reflection.factory.DefaultObjectFactory.instantiateClass(DefaultObjectFactory.java:88)
at org.apache.ibatis.reflection.factory.DefaultObjectFactory.create(DefaultObjectFactory.java:53)
at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.createUsingConstructor(DefaultResultSetHandler.java:676)
at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.createByConstructorSignature(DefaultResultSetHandler.java:654)
at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.createResultObject(DefaultResultSetHandler.java:618)
at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.createResultObject(DefaultResultSetHandler.java:591)
at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.getRowValue(DefaultResultSetHandler.java:397)
at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleRowValuesForSimpleResultMap(DefaultResultSetHandler.java:354)
at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleRowValues(DefaultResultSetHandler.java:328)
at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleResultSet(DefaultResultSetHandler.java:301)
at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleResultSets(DefaultResultSetHandler.java:194)
at org.apache.ibatis.executor.statement.PreparedStatementHandler.query(PreparedStatementHandler.java:65)
at org.apache.ibatis.executor.statement.RoutingStatementHandler.query(RoutingStatementHandler.java:79)
at org.apache.ibatis.executor.SimpleExecutor.doQuery(SimpleExecutor.java:63)
at org.apache.ibatis.executor.BaseExecutor.queryFromDatabase(BaseExecutor.java:324)
at org.apache.ibatis.executor.BaseExecutor.query(BaseExecutor.java:156)
at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:109)
at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:83)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:147)
... 6 more
Caused by: java.lang.IllegalArgumentException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.apache.ibatis.reflection.factory.DefaultObjectFactory.instantiateClass(DefaultObjectFactory.java:74)
... 24 more
解决方法:
1.实体类Bean中的get/set方法与成员变量不一导致。
2.在实体类Bean中加入无参构造函数。
实体类中要么就有参无参构造方法都不写,要不就都要写,不能只写有参构造方法
mybatis为什么持久化类时必须提供一个不带参数的默认构造函数?
原因:因为mybatis框架会调用这个默认构造方法来构造实例对象。
即Class类的newInstance方法,这个方法就是通过调用默认构造方法来创建实例对象的 。
另外,如果没有提供任何构造方法,虚拟机会自动提供默认构造方法(无参构造器),但是如果提供了其他有参数的构造方法的话,虚拟机就不再为你提供默认构造方法,这时必须手动把无参构造器写在代码里,否则new xxx()是会报错的,所以默认的构造方法不是必须的,只在有多个构造方法时才是必须的,这里“必须”指的是“必须手动写出来”。
当查询的时候返回的实体类是一个对象实例,是mybatis动态通过反射生成的射的Class.forName(“className”).newInstance();需要对应的类提供一个无参构造函数。