如何使用通配符加载mapper xml文件?

时间:2016-03-10 03:28:01

标签: mybatis ibatis

Mybatis(ibatis)可以typeAlliases加载package作为

<typeAliases>
   <package name="com.my.example.bean"/> 
</typeAliases>

如果是这样,我如何加载mapper xml文件呢?我讨厌我总是需要为每个新的xml映射器文件添加mybatis-config.xml

<mappers>
    <mapper resource="mybatis/mapper/A.xml" />
    <mapper resource="mybatis/mapper/B.xml" />
    <mapper resource="mybatis/mapper/C.xml" />
    <mapper resource="mybatis/mapper/D.xml" />
  .......
</mappers>

我想使用通配符“* .xml”作为

<mappers>
    <mapper resource="mybatis/mapper/*.xml" />
</mappers>

我怎样才能实现它?

2 个答案:

答案 0 :(得分:3)

MyBatis还可以从包中加载映射器(接口),请参阅documentation

 <?xml version="1.0" encoding="UTF-8"?>
 <soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
  <soapenv:Body>
   <dbs:DBSelectResult xmlns:dbs="http://esb.agava.net/mediators/db/dbselect">
   <dbs:row>
        <dbs:NAME name="NAME">100000007</dbs:NAME>
        <dbs:TITLE__C name="TITLE__C">test</dbs:TITLE__C>
        <dbs:SHORT_DESCRIPTION__C name="SHORT_DESCRIPTION__C" />
     </dbs:row>
     <dbs:row>
        <dbs:NAME name="NAME">100000005</dbs:NAME>
        <dbs:TITLE__C name="TITLE__C">TEST4</dbs:TITLE__C>
        <dbs:SHORT_DESCRIPTION__C name="SHORT_DESCRIPTION__C">TEST4</dbs:SHORT_DESCRIPTION__C>
     </dbs:row>
   </dbs:DBSelectResult>
   </soapenv:Body>
  </soapenv:Envelope>

当然,这似乎要求您拥有接口,而不仅仅是xml文件。但是,如果没有接口,请不要知道加载所有XML文件的任何方法。

答案 1 :(得分:3)

我今天遇到了同样的问题,我用通配符对其进行了解决。

是否有用于配置sqlSessionFactory bean的xml文件。添加<property name="mapperLocations" value="classpath:sqlmap/**/*Mapper.xml"/>之类的属性,

classpath:sqlmap/**/*Mapper.xml是您mapper.xml的位置,并删除所有映射器资源。