来自生成文件的MyBatis异常

时间:2011-03-23 15:24:26

标签: java generator mybatis

我使用MyBatis Generator 1.3.1创建了一个Mapper.xml文件。当MyBatis解析Mapper文件时,它会抛出一个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无法识别&lt; #views&gt;领域。它正在解析的XML部分是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似乎已经解析了第一条评论和select语句。它刚刚解析了#text类型的节点,但我不确定#comment的来源。

我没有修改生成的文件,我很困惑,这在过去已经正常运行并且突然停止了。


编辑: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>

这是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>

2 个答案:

答案 0 :(得分:1)

我已经弄明白是什么导致了这个问题。 (它与myBatis生成过程无关。)

当我将一个实用程序包添加到我的Eclipse构建路径时发生了问题,该路径包含一个包含许多旧包的第三方库。我怀疑违规包是旧的org.apache.xerces.parsers。

我通过从头开始构建Eclipse项目并一次添加一个包和代码来发现错误。

可以通过添加项目来重现运行时错误(SQL语句中的未知元素&lt; #project&gt;),并通过从构建路径中删除项目来消除。

答案 1 :(得分:1)

我遇到了由非标准XML解析器引起的同样问题,我无法替换它。 我在MyBatis issue management上打开了一个问题并附上了补丁。希望这很快就会解决。

编辑:事实证明问题已在r5388中解决。