org.apache.ibatis.exceptions.PersistenceException: ### Error querying database. Cause: org.apache.

研究了几个月的android 最近这段时间回顾了一下java知识,发下有一些知识已经忘记了,不由自主的感慨一下,要想当一名优秀的程序员是很痛苦的,你必须不断学习新技术来赶上时代的脚步,同时你必须不断温习以前学的技术点来保持的自己优秀的状态,尽管这样很累,但这些努力会已其他方式一点一滴的回报自己,好了,闲话不多说,直接进入主题。

   我在整合SSM框架,测试mybatis就遇到一个小错误,个人认为是一个比较低级的错误,尽管很容易解决,但我决定是分享出来。


这里就贴出关键错误点,在测试mabatis我用到的是junit  

@org.junit.Test
	public void Test(){
		PersonDao personDao =sqlSession.getMapper(PersonDao.class);
		Person persons=personDao.getPersons();
		System.out.println("长度为:"+persons.getPname());
	}
点击运行后,Junit就出错,具体错误如下:

org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: org.apache.ibatis.reflection.ReflectionException: Error instantiating class com.zking.entity.Person with invalid types () or values (). Cause: java.lang.NoSuchMethodException: com.zking.entity.Person.()
### The error may exist in com/zking/entity/Person.xml
### The error may involve defaultParameterMap
### The error occurred while setting parameters
### SQL: select * from person
### Cause: org.apache.ibatis.reflection.ReflectionException: Error instantiating class com.zking.entity.Person with invalid types () or values (). Cause: java.lang.NoSuchMethodException: com.zking.entity.Person.()
	at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:23)
	at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:107)
	at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:98)
	at org.apache.ibatis.session.defaults.DefaultSqlSession.selectOne(DefaultSqlSession.java:62)
	at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:63)
	at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:43)
	at com.sun.proxy.$Proxy4.getPersons(Unknown Source)
	at com.zking.test.TestMyBatis.Test(TestMyBatis.java:38)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
	at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
	at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: org.apache.ibatis.reflection.ReflectionException: Error instantiating class com.zking.entity.Person with invalid types () or values (). Cause: java.lang.NoSuchMethodException: com.zking.entity.Person.()
	at org.apache.ibatis.reflection.factory.DefaultObjectFactory.instantiateClass(DefaultObjectFactory.java:83)
	at org.apache.ibatis.reflection.factory.DefaultObjectFactory.create(DefaultObjectFactory.java:45)
	at org.apache.ibatis.reflection.factory.DefaultObjectFactory.create(DefaultObjectFactory.java:38)
	at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.createResultObject(DefaultResultSetHandler.java:535)
	at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.createResultObject(DefaultResultSetHandler.java:514)
	at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.getRowValue(DefaultResultSetHandler.java:334)
	at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleRowValuesForSimpleResultMap(DefaultResultSetHandler.java:294)
	at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleRowValues(DefaultResultSetHandler.java:269)
	at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleResultSet(DefaultResultSetHandler.java:239)
	at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleResultSets(DefaultResultSetHandler.java:153)
	at org.apache.ibatis.executor.statement.PreparedStatementHandler.query(PreparedStatementHandler.java:57)
	at org.apache.ibatis.executor.statement.RoutingStatementHandler.query(RoutingStatementHandler.java:70)
	at org.apache.ibatis.executor.SimpleExecutor.doQuery(SimpleExecutor.java:57)
	at org.apache.ibatis.executor.BaseExecutor.queryFromDatabase(BaseExecutor.java:259)
	at org.apache.ibatis.executor.BaseExecutor.query(BaseExecutor.java:132)
	at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:105)
	at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:81)
	at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:104)
	... 30 more
Caused by: java.lang.NoSuchMethodException: com.zking.entity.Person.()
	at java.lang.Class.getConstructor0(Unknown Source)
	at java.lang.Class.getDeclaredConstructor(Unknown Source)
	at org.apache.ibatis.reflection.factory.DefaultObjectFactory.instantiateClass(DefaultObjectFactory.java:57)
	... 47 more

控制台的报错如下:

DEBUG [main] - Logging initialized using 'class org.apache.ibatis.logging.log4j.Log4jImpl' adapter.
DEBUG [main] - Class not found: org.jboss.vfs.VFS
DEBUG [main] - JBoss 6 VFS API is not available in this environment.
DEBUG [main] - Class not found: org.jboss.vfs.VirtualFile
DEBUG [main] - VFS implementation org.apache.ibatis.io.JBoss6VFS is not valid in this environment.
DEBUG [main] - Using VFS adapter org.apache.ibatis.io.DefaultVFS
DEBUG [main] - Find JAR URL: file:/E:/wk/SSMandAndroid/target/classes/com/zking/entity
DEBUG [main] - Not a JAR: file:/E:/wk/SSMandAndroid/target/classes/com/zking/entity
DEBUG [main] - Reader entry: Person.class
DEBUG [main] - Reader entry: Person.xml
DEBUG [main] - Listing file:/E:/wk/SSMandAndroid/target/classes/com/zking/entity
DEBUG [main] - Find JAR URL: file:/E:/wk/SSMandAndroid/target/classes/com/zking/entity/Person.class
DEBUG [main] - Not a JAR: file:/E:/wk/SSMandAndroid/target/classes/com/zking/entity/Person.class
DEBUG [main] - Reader entry: 漱壕


接下来分享下解决这个问题的过程,希望对新手有一定的帮助。

首先我筛选控制台的错误信息,想从中找到我想要的具体错误点,


DEBUG [main] - Find JAR URL: file:/E:/wk/SSMandAndroid/target/classes/com/zking/entity
DEBUG [main] - Not a JAR: file:/E:/wk/SSMandAndroid/target/classes/com/zking/entity


DEBUG [main] - Find JAR URL: file:/E:/wk/SSMandAndroid/target/classes/com/zking/entity/Person.class
DEBUG [main] - Not a JAR: file:/E:/wk/SSMandAndroid/target/classes/com/zking/entity/Person.class


就简单的看控制台可以发现,实体包entity里出现了两次,因此实体包是我检查的首个对象,

一般框架最容易出错的地方无非就是忘记加包,配置文件以及映射文件少写东西(写错东西),

我就把配置mybatis框架的流程检查了一下,并没有错误,主要是检查XML文件,随后我又看了Junit上的报错,关键错误显示:



### Error querying database.  Cause: org.apache.ibatis.reflection.ReflectionException: Error instantiating class com.zking.entity.Person with invalid types () or values (). Cause: java.lang.NoSuchMethodException: com.zking.entity.Person.()


意思是实体类person没有初始化,打开实体类检查,发现是自己忘记加无参构造方法了,加上无参构造方法,问题顺利解决。


你可能感兴趣的:(java框架)