具体问题
nested exception is org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.builder.BuilderException: Error evaluating expression '_clause.operator.alias'. Cause: org.apache.ibatis.ognl.OgnlException: source is null for getProperty(null, "operator")] with root cause [org.apache.juli.logging.DirectJDKLog.log(DirectJDKLog.java:175)]
org.apache.ibatis.ognl.OgnlException: source is null for getProperty(null, "operator")
at org.apache.ibatis.ognl.OgnlRuntime.getProperty(OgnlRuntime.java:3226) ~[mybatis-3.5.4.jar:3.5.4]
at org.apache.ibatis.ognl.ASTProperty.getValueBody(ASTProperty.java:114) ~[mybatis-3.5.4.jar:3.5.4]
at org.apache.ibatis.ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:212) ~[mybatis-3.5.4.jar:3.5.4]
at org.apache.ibatis.ognl.SimpleNode.getValue(SimpleNode.java:258) ~[mybatis-3.5.4.jar:3.5.4]
at org.apache.ibatis.ognl.ASTChain.getValueBody(ASTChain.java:141) ~[mybatis-3.5.4.jar:3.5.4]
at org.apache.ibatis.ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:212) ~[mybatis-3.5.4.jar:3.5.4]
at org.apache.ibatis.ognl.SimpleNode.getValue(SimpleNode.java:258) ~[mybatis-3.5.4.jar:3.5.4]
at org.apache.ibatis.ognl.Ognl.getValue(Ognl.java:560) ~[mybatis-3.5.4.jar:3.5.4]
at org.apache.ibatis.ognl.Ognl.getValue(Ognl.java:524) ~[mybatis-3.5.4.jar:3.5.4]
at org.apache.ibatis.scripting.xmltags.OgnlCache.getValue(OgnlCache.java:46) ~[mybatis-3.5.4.jar:3.5.4]
at org.apache.ibatis.scripting.xmltags.TextSqlNode$BindingTokenParser.handleToken(TextSqlNode.java:77) ~[mybatis-3.5.4.jar:3.5.4]
at org.apache.ibatis.parsing.GenericTokenParser.parse(GenericTokenParser.java:77) ~[mybatis-3.5.4.jar:3.5.4]
at org.apache.ibatis.scripting.xmltags.TextSqlNode.apply(TextSqlNode.java:51) ~[mybatis-3.5.4.jar:3.5.4]
at org.apache.ibatis.scripting.xmltags.DynamicSqlSource.getBoundSql(DynamicSqlSource.java:39) ~[mybatis-3.5.4.jar:3.5.4]
at org.apache.ibatis.mapping.MappedStatement.getBoundSql(MappedStatement.java:297) ~[mybatis-3.5.4.jar:3.5.4]
at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:81) ~[mybatis-3.5.4.jar:3.5.4]
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:147) ~[mybatis-3.5.4.jar:3.5.4]
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:140) ~[mybatis-3.5.4.jar:3.5.4]
at sun.reflect.GeneratedMethodAccessor142.invoke(Unknown Source) ~[?:?]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_152]
at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_152]
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:426) ~[mybatis-spring-2.0.4.jar:2.0.4]
at com.sun.proxy.$Proxy147.selectList(Unknown Source) ~[?:?]
at org.mybatis.spring.SqlSessionTemplate.selectList(SqlSessionTemplate.java:223) ~[mybatis-spring-2.0.4.jar:2.0.4]
at org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:147) ~[mybatis-3.5.4.jar:3.5.4]
at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:80) ~[mybatis-3.5.4.jar:3.5.4]
at org.apache.ibatis.binding.MapperProxy$PlainMethodInvoker.invoke(MapperProxy.java:144) ~[mybatis-3.5.4.jar:3.5.4]
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:85) ~[mybatis-3.5.4.jar:3.5.4]
at com.sun.proxy.$Proxy322.testGetHumanNamePage(Unknown Source) ~[?:?]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_152]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_152]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_152]
相关代码
1、主要是repository层定义,然后简单的使用
首先是简单的命名,然后运行方法:
testHumanRepository.testGetHumanNamePage();
就出现了上面的错误。
分析问题
1、查看到ibatis的底层解析,在类org.apache.ibatis.scripting.xmltags.DynamicSqlSource的方法getBoundSql中发现
可以看到这里是:
select name from sys_human where XXX
可是我逻辑中调用的方法是:
哪里有select name from sys_human,唯一有这个的方法是:
所以排查觉得是重名导致的问题,ibatis以为我要调用的是第一个方法;我们在使用的时候,JVM根据重载机制知道我们调用的是第三个方法,但是ibatis是根据代理去找方法的,这三个会直接映射到第一个出现的方法去,也就导致这个错误的出现;
解决方法
1、将方法名区分开,添加1,2,3后缀
2、因为第一个方法中使用了@clause()方法类表达式,所以要使用<script>标签包裹起来
后记
1、我们使用ibatis的时候,认为框架也会像jvm一样,能使用重载函数或重写函数之类的特性,但是最好要经过验证,否则就会出现与想象中运行不一致的问题;比如以下情况,比较经典,列出来供大家参考:
经典错误
假如改成这样,是可以正常运行的,不会报异常的:
调用是这样调用的:
第一个和第三个重名,其实本质上就是把第一个的语句用<script>包裹,运行起来至少是正常的;
但是正如前面所截图,运行的语句实际上是:
select name from sys_human where XXX
所以就算没有报异常,返回值也不应该是 List<SysHuman> 或者说就算是SysHuman,那么也只填充了 SysHuman 的name属性;验证之后为:
返回值是List<String>,不是 List<SysHuman> ,也就是本质上是调用第一个方法:
返回值是List<String>;
为什么没有报类型转换的错误?按道理返回类型也不一样啊。
那是因为Java的类型擦除机制,返回值都是泛型,都可以插入Object,所以没有报错。