Search code examples
javadatabasedatabase-connectionibatismybatis

mybatis mapper xml: The content of element type "mapper" must match


I'm having a hard time getting my simple mybatis file to work. I have this file:

<?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>

I get the following exception:

Caused by: org.xml.sax.SAXParseException: The content of element type "mapper" must match "(cache-ref|cache|resultMap*|parameterMap*|sql*|insert*|update*|delete*|select*)+".

Which is confusing because I definitely have some s in there. Any ideas what could be wrong here? Thanks!


Solution

  • What you have wrong here is that the typeAlias element does not go into the mapper file. It's not present in the mybatis-3-mapper.dtd. The typeAlias element has to be included in the mybatis-config file (inside the typeAliases element, as it is clear in the mybatis-3-config.dtd:

    <!ELEMENT typeAliases (typeAlias*,package*)>
    
    <!ELEMENT typeAlias EMPTY>
    <!ATTLIST typeAlias
    type CDATA #REQUIRED
    alias CDATA #IMPLIED
    >