简体   繁体   English

MyBatis映射器位置

[英]MyBatis mapper location

I have problem with MyBatis configuration within two modules. 我在两个模块中的MyBatis配置有问题。 Project structure look like that: 项目结构如下所示:

  • DB D B
    • src src
      • main 主要
        • java 爪哇
          • VideosDAO.class VideosDAO.class
          • ConnectionFactory.class ConnectionFactory.class
          • Videos.class Videos.class
          • VideosMapper.class VideosMapper.class
        • resources 资源
          • VideoMapper.xml VideoMapper.xml
          • config.xml config.xml
    • pom.xml pom.xml
  • MIXED 混合的
    • src src
      • main 主要
        • java 爪哇
          • ArticlesDAO.class 文章类
          • Articles.class Articles.class
          • ArticlesMapper.class ArticlesMapper.class
        • resources 资源
          • ArticlesMapper.xml ArticlesMapper.xml
    • pom.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. 当我在DB模块中拥有所有东西时,我可以连接到数据库并执行SQL,但是当我想在MIXED模块中使用DB模块中的ConnectionFactory.class和config.xml时,它将无法正常工作。

### 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 我认为问题出在config.xml文件中

<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? 我已经尝试了以下所有方法: http : //www.mybatis.org/mybatis-3/configuration.html#mappers但没有任何效果:(任何关于config.xml应该如何允许我使用其他模块类的想法而不是复制它们,但同时使用其他模块中的映射器?

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. 首先,确保您的Maven MIXED模块依赖于Maven DB模块。 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 ) 将依赖项添加到MIXED模块的pom.xml中(例如查看此问题

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: 完成此操作后, DB模块的类和资源位于MIXED模块的类路径中,您可以在config.xml引用它们,就像引用来自模块本身的资源一样:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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