简体   繁体   中英

MyBatis configuration

I want to use interface instead of mapper XML file in MyBatis. In the MyBatis configuration file I define mapper <mapper class="aa.B" /> where B contains methods annotated with MyBatis annotations. When I try to create a mapper instance session.getMapper(B.class); I get an error:

org.xml.sax.SAXParseException: Attribute "class" must be declared for element type "mapper".

How can I use interfaces to map SQL statements here?

The exception is occurred by absence of the attribute 'class' in 'mapper' element in DTD.

For example, in MyBatis 3.0.1, 'mapper' doesn't have the 'class' attribute in DTD like this:

<!ATTLIST mapper
resource CDATA #IMPLIED
url CDATA #IMPLIED
>

You need to upgrade your MyBatis library to latest one like 3.1.1.

In the version, the attribute definition of 'mapper' is as follows:

<!ATTLIST mapper
resource CDATA #IMPLIED
url CDATA #IMPLIED
class CDATA #IMPLIED
>

You can find your DTD file in the following path in your MyBatis library:

org/apache/ibatis/builder/xml/mybatis-3-config.dtd

You can't add Mapper Interface via the sqlconfig xml file, you've to use the Java API

Configuration.addMapper(B.class)

The node in sqlconfig xml is for definding sqlmap XMLs, not Mapper Interfaces. Or you could use mybatis-spring, which has support to auto add all the mappers in a said package, to the sqlsessionfactory's configuration.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM