mybatis 多mapper xml资源位置

来源:互联网 发布:阿里云和亚马逊云 编辑:程序博客网 时间:2024/06/05 07:38
项目使用mybatis作为持久层开发框架, 其配置如下:
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"    xmlns:context="http://www.springframework.org/schema/context"    xmlns:mvc="http://www.springframework.org/schema/mvc"    xmlns:tx="http://www.springframework.org/schema/tx"    xsi:schemaLocation="http://www.springframework.org/schema/beans                          http://www.springframework.org/schema/beans/spring-beans-3.1.xsd                          http://www.springframework.org/schema/context                          http://www.springframework.org/schema/context/spring-context-3.1.xsd                          http://www.springframework.org/schema/mvc                          http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd                        http://www.springframework.org/schema/tx                         http://www.springframework.org/schema/tx/spring-tx.xsd">    <!-- 自动扫描 -->    <context:component-scan base-package="com.test.*" />    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"        destroy-method="close">        <property name="driverClassName" value="${driver}" />        <property name="url" value="${url}" />        <property name="username" value="${username}" />        <property name="password" value="${password}" />        <!-- 初始化连接大小 -->        <property name="initialSize" value="${initialSize}"></property>        <!-- 连接池最大数量 -->        <property name="maxActive" value="${maxActive}"></property>        <!-- 连接池最大空闲 -->        <property name="maxIdle" value="${maxIdle}"></property>        <!-- 连接池最小空闲 -->        <property name="minIdle" value="${minIdle}"></property>        <!-- 获取连接最大等待时间 -->        <property name="maxWait" value="${maxWait}"></property>        <!-- 启动connection校验定时器,定时器运行时间间隔就是timeBetweenEvictionRunsMillis的值.        默认为-1,表示不启动定时器,这里设定为1小时,只要小于mysql的wait_timeout就可以了 -->        <property name="timeBetweenEvictionRunsMillis">            <value>3600000</value><!--1 hours-->        </property>        <!--          <property name="minEvictableIdleTimeMillis">           <value>20000</value>          </property>        -->        <!-- testWhileIdle: true,表示检查idle的connection,false为不检查 -->        <property name="testWhileIdle">            <value>true</value>        </property>        <!-- 用于检查connection的sql语句. -->        <property name="validationQuery">            <value>select 1 from dual</value>        </property>    </bean>    <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">        <property name="dataSource" ref="dataSource" />        <!-- 自动扫描mapping.xml文件 -->        <property name="mapperLocations" value="classpath*:com/test/core/mapping/*.xml"></property>    </bean>    <!-- DAO接口所在包名,Spring会自动查找其下的类 -->    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">        <property name="basePackage" value="com.test.core.dao" />        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>    </bean>    <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->    <bean id="transactionManager"        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">        <property name="dataSource" ref="dataSource" />    </bean>    <!-- 注册事务 -->    <tx:annotation-driven transaction-manager="transactionManager" /></beans>

可以看到一般xml映射文件放在统一的一个目录下, bean sqlSessionFactory 的属性mapperLocations指定了value 为classpath*:com/test/core/mapping/*.xml
如此程序将在com.test.core.mapping包下找所有的xml文件,进行解析。

现在,要增加一个解析目录,
看org.mybatis.spring.SqlSessionFactoryBean源码可以发现其中的属性

private Resource[] mapperLocations; 

为一个数组类型,所以我们可以将bean sqlSessionFactory 修改如下:

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">        <property name="dataSource" ref="dataSource" />        <!-- 自动扫描mapping.xml文件 -->        <property name="mapperLocations">            <array>                <value>classpath*:com/test/core/mapping/*.xml</value>                <value>classpath*:com/test2/core/mapper/*.xml</value>            </array>        </property>    </bean>

如此,程序将去项目下以及所有的jar下查找com.test.core.mapping以及com.test2.core.mapper包下的所有xml。
写classpath* 而不写classpath 是因为项目的xml文件在jar包中,可以理解成com.test.core.mapping以及com.test2.core.mapper是在不同的jar包中。

原创粉丝点击