Mybatis使用报错org.apache.ibatis.exceptions.PersistenceException:

使用Mybatis中使用Mapper动态代理方式测试报以下错误:

org.apache.ibatis.exceptions.PersistenceException:

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

### The error may exist in mapper\UserMapper.xml

### The error may involve com.dcits.interfacee.userMapper.selectByName-Inline

### The error occurred while setting parameters

### SQL: select *         from user         where username like CONCAT('%',?,'%')

### Cause: org.apache.ibatis.reflection.ReflectionException: Error instantiating class com.dcits.po.User with invalid types () or values (). Cause: java.lang.NoSuchMethodException: com.dcits.po.User.()

  at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:26)

  at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:111)

  at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:102)

  at org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:119)

  at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:63)

  at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:52)

  at com.sun.proxy.$Proxy0.selectByName(Unknown Source)

  at com.dcits.test.UserMapperTest.testSelectByName(UserMapperTest.java:30)

  at com.dcits.test.UserMapperTest.main(UserMapperTest.java:78)

Caused by: org.apache.ibatis.reflection.ReflectionException: Error instantiating class com.dcits.po.User with invalid types () or values (). Cause: java.lang.NoSuchMethodException: com.dcits.po.User.()

  at org.apache.ibatis.reflection.factory.DefaultObjectFactory.instantiateClass(DefaultObjectFactory.java:86)

  at org.apache.ibatis.reflection.factory.DefaultObjectFactory.create(DefaultObjectFactory.java:48)

  at org.apache.ibatis.reflection.factory.DefaultObjectFactory.create(DefaultObjectFactory.java:41)

  at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.createResultObject(DefaultResultSetHandler.java:528)

  at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.createResultObject(DefaultResultSetHandler.java:507)

  at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.getRowValue(DefaultResultSetHandler.java:331)

  at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleRowValuesForSimpleResultMap(DefaultResultSetHandler.java:291)

  at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleRowValues(DefaultResultSetHandler.java:266)

  at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleResultSet(DefaultResultSetHandler.java:236)

  at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleResultSets(DefaultResultSetHandler.java:150)

  at org.apache.ibatis.executor.statement.PreparedStatementHandler.query(PreparedStatementHandler.java:60)

  at org.apache.ibatis.executor.statement.RoutingStatementHandler.query(RoutingStatementHandler.java:73)

  at org.apache.ibatis.executor.SimpleExecutor.doQuery(SimpleExecutor.java:60)

  at org.apache.ibatis.executor.BaseExecutor.queryFromDatabase(BaseExecutor.java:267)

  at org.apache.ibatis.executor.BaseExecutor.query(BaseExecutor.java:137)

  at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:96)

  at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:77)

  at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:108)

  ... 7 more

Caused by: java.lang.NoSuchMethodException: com.dcits.po.User.()

  at java.lang.Class.getConstructor0(Class.java:3082)

  at java.lang.Class.getDeclaredConstructor(Class.java:2178)

  at org.apache.ibatis.reflection.factory.DefaultObjectFactory.instantiateClass(DefaultObjectFactory.java:60)

  ... 24 more

报错原因:mybatis在初始化对象时,需要无参构造进行pojo属性的加载。而我在生成pojo类时创建了有参数的构造方法。在简单的Dao中调用时是没有问题的,但是如果使用Mybatis框架根据接口定义创建接口的动态代理对象(代理对象的方法体如Dao中方法体的实现)就会报错。

解决办法:

去掉pojo类中的有参构造方法,使用无参构造方法初始化对象。

Mybatis使用报错org.apache.ibatis.exceptions.PersistenceException:_第1张图片

你可能感兴趣的:(Mybatis,Idea,java)