mappers

Now that the behavior of MyBatis is configured with the above configuration elements, we’re ready to define our mapped SQL statements. But first, we need to tell MyBatis where to find them. Java doesn’t really provide any good means of auto-discovery in this regard, so the best way to do it is to simply tell MyBatis where to find the mapping files. You can use classpath relative resource references, fully qualified url references (including file:/// URLs), class names or package names. For example:

  1. <!-- Using classpath relative resources -->
  2. <mappers>
  3. <mapper resource="org/mybatis/builder/AuthorMapper.xml"/>
  4. <mapper resource="org/mybatis/builder/BlogMapper.xml"/>
  5. <mapper resource="org/mybatis/builder/PostMapper.xml"/>
  6. </mappers>
  1. <!-- Using url fully qualified paths -->
  2. <mappers>
  3. <mapper url="file:///var/mappers/AuthorMapper.xml"/>
  4. <mapper url="file:///var/mappers/BlogMapper.xml"/>
  5. <mapper url="file:///var/mappers/PostMapper.xml"/>
  6. </mappers>
  1. <!-- Using mapper interface classes -->
  2. <mappers>
  3. <mapper class="org.mybatis.builder.AuthorMapper"/>
  4. <mapper class="org.mybatis.builder.BlogMapper"/>
  5. <mapper class="org.mybatis.builder.PostMapper"/>
  6. </mappers>
  1. <!-- Register all interfaces in a package as mappers -->
  2. <mappers>
  3. <package name="org.mybatis.builder"/>
  4. </mappers>

These statement simply tell MyBatis where to go from here. The rest of the details are in each of the SQL Mapping files, and that’s exactly what the next section will discuss.