简体   繁体   中英

MyBatis mapper location

I have problem with MyBatis configuration within two modules. Project structure look like that:

  • DB
    • src
      • main
        • java
          • VideosDAO.class
          • ConnectionFactory.class
          • Videos.class
          • VideosMapper.class
        • resources
          • VideoMapper.xml
          • config.xml
    • pom.xml
  • MIXED
    • src
      • main
        • java
          • ArticlesDAO.class
          • Articles.class
          • ArticlesMapper.class
        • resources
          • ArticlesMapper.xml
    • pom.xml

When I have all stuff in DB module I can connect to database and execute SQL but when I want to use ConnectionFactory.class and config.xml from DB module in MIXED module it is not working.

### Error building SqlSession.
### Cause: org.apache.ibatis.builder.BuilderException: Error creating document 
instance.  Cause: org.xml.sax.SAXParseException; lineNumber: 24; columnNumber: 
24; Premature end of file.

I think problem is in config.xml file within

<mappers>
</mappers>

I've tried all possibilities from: http://www.mybatis.org/mybatis-3/configuration.html#mappers but nothing worked :( Any ideas how config.xml should look like to allow me to use classes from other module and not duplicate them but in the same time use mapper from other module?

Some of my tries:

 <mappers>
    <mapper url="file:///MIXED/src/main/resources/ArticlesMapper.xml"/>
    <mapper resource="VideoMapper.xml"/>
 </mappers>

<mappers>
    <mapper resource="MIXED/src/main/resources/ArticlesMapper.xml"/>
    <mapper resource="VideoMapper.xml"/>
</mappers>

First make sure that your maven MIXED module depends on maven DB module. You need this so that classes and resources are available. Add a dependency into pom.xml of the MIXED module (have a look for example to this question )

After you did this classes and resources of the DB module are in classpath of the MIXED module and you reference them in config.xml the same way to you reference resources from the module itself:

<mappers>
   <mapper resource="VideoMapper.xml"/>
</mappers>

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