MyBatis Mapper Locations Configuration

In MyBatis, mapper-locations is an attribute used to configure the path of Mapper interface XML files. By setting up mapper-locations, you can instruct MyBatis where to find the XML files for Mapper interfaces.

There are multiple ways to configure mapper-locations, such as using relative or absolute paths to specify the location of XML files. Here are some common examples of mapper-locations configurations.

  1. Use relative path:
  2. Create a bean called “sqlSessionFactory” of the class “org.mybatis.spring.SqlSessionFactoryBean” with the property “mapperLocations” set to the value “classpath:com/example/mappers/*.xml”.
  3. In the example above, the value of mapper-locations is “classpath:com/example/mappers/*.xml”, indicating that the XML files of Mapper interfaces are located in the com/example/mappers directory.
  4. Use the absolute path:
  5. Create a bean with the ID “sqlSessionFactory” using the class “org.mybatis.spring.SqlSessionFactoryBean” and set the location of the mapper files to “file:/path/to/mappers/*.xml”.
  6. In the above example, the value of mapper-locations is “file:/path/to/mappers/*.xml”, indicating that the XML files for the Mapper interfaces are located in the /path/to/mappers directory.
  7. Match multiple paths using wildcards.
  8. The bean with the id “sqlSessionFactory” is defined using the class “org.mybatis.spring.SqlSessionFactoryBean” and the property “mapperLocations” is set with the value “classpath:com/example/mappers/*/*.xml”.
  9. In the example above, the value of mapper-locations is “classpath:com/example/mappers//.xml”, indicating that the XML file for the Mapper interface is located in any subdirectory within the com/example/mappers directory.

It is important to note that the mapper-locations can be configured with multiple paths, which can be separated by commas or semicolons. If there are multiple paths, MyBatis will search for XML files in the specified order until it finds one.

I hope the above information is helpful to you!

bannerAds