关于mapper.java与mapper.xml是否在一个文件夹中

先看结果:这样配置就可以任意存放你的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 "&lt;sqlmapper&gt;" 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文件位置

 

 

 

 

 

 

转载于:https://my.oschina.net/u/3418748/blog/1031015

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值