Search code examples
mybatisibatis

How to load mapper xml files with wildcard?


Mybatis(ibatis) can load typeAlliases with package as

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

If so , how can I load mapper xml files also ? I hate I always need to add in mybatis-config.xml for every new xml mapper files as

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

I'd like to use with wildcard "*.xml" as

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

How can I achieve it ?


Solution

  • MyBatis can also load mappers (Interfaces) from a package, see the documentation.

    <mappers>
      <package name="org.mybatis.builder"/>
    </mappers>
    

    Of course this seems to require that you have the interfaces, too, and not just xml files. Don't know of any method to load all the XML files, if you have no Interfaces, though.