在一次使用mybatis和mysql开发过程中,需要批量插入数据,并返回主键id。于是写了下面的接口和SQL实现:
// 接口定义
int batchInsert(List list);
// mybatis SQL (这个SQL是正解,但在开发过程中始终无法返回主键)
insert into marketing_activity_record
(create_time, update_time, create_user, update_user, trace_no, order_no, activity_id,user_id,receive_config,confirm_status,tel)
values
(#{item.createTime},#{item.updateTime},#{item.createUser},#{item.updateUser},
#{item.traceNo},#{item.orderNo},#{item.activityId},#{item.userId},#{item.receiveConfig},#{item.confirmStatus},#{item.tel})
在测试的过程中,发现始终没有返回主键信息。查看使用的mybatis版本,发现是"3.2.3",而 “mybatis3.3.1支持批量插入后返回主键ID”,于是直接 升级mybatis版本为“3.4.6” 于是成功解决问题。
上面通过升级mybatis版本解决了批量插入数据的问题,但意外的却引起另一种错误。如下:
ERROR: 2019-04-01 23:19:53.744 10.18.32.208 [com.alibaba.dubbo.rpc.filter.ExceptionFilter.java:87 ExceptionFilter::invoke()] traceid[155413199334037956] traceUrl[] rpcid[] logid[155413199368698767] url[com.missfresh.pmp.bg.center.service.ISeckillService.getBill] [DUBBO] Got unchecked and undeclared exception which called by 10.18.32.243. service: com.missfresh.pmp.bg.center.service.ISeckillService, method: getBill, exception: org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: java.lang.IllegalArgumentException: invalid comparison: java.util.Date and java.lang.String
### Cause: java.lang.IllegalArgumentException: invalid comparison: java.util.Date and java.lang.String, dubbo version: 2.6.0.2, current host: 10.18.32.208
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: java.lang.IllegalArgumentException: invalid comparison: java.util.Date and java.lang.String
### Cause: java.lang.IllegalArgumentException: invalid comparison: java.util.Date and java.lang.String
at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:77)
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:446)
at com.sun.proxy.$Proxy31.selectList(Unknown Source)
at org.mybatis.spring.SqlSessionTemplate.selectList(SqlSessionTemplate.java:230)
at org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:139)
at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:76)
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:59)
at com.sun.proxy.$Proxy65.getSeckillBill(Unknown Source)
at com.missfresh.pmp.bg.center.service.impl.SeckillServiceImpl.dbGetSeckillBill(SeckillServiceImpl.java:1513)
at com.missfresh.pmp.bg.center.service.impl.SeckillServiceImpl.getBill(SeckillServiceImpl.java:405)
at com.missfresh.pmp.bg.center.service.impl.SeckillServiceImpl$$FastClassBySpringCGLIB$$bbb6d3a7.invoke()
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:669)
at com.missfresh.pmp.bg.center.service.impl.SeckillServiceImpl$$EnhancerBySpringCGLIB$$2713cc24.getBill()
at com.alibaba.dubbo.common.bytecode.Wrapper11.invokeMethod(Wrapper11.java)
at com.alibaba.dubbo.rpc.proxy.javassist.JavassistProxyFactory$1.doInvoke(JavassistProxyFactory.java:45)
at com.alibaba.dubbo.rpc.proxy.AbstractProxyInvoker.invoke(AbstractProxyInvoker.java:71)
at com.alibaba.dubbo.config.invoker.DelegateProviderMetaDataInvoker.invoke(DelegateProviderMetaDataInvoker.java:48)
at com.alibaba.dubbo.rpc.protocol.InvokerWrapper.invoke(InvokerWrapper.java:52)
at com.missfresh.pmp.bg.center.filter.ParamCheckFilter.invoke(ParamCheckFilter.java:48)
at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:68)
at com.missfresh.as.log.filter.NoticeFilterDubbo.invoke(NoticeFilterDubbo.java:64)
at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:68)
at com.alibaba.dubbo.rpc.filter.ExceptionFilter.invoke(ExceptionFilter.java:64)
at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:68)
at com.alibaba.dubbo.monitor.support.MonitorFilter.invoke(MonitorFilter.java:64)
at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:68)
at com.alibaba.dubbo.rpc.filter.TimeoutFilter.invoke(TimeoutFilter.java:41)
at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:68)
at com.alibaba.dubbo.rpc.protocol.dubbo.filter.TraceFilter.invoke(TraceFilter.java:77)
at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:68)
at com.alibaba.dubbo.rpc.filter.ContextFilter.invoke(ContextFilter.java:71)
at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:68)
at com.alibaba.dubbo.rpc.filter.GenericFilter.invoke(GenericFilter.java:131)
at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:68)
at com.alibaba.dubbo.rpc.filter.ClassLoaderFilter.invoke(ClassLoaderFilter.java:37)
at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:68)
at com.alibaba.dubbo.rpc.filter.EchoFilter.invoke(EchoFilter.java:37)
at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:68)
at com.alibaba.dubbo.rpc.protocol.dubbo.DubboProtocol$1.reply(DubboProtocol.java:98)
at com.alibaba.dubbo.remoting.exchange.support.header.HeaderExchangeHandler.handleRequest(HeaderExchangeHandler.java:96)
at com.alibaba.dubbo.remoting.exchange.support.header.HeaderExchangeHandler.received(HeaderExchangeHandler.java:168)
at com.alibaba.dubbo.remoting.transport.DecodeHandler.received(DecodeHandler.java:50)
at com.alibaba.dubbo.remoting.transport.dispatcher.ChannelEventRunnable.run(ChannelEventRunnable.java:79)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:748)
Caused by: org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: java.lang.IllegalArgumentException: invalid comparison: java.util.Date and java.lang.String
### Cause: java.lang.IllegalArgumentException: invalid comparison: java.util.Date and java.lang.String
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 sun.reflect.GeneratedMethodAccessor135.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:433)
... 45 common frames omitted
Caused by: java.lang.IllegalArgumentException: invalid comparison: java.util.Date and java.lang.String
at org.apache.ibatis.ognl.OgnlOps.compareWithConversion(OgnlOps.java:93)
at org.apache.ibatis.ognl.OgnlOps.isEqual(OgnlOps.java:143)
at org.apache.ibatis.ognl.OgnlOps.equal(OgnlOps.java:802)
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:470)
at org.apache.ibatis.ognl.Ognl.getValue(Ognl.java:434)
at org.apache.ibatis.scripting.xmltags.OgnlCache.getValue(OgnlCache.java:44)
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.apply(MixedSqlNode.java:33)
at org.apache.ibatis.scripting.xmltags.DynamicSqlSource.getBoundSql(DynamicSqlSource.java:41)
at org.apache.ibatis.mapping.MappedStatement.getBoundSql(MappedStatement.java:292)
at org.apache.ibatis.executor.BaseExecutor.query(BaseExecutor.java:134)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:148)
... 50 common frames omitted
在mybatis 3.3.0中对于时间参数进行比较时引入了一个bug. 如果拿传入的时间类型参数与空字符串 ‘’ 进行对比判断则会引发异常. 所以在使用时间和字符串进行比较的代码,只保留非空判断就正常了。
改造前:
AND start_time = ]]> #{startTime}
改造后:
AND start_time = ]]> #{startTime}