210. 매퍼 (mappers)

2012. 4. 23. 08:39공부/MYBATIS

지금까지 거론된 설정 요소에 의해 마이바티스의 행동이 설정되었고, 매핑된 SQL 구문을 정의할 준비가 되었어. 하지만 먼저, 어디에 그것들을 정의할지 마이바티스에 알려줄 필요가 있지. 자바는 이것과 관련된 어떠한 좋은 도구도 제공하지 않으므로, 가장 좋은 방법은 매핑파일을 어디서 찾을 수 있는지 마이바티스에게 간단하게 말하는 거야. 상대경로의 리소스 참조 클래스 패스를 사용할 수도 있고, 무자 그대로의 풀 url 참조(file:// 를 포함한 URL)를 사용할 수도 있어. 예를 들면

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

이러한 구문들이 어디로 찾아가야할 지 마이바티스에게 알려주는 방법들이야. 세부사항의 나머지 부분은 각각의 SQL 매핑파일의 내부에 있고, 다음 장에서 이에 대해 논의할 것이야