【mybatis】 “if“ 相关联的 “test“ 属性值不能包含 ‘<‘ 字符
程序员文章站
2022-03-31 23:26:27
...
前言
- mybatis 3.4.6
错误代码
mybatis XML mapper 错误
<select id="selectxxxList" parameterType="map" resultMap="xxxResult">
...
<if test="statusList != null and statusList.size()>0" >
and status in
<foreach item="item" collection="statusList" open="(" separator="," close=")">
#{item}
</foreach>
</if>
<if test="statusList == null or statusList.size() <= 0" >
and status is null
</if>
</select>
错误信息
Caused by: org.springframework.core.NestedIOException: Failed to parse mapping resource: 'file [D:\xxx\xxxMapper.xml]'; nested exception is org.apache.ibatis.builder.BuilderException: Error creating document instance. Cause: org.xml.sax.SAXParseException; lineNumber: 47; columnNumber: 69; 与元素类型 "if" 相关联的 "test" 属性值不能包含 '<' 字符。
at org.mybatis.spring.SqlSessionFactoryBean.buildSqlSessionFactory(SqlSessionFactoryBean.java:523)
at org.mybatis.spring.SqlSessionFactoryBean.afterPropertiesSet(SqlSessionFactoryBean.java:380)
at org.mybatis.spring.SqlSessionFactoryBean.getObject(SqlSessionFactoryBean.java:547)
at com.office.framework.config.MyBatisConfig.sqlSessionFactory(MyBatisConfig.java:106)
at com.office.framework.config.MyBatisConfig$$EnhancerBySpringCGLIB$$e8817d46.CGLIB$sqlSessionFactory$0(<generated>)
at com.office.framework.config.MyBatisConfig$$EnhancerBySpringCGLIB$$e8817d46$$FastClassBySpringCGLIB$$2a3dbd36.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244)
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:363)
at com.office.framework.config.MyBatisConfig$$EnhancerBySpringCGLIB$$e8817d46.sqlSessionFactory(<generated>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)
... 102 common frames omitted
Caused by: org.apache.ibatis.builder.BuilderException: Error creating document instance. Cause: org.xml.sax.SAXParseException; lineNumber: 47; columnNumber: 69; 与元素类型 "if" 相关联的 "test" 属性值不能包含 '<' 字符。
at org.apache.ibatis.parsing.XPathParser.createDocument(XPathParser.java:259)
at org.apache.ibatis.parsing.XPathParser.<init>(XPathParser.java:125)
at org.apache.ibatis.builder.xml.XMLMapperBuilder.<init>(XMLMapperBuilder.java:78)
at org.mybatis.spring.SqlSessionFactoryBean.buildSqlSessionFactory(SqlSessionFactoryBean.java:520)
... 115 common frames omitted
Caused by: org.xml.sax.SAXParseException: 与元素类型 "if" 相关联的 "test" 属性值不能包含 '<' 字符。
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:203)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(ErrorHandlerWrapper.java:177)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:400)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:327)
at com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(XMLScanner.java:1472)
at com.sun.org.apache.xerces.internal.impl.XMLScanner.scanAttributeValue(XMLScanner.java:944)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanAttribute(XMLDocumentFragmentScannerImpl.java:1548)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanStartElement(XMLDocumentFragmentScannerImpl.java:1315)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2784)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:602)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:505)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:841)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:770)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:243)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:339)
at org.apache.ibatis.parsing.XPathParser.createDocument(XPathParser.java:257)
... 118 common frames omitted
分析
上述错误信息可以定位问题为:
- mybatis 的 mapper xml 文件有错误
- xml 文件读取错误
- 特殊字符
<
不能正确 处理
嗯,既然如此,将特殊字符转义。
ps:对于xml的特殊字符有两种处理方法,一个是转移,一个是用<![CDATA[]]>
包裹。上面的问题只能转义。
XML转义字符
&(逻辑与) &
<(小于) <
>(大于) >
"(双引号) "
'(单引号) '
举例:
SELECT * FROM user WHERE age <= 30 AND age >= 18
改正:
<select id="selectxxxList" parameterType="map" resultMap="xxxResult">
...
<if test="statusList != null and statusList.size() > 0" >
and status in
<foreach item="item" collection="statusList" open="(" separator="," close=")">
#{item}
</foreach>
</if>
<if test="statusList == null or statusList.size() <= 0" >
and status is null
</if>
</select>