MyBatis exception from generated file
I used MyBatis Generator 1.3.1 to create a Mapper.xml file. When MyBatis parses the Mapper file, it throws a BuilderException:
Exception in thread "main" org.apache.ibatis.builder.BuilderException: Unknown element <#comment> in SQL statement.
at org.apache.ibatis.builder.xml.XMLStatementBuilder.parseDynamicTags(XMLStatementBuilder.java:83)
at org.apache.ibatis.builder.xml.XMLStatementBuilder.parseStatementNode(XMLStatementBuilder.java:43)
at org.apache.ibatis.session.Configuration.parseStatementNodes(Configuration.java:513)
at org.apache.ibatis.session.Configuration.buildStatementsForNamespace(Configuration.java:502)
at org.apache.ibatis.session.Configuration.buildStatementsFromId(Configuration.java:467)
at org.apache.ibatis.session.Configuration.getMappedStatement(Configuration.java:391)
at org.apache.ibatis.binding.MapperMethod.setupCommandType(MapperMethod.java:160)
at org.apache.ibatis.binding.MapperMethod.<init>(MapperMethod.java:48)
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:37)
at $Proxy1.selectByExample(Unknown Source)
parseDynamicTags is not recognizing the <#comment> field. The portion of the XML that it is parsing is selectByExample:
<select id="selectByExample" resultMap="BaseResultMap" parameterType="test.model.TblPosStageExample" >
<!-- WARNING - @mbggenerated .. -->
select
<if test="distinct" >
distinct
</if>
<include refid="Base_Column_List" />
from tbl_Pos_Stage
<if test="_parameter != null" >
<include refid="Example_Where_Clause" />
</if>
<if test="orderByClause != null" >
order by ${orderByClause}
</if>
</select>
MyBatis seems to have parsed the first comment and the select statement. It has just parsed a node of type #text, but I am not sure where the #comment comes from.
I have not modified the generated file, and I am puzzled that this has functioned correctly in the past and stopped suddenly.
EDIT: Base_column_list
<sql id="Base_Column_List" >
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Wed Mar 23 08:04:42 EST 2011.
-->
SurrogatePK, businessDate, positionId, busAIdCode, {95 columns deleted for brevity}
marketValueCcy, settledMarketValueCcy
</sql>
And here is the example_where_clause
<sql id="Example_Where_Clause" >
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Wed Mar 23 08:04:42 EST 2011.
-->
<where >
<foreach collection="oredCriteria" item="criteria" separator="or" >
<if test="criteria.valid" >
开发者_运维技巧 <trim prefix="(" suffix=")" prefixOverrides="and" >
<foreach collection="criteria.criteria" item="criterion" >
<choose >
<when test="criterion.noValue" >
and ${criterion.condition}
</when>
<when test="criterion.singleValue" >
and ${criterion.condition} #{criterion.value}
</when>
<when test="criterion.betweenValue" >
and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
</when>
<when test="criterion.listValue" >
and ${criterion.condition}
<foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >
#{listItem}
</foreach>
</when>
</choose>
</foreach>
</trim>
</if>
</foreach>
</where>
</sql>
I have figured out what caused this problem. (It had nothing to do with the myBatis generation process.)
The problem occurred when I added a utility package to my Eclipse build path, which included a third party library that had many old packages. I suspect that the offending package is an old org.apache.xerces.parsers.
I found the error by building the Eclipse project from scratch and adding packages and code, one at a time.
The runtime error (Unknown element <#comment> in SQL statement) could be reproduced by adding the Project and eliminated by removing the Project from the Build Path.
I have the same problem caused by a non-standard XML-parser which I cannot replace. I've opened an issue on the MyBatis issue management and attached a patch. Hope this will be fixed soon.
Edit: As it turns out the problem have been solved in r5388.
精彩评论