mybatis遇到问题解决方法---- Error querying database.

时间:2024-04-03 11:59:39

程序员快乐多,找bug,一个又一个,感觉还不错。

今天来说说映射的问题,这个问题通常会在什么时候出现呢?

大家肯定能想到的就是再进行数据库查询的时候,不错,按照规则,我们通常会将数据库字段多单词之间用下划线连接(stu_no,stu_name……),而我们在写java实体bean(也称entity)时,通常采用驼峰命名法(stuNo,stuName……),如稍不注意,则会出现查找不到的情况,特别是对于初学者,这种错误是打击性的,因为,怎么都找不到,当然,主要原因还是我比较菜。

这里跟大家简述一下在mybatis中遇到的映射问题:

mybatis遇到问题解决方法---- Error querying database.

在这里我们很容易就发现带有引号的这个‘stuno’,以及前面的ReflectionException,所以很容易判断是映射出了问题

我们看一下pojo和数据库中的字段,大家看一下我的:

mybatis遇到问题解决方法---- Error querying database.

mybatis遇到问题解决方法---- Error querying database.

是不是很容易发现了问题,ok,修改一下stuMap即可,修改前后的代码如下图:

修改前
<resultMap type="student" id="studentMap">
 		<result column="stu_no" property="stuno"/>
 		<result column="stu_name" property="stuName"/>
 	</resultMap>
修改后
<resultMap type="student" id="studentMap">
 		<result column="stu_no" property="stuNo"/>
 		<result column="stu_name" property="stuName"/>
 	</resultMap>

这个时候再去运行,就会发现,无障碍了。

再此附上事故现场:

org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: org.apache.ibatis.reflection.ReflectionException: Could not set property 'stuno' of 'class com.zzxtit.boot.mybatis.first.Student' with value '20160800714' Cause: org.apache.ibatis.reflection.ReflectionException: There is no setter for property named 'stuno' in 'class com.zzxtit.boot.mybatis.first.Student'
### The error may exist in com/zzxtit/boot/mybatis/mapper/stuMapper.xml
### The error may involve com.zzxtit.boot.mybatis.mapper.StudentMapper.getStuByStuNo
### The error occurred while handling results
### SQL: Select * from student where stu_no = ?
### Cause: org.apache.ibatis.reflection.ReflectionException: Could not set property 'stuno' of 'class com.zzxtit.boot.mybatis.first.Student' with value '20160800714' Cause: org.apache.ibatis.reflection.ReflectionException: There is no setter for property named 'stuno' in 'class com.zzxtit.boot.mybatis.first.Student'
    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.session.defaults.DefaultSqlSession.selectOne(DefaultSqlSession.java:77)
    at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:84)
    at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:58)
    at com.sun.proxy.$Proxy6.getStuByStuNo(Unknown Source)
    at com.zzxtit.boot.mybatis.mapper.MapperDemo.getTest(MapperDemo.java:41)
    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:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    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:538)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)
Caused by: org.apache.ibatis.reflection.ReflectionException: Could not set property 'stuno' of 'class com.zzxtit.boot.mybatis.first.Student' with value '20160800714' Cause: org.apache.ibatis.reflection.ReflectionException: There is no setter for property named 'stuno' in 'class com.zzxtit.boot.mybatis.first.Student'
    at org.apache.ibatis.reflection.wrapper.BeanWrapper.setBeanProperty(BeanWrapper.java:185)
    at org.apache.ibatis.reflection.wrapper.BeanWrapper.set(BeanWrapper.java:59)
    at org.apache.ibatis.reflection.MetaObject.setValue(MetaObject.java:140)
    at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.applyPropertyMappings(DefaultResultSetHandler.java:455)
    at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.getRowValue(DefaultResultSetHandler.java:404)
    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:148)
    ... 30 more
Caused by: org.apache.ibatis.reflection.ReflectionException: There is no setter for property named 'stuno' in 'class com.zzxtit.boot.mybatis.first.Student'
    at org.apache.ibatis.reflection.Reflector.getSetInvoker(Reflector.java:387)
    at org.apache.ibatis.reflection.MetaClass.getSetInvoker(MetaClass.java:167)
    at org.apache.ibatis.reflection.wrapper.BeanWrapper.setBeanProperty(BeanWrapper.java:177)
    ... 46 more