汉堡里没有面包 2020-06-21 00:42 采纳率: 0%
浏览 3633

MyBatis中if标签 判断数组大小时使用size()报错;但isEmpty()却能正常执行

使用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

求指点。。。

  • 写回答

2条回答 默认 最新

  • weixin_42197885 2023-01-12 16:28
    关注

    1=0
    在于:ids.size()==0

    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效