mybatis 多mapper xml资源位置

项目使用mybatis作为持久层开发框架, 其配置如下:

<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>
        
        <property name="timeBetweenEvictionRunsMillis">
            <value>3600000value>
        property>
        
        
        <property name="testWhileIdle">
            <value>truevalue>
        property>
        
        <property name="validationQuery">
            <value>select 1 from dualvalue>
        property>
    bean>

    
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        
        <property name="mapperLocations" value="classpath*:com/test/core/mapping/*.xml">property>
    bean>

    
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.test.core.dao" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory">property>
    bean>

    
    <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" />
        
        <property name="mapperLocations">
            <array>
                <value>classpath*:com/test/core/mapping/*.xmlvalue>
                <value>classpath*:com/test2/core/mapper/*.xmlvalue>
            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包中。

你可能感兴趣的:(mybatis)