Mybatis报错:There is no getter for property named 'xxx' in 'class xxx 的解决办法

在mybatis中遇到这样的报错,There is no getter for property named ‘xxx’ in 'class xxx ,这篇博客给出了解决办法。

文章目录

    • 一、错误信息
      • 1. 控制台报错信息
      • 2. 主要的错误信息
    • 二、解决错误

一、错误信息

1. 控制台报错信息

org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'username' in 'class com.zxy.pojo.User'
### Cause: org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'username' in 'class com.zxy.pojo.User'

	at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
	at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:150)
	at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:141)
	at org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:137)
	at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:75)
	at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:59)
	at com.sun.proxy.$Proxy5.findUserByVO(Unknown Source)
	at com.zxy.test.MybatisTest.testFindByVO(MybatisTest.java:154)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
	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:30)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
	at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'username' in 'class com.zxy.pojo.User'
	at org.apache.ibatis.reflection.Reflector.getGetInvoker(Reflector.java:419)
	at org.apache.ibatis.reflection.MetaClass.getGetInvoker(MetaClass.java:164)
	at org.apache.ibatis.reflection.wrapper.BeanWrapper.getBeanProperty(BeanWrapper.java:162)
	at org.apache.ibatis.reflection.wrapper.BeanWrapper.get(BeanWrapper.java:49)
	at org.apache.ibatis.reflection.MetaObject.getValue(MetaObject.java:122)
	at org.apache.ibatis.reflection.MetaObject.getValue(MetaObject.java:119)
	at org.apache.ibatis.executor.BaseExecutor.createCacheKey(BaseExecutor.java:219)
	at org.apache.ibatis.executor.CachingExecutor.createCacheKey(CachingExecutor.java:146)
	at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:82)
	at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:148)
	... 30 more

2. 主要的错误信息

There is no getter for property named ‘username’ in ‘class com.zxy.pojo.User’

二、解决错误

  1. 最先想着是不是User实体类没有写 Getter and Setter 方法,可是发现写了。
    Mybatis报错:There is no getter for property named 'xxx' in 'class xxx 的解决办法_第1张图片

  2. 然后查看数据库的表字段和User实体类的表字段

数据库的表字段
Mybatis报错:There is no getter for property named 'xxx' in 'class xxx 的解决办法_第2张图片
User实体类的字段
Mybatis报错:There is no getter for property named 'xxx' in 'class xxx 的解决办法_第3张图片

发现数据库表字段与实体类属性不一致,这是我后来改的,在 sql 里面加了别名。
Mybatis报错:There is no getter for property named 'xxx' in 'class xxx 的解决办法_第4张图片
3. 最后发现在模糊匹配的时候把属性还没有改过来。
Mybatis报错:There is no getter for property named 'xxx' in 'class xxx 的解决办法_第5张图片

注意:在遇到这个错误的时候一定要 检查你的实体类中的属性名是否和要映射的数据库表中的字段名字一致。sql 语句的内容不区分大小写,如果不是 sql 语句的内容一定要区分大小写

你可能感兴趣的:(常见错误)