项目背景描述:spring-boot (2.1.5) + mybatis-plus (3.3.1) + jdk8
一、问题描述
错误信息如下:
Caused by: org.apache.ibatis.builder.BuilderException: Error evaluating expression 'ew != null and ew.sqlFirst != null'. Cause: org.apache.ibatis.ognl.OgnlException: sqlFirst
.......
Caused by: org.apache.ibatis.ognl.OgnlException: sqlFirst
........
Caused by: com.baomidou.mybatisplus.core.exceptions.MybatisPlusException: can not use this method for "getSqlFirst"
........
Caused by: org.apache.ibatis.builder.BuilderException: Error evaluating expression 'ew != null and ew.sqlFirst != null'. Cause: org.apache.ibatis.ognl.OgnlException: sqlFirst [com.baomidou.mybatisplus.core.exceptions.MybatisPlusException: can not use this method for "getSqlFirst"] at org.apache.ibatis.scripting.xmltags.OgnlCache.getValue(OgnlCache.java:48) 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.ChooseSqlNode.apply(ChooseSqlNode.java:35) at org.apache.ibatis.scripting.xmltags.MixedSqlNode.lambda$apply$0(MixedSqlNode.java:32) 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:297) at com.baomidou.mybatisplus.core.executor.MybatisCachingExecutor.query(MybatisCachingExecutor.java:88) at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:147) at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:140) 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:497) at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:426) ... 69 common frames omitted Caused by: org.apache.ibatis.ognl.OgnlException: sqlFirst at org.apache.ibatis.ognl.OgnlRuntime.getMethodValue(OgnlRuntime.java:1693) at org.apache.ibatis.ognl.ObjectPropertyAccessor.getPossibleProperty(ObjectPropertyAccessor.java:60) at org.apache.ibatis.ognl.ObjectPropertyAccessor.getProperty(ObjectPropertyAccessor.java:147) at org.apache.ibatis.ognl.OgnlRuntime.getProperty(OgnlRuntime.java:2719) at org.apache.ibatis.ognl.ASTProperty.getValueBody(ASTProperty.java:114) 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.ASTChain.getValueBody(ASTChain.java:141) 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.ASTNotEq.getValueBody(ASTNotEq.java:50) 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:493) at org.apache.ibatis.ognl.Ognl.getValue(Ognl.java:457) at org.apache.ibatis.scripting.xmltags.OgnlCache.getValue(OgnlCache.java:46) ... 85 common frames omitted Caused by: com.baomidou.mybatisplus.core.exceptions.MybatisPlusException: can not use this method for "getSqlFirst" at com.baomidou.mybatisplus.core.toolkit.ExceptionUtils.mpe(ExceptionUtils.java:49) at com.baomidou.mybatisplus.extension.conditions.AbstractChainWrapper.getSqlFirst(AbstractChainWrapper.java:272) 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:497) at org.apache.ibatis.ognl.OgnlRuntime.invokeMethod(OgnlRuntime.java:881) at org.apache.ibatis.ognl.OgnlRuntime.getMethodValue(OgnlRuntime.java:1691) ... 103 common frames omitted
二、问题原因
是我在项目中使用了mybatis-plus里的lambdaQuery()表达式查询的原因,如下是一种错误写法。
public CommResponse<List<PushRuleResponse>> getByEventId(String devModel, String eventId) { List<MessageTemplateDO> messageTemplateDOS = messageTemplateMapper.selectList(lambdaQuery() .eq(MessageTemplateDO::getTemplateCode, request.getTemplateCode())); return CommResponse.ok(PushRuleConvert.INSTANCE.convertResponseList(list)); }
三、解决方案
将lambdaQuery()表达式查询的方法改为QueryWrapper()条件构造器查询方法即可,如下图是正确的写法
private List<PushRuleDO> selectList(String devModel, String eventId){ QueryWrapper<PushRuleDO> queryWrapper = new QueryWrapper<>(); queryWrapper.eq("dev_model", devModel); queryWrapper.eq("event_id", eventId); List<PushRuleDO> list = pushRuleMapper.selectList(queryWrapper); return list; }
具体详细为什么mybatis-plus提供的这个方法不能用的原因还有待进一步查找。
完结!