一、异常描述
关于mybatis映射xml配置文件执行报“org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.builder.BuilderException: The expression 'datas' evaluated to a null value..Caused by: org.apache.ibatis.builder.BuilderException: The expression 'datas' evaluated to a null value.”错误异常,详细日志如下
[14:27:08.651] [ERROR] [<T=V0MN2AP210IIQCdX>] ReportDataController: ReportDataController-edit@b@org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.builder.BuilderException: The expression 'datas' evaluated to a null value.@b@ at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:76) ~[mybatis-spring-1.2.3-1.2.3.jar:1.2.3]@b@ at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:399) ~[mybatis-spring-1.2.3-1.2.3.jar:1.2.3]@b@ at com.sun.proxy.$Proxy37.insert(Unknown Source) ~[?:?]@b@ at org.mybatis.spring.SqlSessionTemplate.insert(SqlSessionTemplate.java:253) ~[mybatis-spring-1.2.3-1.2.3.jar:1.2.3]@b@ at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:52) ~[mybatis-3.3.0-3.3.0.jar:3.3.0]@b@ at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:53) ~[mybatis-3.3.0-3.3.0.jar:3.3.0]@b@ at com.sun.proxy.$Proxy56.batchInsert(Unknown Source) ~[?:?]@b@ at com.xwood.pms.templet.service.impl.ReportDataConfServiceImpl.deleteBeforeInsert(ReportDataConfServiceImpl.java:37) ~[bin/:?]@b@ at com.xwood.pms.templet.web.controller.ReportDataController.edit(ReportDataController.java:168) [bin/:?]@b@ at com.xwood.pms.templet.web.controller.ReportDataController$$FastClassBySpringCGLIB$$acc8b017.invoke(<generated>) [spring-core-3.2.13.RELEASE-3.2.13.RELEASE.jar:?]@b@ at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) [spring-core-3.2.13.RELEASE-3.2.13.RELEASE.jar:3.2.13.RELEASE]@b@ at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:700) [spring-aop-3.2.13.RELEASE-3.2.13.RELEASE.jar:3.2.13.RELEASE]@b@ at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150) [spring-aop-3.2.13.RELEASE-3.2.13.RELEASE.jar:3.2.13.RELEASE]@b@ at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:91) [spring-aop-3.2.13.RELEASE-3.2.13.RELEASE.jar:3.2.13.RELEASE]@b@ at com.xwood.pms.templet.aop.LogAppendAdvice.logController(LogAppendAdvice.java:47) [bin/:?]@b@ at sun.reflect.GeneratedMethodAccessor74.invoke(Unknown Source) ~[?:?]@b@ at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_11]@b@ at java.lang.reflect.Method.invoke(Method.java:483) ~[?:1.8.0_11]@b@ at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:621) [spring-aop-3.2.13.RELEASE-3.2.13.RELEASE.jar:3.2.13.RELEASE]@b@ at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:610) [spring-aop-3.2.13.RELEASE-3.2.13.RELEASE.jar:3.2.13.RELEASE]@b@ at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:65) [spring-aop-3.2.13.RELEASE-3.2.13.RELEASE.jar:3.2.13.RELEASE]@b@ at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) [spring-aop-3.2.13.RELEASE-3.2.13.RELEASE.jar:3.2.13.RELEASE]@b@ at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:91) [spring-aop-3.2.13.RELEASE-3.2.13.RELEASE.jar:3.2.13.RELEASE]@b@ at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) [spring-aop-3.2.13.RELEASE-3.2.13.RELEASE.jar:3.2.13.RELEASE]@b@ at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:633) [spring-aop-3.2.13.RELEASE-3.2.13.RELEASE.jar:3.2.13.RELEASE]@b@ at com.xwood.pms.templet.web.controller.ReportDataController$$EnhancerBySpringCGLIB$$a23d0713.edit(<generated>) [spring-core-3.2.13.RELEASE-3.2.13.RELEASE.jar:?]@b@ at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_11]@b@ at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_11]@b@ at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_11]@b@ at java.lang.reflect.Method.invoke(Method.java:483) ~[?:1.8.0_11]@b@ at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215) [spring-web-3.2.13.RELEASE-3.2.13.RELEASE.jar:3.2.13.RELEASE]@b@ at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132) [spring-web-3.2.13.RELEASE-3.2.13.RELEASE.jar:3.2.13.RELEASE]@b@ at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104) [spring-webmvc-3.2.13.RELEASE-3.2.13.RELEASE.jar:3.2.13.RELEASE]@b@ at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:743) [spring-webmvc-3.2.13.RELEASE-3.2.13.RELEASE.jar:3.2.13.RELEASE]@b@ at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:672) [spring-webmvc-3.2.13.RELEASE-3.2.13.RELEASE.jar:3.2.13.RELEASE]@b@ at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:82) [spring-webmvc-3.2.13.RELEASE-3.2.13.RELEASE.jar:3.2.13.RELEASE]@b@ at com.xwood.sbase.web.DefWebRequestHandler.handleRequest(DefWebRequestHandler.java:61) [sbase-5.2.6.jar:5.2.6]@b@ at com.xwood.sbase.web.filter.DefaultFilterChain.doFinal(DefaultFilterChain.java:64) [sbase-5.2.6.jar:5.2.6]@b@ at com.xwood.sbase.web.filter.DefaultFilterChain.doFilter(DefaultFilterChain.java:57) [sbase-5.2.6.jar:5.2.6]@b@ at com.xwood.pms.templet.common.filter.XssSecurityFilter.doFilter(XssSecurityFilter.java:50) [bin/:?]@b@ at com.xwood.sbase.web.filter.DefaultFilterChain.doFilter(DefaultFilterChain.java:52) [sbase-5.2.6.jar:5.2.6]@b@ at com.xwood.sbase.web.filter.impl.PostJsonToFormFilter.doFilter(PostJsonToFormFilter.java:38) [sbase-5.2.6.jar:5.2.6]@b@ at com.xwood.sbase.web.filter.DefaultFilterChain.doFilter(DefaultFilterChain.java:52) [sbase-5.2.6.jar:5.2.6]@b@ at com.xwood.sbase.web.filter.impl.FileUploadFilter.doFilter(FileUploadFilter.java:40) [sbase-5.2.6.jar:5.2.6]@b@ at com.xwood.sbase.web.filter.DefaultFilterChain.doFilter(DefaultFilterChain.java:52) [sbase-5.2.6.jar:5.2.6]@b@ at com.xwood.sbase.web.DefWebDispatcher.handleRequest(DefWebDispatcher.java:134) [sbase-5.2.6.jar:5.2.6]@b@ at com.xwood.sbase.sar.SARContextBean.handleWebRequest(SARContextBean.java:170) [sbase-5.2.6.jar:5.2.6]@b@ at com.xwood.sbase.web.PAppWebRequestHandler.handleRequest(PAppWebRequestHandler.java:44) [sbase-5.2.6.jar:5.2.6]@b@ at com.xwood.sbase.web.filter.DefaultFilterChain.doFinal(DefaultFilterChain.java:64) [sbase-5.2.6.jar:5.2.6]@b@ at com.xwood.sbase.web.filter.DefaultFilterChain.doFilter(DefaultFilterChain.java:57) [sbase-5.2.6.jar:5.2.6]@b@ at com.xwood.sbase.web.filter.impl.CommonWebFilter.doFilter(CommonWebFilter.java:46) [sbase-5.2.6.jar:5.2.6]@b@ at com.xwood.sbase.web.filter.DefaultFilterChain.doFilter(DefaultFilterChain.java:52) [sbase-5.2.6.jar:5.2.6]@b@ at com.xwood.sbase.web.DefPAppWebDispatcher.dispatch(DefPAppWebDispatcher.java:80) [sbase-5.2.6.jar:5.2.6]@b@ at com.xwood.sbase.PAppContextBean.handleWebRequest(PAppContextBean.java:182) [sbase-5.2.6.jar:5.2.6]@b@ at com.xwood.sbase.protocol.web.PappServlet.service(PappServlet.java:63) [sbase-5.2.6.jar:5.2.6]@b@ at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:684) [jetty-servlet-8.1.16.v20140903-8.1.16.jar:8.1.16.v20140903]@b@ at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1496) [jetty-servlet-8.1.16.v20140903-8.1.16.jar:8.1.16.v20140903]@b@ at com.xwood.sbase.protocol.jetty.JettyResourceFilter.doFilter(JettyResourceFilter.java:73) [sbase-5.2.6.jar:5.2.6]@b@ at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1467) [jetty-servlet-8.1.16.v20140903-8.1.16.jar:8.1.16.v20140903]@b@ at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:501) [jetty-servlet-8.1.16.v20140903-8.1.16.jar:8.1.16.v20140903]@b@ at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:229) [jetty-server-8.1.16.v20140903-8.1.16.jar:8.1.16.v20140903]@b@ at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1086) [jetty-server-8.1.16.v20140903-8.1.16.jar:8.1.16.v20140903]@b@ at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:429) [jetty-servlet-8.1.16.v20140903-8.1.16.jar:8.1.16.v20140903]@b@ at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:193) [jetty-server-8.1.16.v20140903-8.1.16.jar:8.1.16.v20140903]@b@ at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1020) [jetty-server-8.1.16.v20140903-8.1.16.jar:8.1.16.v20140903]@b@ at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:135) [jetty-server-8.1.16.v20140903-8.1.16.jar:8.1.16.v20140903]@b@ at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:116) [jetty-server-8.1.16.v20140903-8.1.16.jar:8.1.16.v20140903]@b@ at org.eclipse.jetty.server.Server.handle(Server.java:370) [jetty-server-8.1.16.v20140903-8.1.16.jar:8.1.16.v20140903]@b@ at org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(AbstractHttpConnection.java:494) [jetty-server-8.1.16.v20140903-8.1.16.jar:8.1.16.v20140903]@b@ at org.eclipse.jetty.server.AbstractHttpConnection.content(AbstractHttpConnection.java:982) [jetty-server-8.1.16.v20140903-8.1.16.jar:8.1.16.v20140903]@b@ at org.eclipse.jetty.server.AbstractHttpConnection$RequestHandler.content(AbstractHttpConnection.java:1043) [jetty-server-8.1.16.v20140903-8.1.16.jar:8.1.16.v20140903]@b@ at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:865) [jetty-http-8.1.16.v20140903-8.1.16.jar:8.1.16.v20140903]@b@ at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:240) [jetty-http-8.1.16.v20140903-8.1.16.jar:8.1.16.v20140903]@b@ at org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:82) [jetty-server-8.1.16.v20140903-8.1.16.jar:8.1.16.v20140903]@b@ at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:696) [jetty-io-8.1.16.v20140903-8.1.16.jar:8.1.16.v20140903]@b@ at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:53) [jetty-io-8.1.16.v20140903-8.1.16.jar:8.1.16.v20140903]@b@ at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:608) [jetty-util-8.1.16.v20140903-8.1.16.jar:8.1.16.v20140903]@b@ at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:543) [jetty-util-8.1.16.v20140903-8.1.16.jar:8.1.16.v20140903]@b@ at java.lang.Thread.run(Thread.java:745) [?:1.8.0_11]@b@Caused by: org.apache.ibatis.builder.BuilderException: The expression 'datas' evaluated to a null value.@b@ at org.apache.ibatis.scripting.xmltags.ExpressionEvaluator.evaluateIterable(ExpressionEvaluator.java:45) ~[mybatis-3.3.0-3.3.0.jar:3.3.0]@b@ at org.apache.ibatis.scripting.xmltags.ForEachSqlNode.apply(ForEachSqlNode.java:55) ~[mybatis-3.3.0-3.3.0.jar:3.3.0]@b@ at org.apache.ibatis.scripting.xmltags.MixedSqlNode.apply(MixedSqlNode.java:33) ~[mybatis-3.3.0-3.3.0.jar:3.3.0]@b@ at org.apache.ibatis.scripting.xmltags.DynamicSqlSource.getBoundSql(DynamicSqlSource.java:41) ~[mybatis-3.3.0-3.3.0.jar:3.3.0]@b@ at org.apache.ibatis.mapping.MappedStatement.getBoundSql(MappedStatement.java:280) ~[mybatis-3.3.0-3.3.0.jar:3.3.0]@b@ at org.apache.ibatis.executor.statement.BaseStatementHandler.<init>(BaseStatementHandler.java:64) ~[mybatis-3.3.0-3.3.0.jar:3.3.0]@b@ at org.apache.ibatis.executor.statement.PreparedStatementHandler.<init>(PreparedStatementHandler.java:39) ~[mybatis-3.3.0-3.3.0.jar:3.3.0]@b@ at org.apache.ibatis.executor.statement.RoutingStatementHandler.<init>(RoutingStatementHandler.java:45) ~[mybatis-3.3.0-3.3.0.jar:3.3.0]@b@ at org.apache.ibatis.session.Configuration.newStatementHandler(Configuration.java:488) ~[mybatis-3.3.0-3.3.0.jar:3.3.0]@b@ at org.apache.ibatis.executor.SimpleExecutor.doUpdate(SimpleExecutor.java:47) ~[mybatis-3.3.0-3.3.0.jar:3.3.0]@b@ at org.apache.ibatis.executor.BaseExecutor.update(BaseExecutor.java:115) ~[mybatis-3.3.0-3.3.0.jar:3.3.0]@b@ at org.apache.ibatis.executor.CachingExecutor.update(CachingExecutor.java:75) ~[mybatis-3.3.0-3.3.0.jar:3.3.0]@b@ at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:170) ~[mybatis-3.3.0-3.3.0.jar:3.3.0]@b@ at org.apache.ibatis.session.defaults.DefaultSqlSession.insert(DefaultSqlSession.java:157) ~[mybatis-3.3.0-3.3.0.jar:3.3.0]@b@ at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_11]@b@ at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_11]@b@ at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_11]@b@ at java.lang.reflect.Method.invoke(Method.java:483) ~[?:1.8.0_11]@b@ at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:386) ~[mybatis-spring-1.2.3-1.2.3.jar:1.2.3]@b@ ... 77 more
二、解决方法
1. 原来xml配置如下
<?xml version="1.0" encoding="UTF-8" ?> @b@<!DOCTYPE mapper @b@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" @b@ "http://mybatis.org/dtd/mybatis-3-mapper.dtd">@b@<mapper namespace="com.xwood.soofa.data.dao.ReportDataDAO"> @b@ @b@ <!-- 批量新增 -->@b@ <insert id="batchInsert" parameterType="ccom.xwood.soofa.data.dto.ReportDataDTO">@b@ INSERT INTO report_master@b@ (ID, @b@ FIELD_TYPE,@b@ FIELD_NAME, @b@ STATUS,@b@ CREATED_BY,@b@ CREATED_DATE,@b@ UPDATED_BY,@b@ UPDATED_DATE,@b@ ORD_NUM)@b@ SELECT report_data_seq.nextval id,@b@ c.plateId,@b@ c.fieldType,@b@ c.fieldName,@b@ c.kpiId,@b@ c.status,@b@ c.createdBy,@b@ c.createdDate,@b@ c.updatedBy,@b@ c.updatedDate,@b@ c.ordNum@b@ FROM ( <foreach collection="datas" separator="union all"@b@ item="repdata" index="index" >@b@ select @b@ #{repdata.fieldType,jdbcType=VARCHAR} fieldType, @b@ 'Y' status,@b@ #{repdata.createdBy,jdbcType=VARCHAR} createdBy,@b@ systimestamp createdDate,@b@ #{repdata.updatedBy,jdbcType=VARCHAR} updatedBy,@b@ systimestamp updatedDate,@b@ #{repdata.ordNum,jdbcType=VARCHAR} ordNum@b@ from dual@b@ </foreach>) c@b@ </insert> @b@ @b@</mapper>
2. ReportDataServiceImpl服务实现类中批量插入batchInsert方法报错
@Service("reportDataService")@b@public class ReportDataServiceImpl implements ReportDataService {@b@ @b@ @b@ @Autowired@b@ private ReportDataDAO ReportDataDAO;@b@ @b@ .. @b@ @Override@b@ public void batchInsert(ReportDataDTO dto) throws Exception {@b@ ReportDataDAO.batchInsert(dto);@b@ }@b@ @b@ @b@}
3. 由于没有判断dto的datas(List<ReportDataDTO>集合)为空情况,造成插入数据报上面异常错误,修改为下
@Service("reportDataService")@b@public class ReportDataServiceImpl implements ReportDataService {@b@ @b@ @b@ @Autowired@b@ private ReportDataDAO ReportDataDAO;@b@ @b@ .. @b@ @Override@b@ public void batchInsert(ReportDataDTO dto) throws Exception {@b@ if(dto!=null&&CollectionUtils.isNotEmpty(dto.getDatas())){@b@ ReportDataDAO.batchInsert(dto);@b@ }@b@ }@b@ @b@ @b@}