java mybatis org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.

博客讲述了在使用 MyBatis 框架时遇到的查询数据库异常问题,问题出现在多线程环境下。作者发现即使 SQL 语句在数据库中能正常执行,但在代码中仍抛出 NullPointerException。通过逐步排查,确定问题与参数有关。最终发现参数集合通过 parallelStream() 填充导致,由于 parallelStream 的并行处理特性,去掉 parallelStream 后问题得到解决,强调了代码执行效率过高可能引发的问题。
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: java.lang.NullPointerException
### Cause: java.lang.NullPointerException
	at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:92)
	at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:440)
	at com.sun.proxy.$Proxy111.selectList(Unknown Source)
	at org.mybatis.spring.SqlSessionTemplate.selectList(SqlSessionTemplate.java:223)
	at com.baomidou.mybatisplus.core.override.MybatisMapperMethod.executeForMany(MybatisMapperMethod.java:177)
	at com.baomidou.mybatisplus.core.override.MybatisMapperMethod.execute(MybatisMapperMethod.java:78)
	at com.baomidou.mybatisplus.core.override.MybatisMapperProxy.invoke(MybatisMapperProxy.java:96)
	at com.sun.proxy.$Proxy149.selectSpuAndSkuCodes(Unknown Source)
	at com.lx.benefits.task.JDPriceNewTask.updateSkuPriceAndStatus(JDPriceNewTask.java:236)
	at com.lx.benefits.task.JDPriceNewTask$$FastClassBySpringCGLIB$$bc7e5622.invoke(<generated>)
	at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
	at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:749)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
	at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:93)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
	at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:100)
	at com.lx.benefits.aspect.SchedulTaskAspect.schedulTaskAspect(SchedulTaskAspect.java:48)
	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.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:644)
	at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:633)
	at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:70)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:175)
	at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688)
	at com.lx.benefits.task.JDPriceNewTask$$EnhancerBySpringCGLIB$$ef464ae9.updateSkuPriceAndStatus(<generated>)
	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.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:84)
	at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)
	at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:93)
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
	at java.util.concurrent.FutureTask.run$$$capture(FutureTask.java:266)
	at java.util.concurrent.FutureTask.run(FutureTask.java)
	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180)
	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at java.lang.Thread.run(Thread.java:748)

查看异常信息可知查询数据库错误, 但是将sql语句直接在数据库中执行是没问题的, 百度了一下, 大多是说mapper注解或者多参数请求未加@param, 看了一下代码, 嗯, 都已经写了, 没问题, 然后重新写一个最简单的sql语句无参数直接返回数据条数count(*) 这次没有问题了, 思考了一下,那可能还是参数问题, 将参数放在查询数据条数的语句中, 即使xml文件中未使用仍然出现这个错误, 由此问题锁定在参数上, 断点跟踪查看参数没有问题, 而且错误不出现了, 思考了一下, 断点后正常, 那就说明有延迟停顿时正常, 查看参数代码, 参数集合是通过parallelStream().forEach填充的, 嗯, parallelStream是多线程且流是在使用时才处理数据的, 直接去掉parallelStream, OK没问题了, 可见代码执行效率太高也不行!

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.reflection.Reflecti是一个MyBatis框架中的异常。它通常表示在使用MyBatis进行数据库操作时发生了反射异常。 MyBatis是一个Java持久层框架,它简化了与数据库的交互。在使用MyBatis时,我们可以通过配置文件或注解来定义SQL语句,并使用Java对象来映射数据库表。当执行SQL语句时,MyBatis会使用反射机制来将结果映射到Java对象中。 然而,当出现反射异常时,就会抛出org.apache.ibatis.reflection.ReflectionException。这可能是由于以下原因导致的: 1. 未正确配置MyBatis的映射文件或注解。请确保你的映射文件或注解正确地定义了SQL语句和映射关系。 2. SQL语句中的参数或返回类型与映射文件或注解中定义的类型不匹配。请检查参数和返回类型是否正确。 3. 数据库连接或配置错误。请确保你的数据库连接配置正确,并且数据库可正常访问。 解决这个问题的方法包括: 1. 检查你的映射文件或注解是否正确配置,并确保SQL语句和映射关系正确。 2. 检查你的参数和返回类型是否与映射文件或注解中定义的类型匹配。 3. 检查你的数据库连接配置是否正确,并确保数据库可正常访问。 4. 查看MyBatis的日志输出,以获取更详细的错误信息。 以下是一个示例代码,演示了如何处理org.mybatis.spring.MyBatisSystemException异常: ```java try { // 执行MyBatis操作 } catch (org.mybatis.spring.MyBatisSystemException e) { // 处理异常 Throwable cause = e.getCause(); if (cause instanceof org.apache.ibatis.reflection.ReflectionException) { // 处理反射异常 // ... } else { // 处理其他异常 // ... } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值