先看结果:这样配置就可以任意存放你的xml文件;
<!-- 加载mapper.xml -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" />
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations">
<list>
<value>classpath:mybatis/mapper/*/*Mapper.xml</value>
<value>classpath:mybatis/dao/*/*Dao.xml</value>
</list>
</property>
</bean>
这其实也是源码里就有的,点开:org.mybatis.spring.SqlSessionFactoryBean
进入:SqlSessionFactoryBean.java 类
搜索setMapperLocations方法,setMapperLocations 这就是设置mapperxml文件位置的。
这是源码里的注释:写的蛮清晰的。
/**
* Set locations of MyBatis mapper files that are going to be merged into the {@code SqlSessionFactory}
* configuration at runtime.
*
* This is an alternative to specifying "<sqlmapper>" entries in an MyBatis config file.
* This property being based on Spring's resource abstraction also allows for specifying
* resource patterns here: e.g. "classpath*:sqlmap/*-mapper.xml".
*/
再看方法:
public void setMapperLocations(Resource[] mapperLocations) {
this.mapperLocations = mapperLocations;
}
是一个Resource[ ]数组,点击查看下,
/**
* Interface for a resource descriptor that abstracts from the actual
* type of underlying resource, such as a file or class path resource.
*
* <p>An InputStream can be opened for every resource if it exists in
* physical form, but a URL or File handle can just be returned for
* certain resources. The actual behavior is implementation-specific.
*/
public interface Resource extends InputStreamSource {
Resource 继承了 InputStreamSource 接口;
这下清晰了吧,所以配置文件 spring-dao.xml 配置文件就是这样。
使用List 配置多个mapper.xml文件位置