【Java】解决MyBatis接受Select中聚合函数的值,出现:java.math.BigDecimal cannot be cast to java.lang.Integer 问题

异常

[org.springframework.scheduling.support.TaskUtils$LoggingErrorHandler 95 handleError] - Unexpected error occurred in scheduled task. java.lang.ClassCastException: java.math.BigDecimal cannot be cast to java.lang.Integer
	at com.auto.async.AutoDashboard.sendSubReport(AutoDashboard.java:506) ~[classes/:?]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_171]
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_171]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_171]
	at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_171]
	at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:333) ~[spring-aop-4.3.13.RELEASE.jar:4.3.13.RELEASE]
	at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190) ~[spring-aop-4.3.13.RELEASE.jar:4.3.13.RELEASE]
	at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:741) ~[spring-aop-4.3.13.RELEASE.jar:4.3.13.RELEASE]
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) ~[spring-aop-4.3.13.RELEASE.jar:4.3.13.RELEASE]
	at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:673) ~[spring-aop-4.3.13.RELEASE.jar:4.3.13.RELEASE]
	at com.auto.async.AutoDashboard$$EnhancerBySpringCGLIB$$b3bebd01.sendSubReport() ~[classes/:?]
	at com.auto.async.AutoRunSub.runSubDashBoard(AutoRunSub.java:69) ~[classes/:?]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_171]
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_171]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_171]
	at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_171]
	at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:65) ~[spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE]
	at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54) [spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE]
	at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:81) [spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE]
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_171]
	at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_171]
	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180) [?:1.8.0_171]
	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293) [?:1.8.0_171]
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [?:1.8.0_171]
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [?:1.8.0_171]
	at java.lang.Thread.run(Thread.java:748) [?:1.8.0_171]

重点异常内容是:
Unexpected error occurred in scheduled task. java.lang.ClassCastException: java.math.BigDecimal cannot be cast to java.lang.Integer

对应代码:

//定义的SQL接口方法
@Select("SELECT SUM(weekTimes) AS 'times', SUM(weekErrTimes) AS 'errTimes', SUM(weekDelay) AS 'delay', SUM(recordLen) AS 'recordLen' FROM dashboard WHERE proId=#{proId}")
Map getProAllErrSub(@Param(value = "proId") int proId);

//应用对应的接口方法
A: Map subs = managerDashboard.getProAllErrSub(pro.getId());
B: int recordLen = Integer.parseInt(subs.get("recordLen").toString());   // 就是这里报错

分析

因为A代码在执行过程中没有报错,但是在B执行的是否没有报错(前提是有数据的),这时候从异常上看,还是数据类型有问题,BigDecimal不能转Integer,并不能说明B处的代码有问题,通过了解才知道,MyBatis查询出来的数据默认都是BigDecimal,所以在接收SELECT数据的是否就不能用Integer类型,而是用BigDecimal,所以修改SQL接口方法返回类型。(BigDecimal不能直接转换为Integer类型)

正确代码

//定义的SQL接口方法
@Select("SELECT SUM(weekTimes) AS 'times', SUM(weekErrTimes) AS 'errTimes', SUM(weekDelay) AS 'delay', SUM(recordLen) AS 'recordLen' FROM dashboard WHERE proId=#{proId}")
Map getProAllErrSub(@Param(value = "proId") int proId);

//应用对应的接口方法
A: Map subs = managerDashboard.getProAllErrSub(pro.getId());
B: int recordLen = Integer.parseInt(subs.get("recordLen").toString());   

你可能感兴趣的:(Java)