错误信息:
org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: java.lang.NumberFormatException: For input string: "李四"
### Cause: java.lang.NumberFormatException: For input string: "李四"
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:153)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:145)
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$PlainMethodInvoker.invoke(MapperProxy.java:145)
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:86)
at com.sun.proxy.$Proxy6.getEmpCondition(Unknown Source)
at com.atguigu.mybatis.test.ResultMapTest.testGetEmpCondition(ResultMapTest.java:19)
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:483)
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.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.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38)
at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:232)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:55)
Caused by: java.lang.NumberFormatException: For input string: "李四"
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043)
at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
at java.lang.Double.parseDouble(Double.java:538)
at org.apache.ibatis.ognl.OgnlOps.doubleValue(OgnlOps.java:253)
at org.apache.ibatis.ognl.OgnlOps.compareWithConversion(OgnlOps.java:104)
at org.apache.ibatis.ognl.OgnlOps.isEqual(OgnlOps.java:153)
at org.apache.ibatis.ognl.OgnlOps.equal(OgnlOps.java:814)
at org.apache.ibatis.ognl.ASTNotEq.getValueBody(ASTNotEq.java:53)
at org.apache.ibatis.ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:212)
at org.apache.ibatis.ognl.SimpleNode.getValue(SimpleNode.java:258)
at org.apache.ibatis.ognl.ASTAnd.getValueBody(ASTAnd.java:61)
at org.apache.ibatis.ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:212)
at org.apache.ibatis.ognl.SimpleNode.getValue(SimpleNode.java:258)
at org.apache.ibatis.ognl.Ognl.getValue(Ognl.java:586)
at org.apache.ibatis.ognl.Ognl.getValue(Ognl.java:550)
at org.apache.ibatis.scripting.xmltags.OgnlCache.getValue(OgnlCache.java:46)
at org.apache.ibatis.scripting.xmltags.ExpressionEvaluator.evaluateBoolean(ExpressionEvaluator.java:32)
at org.apache.ibatis.scripting.xmltags.IfSqlNode.apply(IfSqlNode.java:34)
at org.apache.ibatis.scripting.xmltags.MixedSqlNode.lambda$apply$0(MixedSqlNode.java:32)
at org.apache.ibatis.scripting.xmltags.MixedSqlNode$$Lambda$42/603856241.accept(Unknown Source)
at java.util.ArrayList.forEach(ArrayList.java:1249)
at org.apache.ibatis.scripting.xmltags.MixedSqlNode.apply(MixedSqlNode.java:32)
at org.apache.ibatis.scripting.xmltags.DynamicSqlSource.getBoundSql(DynamicSqlSource.java:39)
at org.apache.ibatis.mapping.MappedStatement.getBoundSql(MappedStatement.java:305)
at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:87)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:151)
... 32 more
分析错误信息和堆栈跟踪,可以得出以下结论:
-
在执行SQL查询时,MyBatis尝试将字符串"李四"转换为数字类型。然而,"李四"不是有效的数字格式,导致了
NumberFormatException
异常。 -
错误发生在
DynamicSqlSource
中,该类是MyBatis用于处理动态SQL的一部分。 -
可能是在执行查询之前的参数处理或SQL语句构建过程中,将参数错误地用作了数字类型的条件,而实际上参数应该是字符串类型。
要解决这个问题,您需要检查涉及到参数的地方,确保参数的数据类型与SQL语句中的占位符匹配。请检查以下几个方面:
-
确认在构建SQL查询或配置MyBatis映射文件时,正确使用了参数,并且参数的数据类型与数据库表的列类型相匹配。
-
检查查询语句中的条件语句,确保使用字符串类型的参数作为字符串比较,而不是误用为数字类型。
-
如果参数是从用户输入获取的,确保对用户输入进行适当的验证和转换,以防止非法输入导致的异常。
通过仔细检查和调试相关的代码,特别是涉及到参数和SQL查询的部分,您应该能够找到导致此异常的具体原因并进行修复。
解决办法:
修改前:
<select id="getEmpCondition" resultType="Emp">
select * from t_emp where
<if test="empName !=null and empName !=' '">
emp_name=#{empName}
</if>
<if test="age !=null and age !=''">
and age=#{age}
</if>
<if test="gender !=null and gender !=''">
and gender=#{gender}
</if>
</select>
修复后:
<select id="getEmpCondition" resultType="Emp">
select * from t_emp where
<if test='empName != " " and empName != null'>
emp_name = #{empName}
</if>
<if test='age != " " and age != null'>
and age = #{age}
</if>
<if test='gender != " " and gender != null'>
and gender = #{gender}
</if>
</select>