Mybatis-元素内容必须由格式正确的字符数据或标记组成

2019年10月12日 20:30 · 阅读(133) ·

参考

mybatis异常 :元素内容必须由格式正确的字符数据或标记组成。

问题描述

调用接口时,报下面的错误

  1. [target\classes\mapper\TbeEinvoiceinfoEntityMapper.xml]'; nested exception is org.apache.ibatis.builder.BuilderException: Error creating document instance. Cause: org.xml.sax.SAXParseException; lineNumber: 107; columnNumber: 48; 元素内容必须由格式正确的字符数据或标记组成。

问题分析

查看 TbeEinvoiceinfoEntityMapper.xml

  1. <where>
  2. <if test="1 == 1">
  3. EinvoiceInfo.IsDelete = 0 AND EinvoiceInfo.BillType = 0
  4. </if>
  5. <if test="param.merchantGuid != null and param.merchantGuid != ''">
  6. AND EinvoiceInfo.MerchantGuid = #{param.merchantGuid}
  7. </if>
  8. <if test="param.beginDate != null and param.beginDate != ''">
  9. AND EinvoiceInfo.EinvoiceTime >= #{param.beginDate}
  10. </if>
  11. </where>
  12. <if test="param.amountSort == null and param.amountSort == '' or param.amountSort == 0">
  13. ORDER BY CreateDate DESC
  14. </if>

错误原因:mybatis查询的时候,需要用到运算符 小于号:< 和 大于号: >,在mybatis配置文件里面,这种会被认为是标签,所以解析错误

问题解决

修改 TbeEinvoiceinfoEntityMapper.xml
有判断符号(>,<,>=,<=) 使用 <![CDATA[ ]]> 包围

  1. <where>
  2. <if test="1 == 1">
  3. EinvoiceInfo.IsDelete = 0 AND EinvoiceInfo.BillType = 0
  4. </if>
  5. <if test="param.merchantGuid != null and param.merchantGuid != ''">
  6. AND EinvoiceInfo.MerchantGuid = #{param.merchantGuid}
  7. </if>
  8. <if test="param.beginDate != null and param.beginDate != ''">
  9. <![CDATA[ AND EinvoiceInfo.EinvoiceTime >= #{param.beginDate} ]]>
  10. </if>
  11. </where>
  12. <if test="param.amountSort == null and param.amountSort == '' or param.amountSort == 0">
  13. ORDER BY CreateDate DESC
  14. </if>

测试结果

添加后 <![CDATA[ ]]>,错误消失