mybatis注解方式if标签报错元素内容必须由格式正确的字符数据或标记组成

在使用mybatis的注解方式的时候出现个问题,我需要一个复杂的sql语句,既有if判断又有in语句,刚开始使用mybatis自己的if动态函数的时候完全没问题,代码如下:

@Select({
            "select * ",
            "from order_info ",
            "where if (#{staffId} is not null,staffId=#{staffId},1=1) " ,
            "and if (#{storeId} is not null,storeId=#{storeId},1=1) ",
            "and if (#{start} is not null,dbctime>=#{start},1=1) ",
            "and if (#{end} is not null,dbctime<=#{end},1=1) ",
            "and if (#{status} is not null,status=#{status},1=1) ",
            "and orderId > #{lastMaxIndex} order by orderId asc ",
            "limit #{size}"
    })
    List<OrderInfoPO> getOrderInfoList(@Param("staffId") Long staffId, @Param("storeId") Long shopId, @Param("start") Date start, @Param("end") Date end,
                                       @Param("status") Integer status, @Param("size") int size, @Param("lastMaxIndex") Long lastMaxIndex);

但是后来要改成in条件,而mybatis的in语句总是出问题,就需要用到

<script>

标签来做,第一次写的代码如下:

@Select({
    "<script>",
    "select * ",
    "from order_info ",
    "where 1 = 1 ",
    "<if test='staffId != null'>and staffId=#{staffId}</if> " ,
    "<if test='storeIds != null and storeIds.size() > 0'>and storeId IN ",
    "<foreach item='storeId' collection='storeIds' open='(' separator=',' close=')'>",
    "#{storeId}",
    "</foreach></if> ",
    "<if test='start != null'>and dbctime>=#{start}</if> ",
    "<if test='end != null'>and dbctime<=#{end}</if> ",
    "<if test='status != null'>and status=#{status}</if>",
    "and orderId > #{lastMaxIndex} order by orderId asc ",
    "limit #{size}",
    "</script>"
})
List<OrderInfoPO> getOrderInfoList(@Param("staffId") Long staffId, @Param("storeIds") List<Long> storeIds, @Param("start") Date start, @Param("end") Date end,
                                   @Param("status") Integer status, @Param("size") int size, @Param("lastMaxIndex") Long lastMaxIndex);

然后就报错


Caused by: org.apache.ibatis.builder.BuilderException: Error creating document instance.  Cause: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 373; 元素内容必须由格式正确的字符数据或标记组成。
	at org.apache.ibatis.parsing.XPathParser.createDocument(XPathParser.java:259)
	at org.apache.ibatis.parsing.XPathParser.<init>(XPathParser.java:115)
	at org.apache.ibatis.scripting.xmltags.XMLLanguageDriver.createSqlSource(XMLLanguageDriver.java:51)
	at org.apache.ibatis.builder.annotation.MapperAnnotationBuilder.buildSqlSourceFromStrings(MapperAnnotationBuilder.java:482)
	at org.apache.ibatis.builder.annotation.MapperAnnotationBuilder.getSqlSourceFromAnnotations(MapperAnnotationBuilder.java:465)
	... 62 more

我一直以为是if标签和foreach标签混用的问题,而且我其他地方用了foreach标签是正常的,这个地方不应该有问题,
最后排查了半天是使用了标签之后,><这些符号需要进行转义,加上<![CDATA[]]>就可以了,改成如下:

@Select({
            "<script>",
            "select * ",
            "from order_info ",
            "where 1 = 1 ",
            "<if test='staffId != null'>and staffId=#{staffId}</if> " ,
            "<if test='storeIds != null and storeIds.size() > 0'>and storeId IN ",
            "<foreach item='storeId' index='index' collection='storeIds' open='(' separator=',' close=')'>",
            "#{storeId}",
            "</foreach></if> ",
            "<if test='start != null'>and dbctime<![CDATA[>=]]>#{start}</if> ",
            "<if test='end != null'>and dbctime<![CDATA[<=]]>#{end}</if> ",
            "<if test='status != null'>and status=#{status}</if>",
            "</script>"
    })
    List<OrderInfoPO> getOrderInfoList(@Param("staffId") Long staffId, @Param("storeIds") List<Long> storeIds, @Param("start") Date start, @Param("end") Date end,
                                       @Param("status") Integer status, @Param("size") int size, @Param("lastMaxIndex") Long lastMaxIndex);

就可以了!!!!!!!!查了一下午才发现,气死我了

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值