简体   繁体   中英

How to add wildcarded mapper paths in MyBatis configuration?

In MyBatis documentation I see only example of adding mappers by exact name.

<mappers>
  <mapper resource="org/mybatis/builder/AuthorMapper.xml"/>
  <mapper resource="org/mybatis/builder/BlogMapper.xml"/>
  <mapper resource="org/mybatis/builder/PostMapper.xml"/>
</mappers>

Specifying each mapper name looks like a redundant verbosity, and I am wondering if there is a way to do the same with wildcards like this:

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

In mybatis-spring-boot-starter they have a property mybatis.mapper-locations that could be used as follows:

mybatis.mapper-locations=classpath:org/mybatis/builder/*Mapper.xml

The only disadvantage is that it involves tons code and Spring autoconfiguring magic under the hood.

Is it possible to add wildcarded mappers path into config xml using MyBatis itself (without Spring dependencies)?

or is there a reason why this useful feature is not available?

From that same documentation page - you can register a package and all interfaces in the package will be registered. This is far less verbose than registering mappers individually:

<!-- Register all interfaces in a package as mappers -->
<mappers>
  <package name="org.mybatis.builder"/>
</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