使用ids.isEmpty()时正常:
<select id="queryBlogForEach" parameterType="map" resultType="Blog">
select * from blog
where
<if test="ids!=null and !ids.isEmpty()">
id in
<foreach item="id" index="index" collection="ids"
open="(" separator="," close=")">
#{id}
</foreach>
</if>
<if test="ids!=null and ids.isEmpty()">1=0</if>
</select>
使用ids.size()报错:
<select id="queryBlogForEach" parameterType="map" resultType="Blog">
select * from blog
where
<if test="ids!=null and ids.size()>0">
id in
<foreach item="id" index="index" collection="ids"
open="(" separator="," close=")">
#{id}
</foreach>
</if>
<if test="ids!=null and ids.size()=0">1=0</if>
</select>
错误信息如下:
org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: org.apache.ibatis.builder.BuilderException: Error evaluating expression 'ids!=null and ids.size()=0'. Cause: org.apache.ibatis.ognl.InappropriateExpressionException: Inappropriate OGNL expression: size()
### Cause: org.apache.ibatis.builder.BuilderException: Error evaluating expression 'ids!=null and ids.size()=0'. Cause: org.apache.ibatis.ognl.InappropriateExpressionException: Inappropriate OGNL expression: size()
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:149)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:140)
at org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:147)
at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:80)
at org.apache.ibatis.binding.MapperProxy$PlainMethodInvoker.invoke(MapperProxy.java:152)
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:85)
at com.sun.proxy.$Proxy9.queryBlogForEach(Unknown Source)
at com.gud.dao.BlogMapperTest.queryBlogForEach(BlogMapperTest.java:92)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)
Caused by: org.apache.ibatis.builder.BuilderException: Error evaluating expression 'ids!=null and ids.size()=0'. Cause: org.apache.ibatis.ognl.InappropriateExpressionException: Inappropriate OGNL expression: size()
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.MixedSqlNode.lambda$apply$0(MixedSqlNode.java:32)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1540)
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:305)
at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:87)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:147)
... 32 more
Caused by: org.apache.ibatis.ognl.InappropriateExpressionException: Inappropriate OGNL expression: size()
at org.apache.ibatis.ognl.SimpleNode.setValueBody(SimpleNode.java:324)
at org.apache.ibatis.ognl.SimpleNode.evaluateSetValueBody(SimpleNode.java:220)
at org.apache.ibatis.ognl.SimpleNode.setValue(SimpleNode.java:308)
at org.apache.ibatis.ognl.ASTChain.setValueBody(ASTChain.java:227)
at org.apache.ibatis.ognl.SimpleNode.evaluateSetValueBody(SimpleNode.java:220)
at org.apache.ibatis.ognl.SimpleNode.setValue(SimpleNode.java:308)
at org.apache.ibatis.ognl.ASTAnd.setValueBody(ASTAnd.java:83)
at org.apache.ibatis.ognl.SimpleNode.evaluateSetValueBody(SimpleNode.java:220)
at org.apache.ibatis.ognl.SimpleNode.setValue(SimpleNode.java:308)
at org.apache.ibatis.ognl.ASTAssign.getValueBody(ASTAssign.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.Ognl.getValue(Ognl.java:560)
at org.apache.ibatis.ognl.Ognl.getValue(Ognl.java:524)
at org.apache.ibatis.scripting.xmltags.OgnlCache.getValue(OgnlCache.java:46)
... 41 more
求指点。。。