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

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

    评论

报告相同问题?

悬赏问题

  • ¥15 如何实验stm32主通道和互补通道独立输出
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题