mybatis mapper xml:元素类型“mapper”的内容必须匹配

时间:2013-04-17 19:16:36

标签: java database database-connection ibatis mybatis

我很难让简单的mybatis文件正常工作。我有这个文件:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
    PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="truthtree.model.mysql.UserMapper">

    <typeAlias alias="User" type="truthtree.model.mysql.User" />

    <select id="getAllUsers" resultType="User">
        select * from User
    </select>

    <select id="findUserByID" resultType="User">
        select *
        from User
        where id=#{id,javaType=int}
    </select>

    <select id="findByNameAndPassword" resultType="User">
        select *
        from User
        where name = #{name,javaType=String}
          and password = #{password,javaType=String}
    </select>

</mapper>

我得到以下异常:

引起:org.xml.sax.SAXParseException:元素类型“mapper”的内容必须匹配“(cache-ref | cache | resultMap * | parameterMap * | sql * | insert * | update * | delete * |选择*)+”。

这令人困惑,因为我肯定有一些东西在里面。任何想法在这里可能是错的?谢谢!

1 个答案:

答案 0 :(得分:3)

这里你错了的是typeAlias元素没有进入mapper文件。它不在mybatis-3-mapper.dtd中。 typeAlias元素必须包含在mybatis-config文件中(typeAliases元素内,因为mybatis-3-config.dtd中很明显:

<!ELEMENT typeAliases (typeAlias*,package*)>

<!ELEMENT typeAlias EMPTY>
<!ATTLIST typeAlias
type CDATA #REQUIRED
alias CDATA #IMPLIED
>