ssm mybatis 配置多个 mapper 目录

时间:2024-11-14 16:02:19

通配符配置

比如目录的结构如下:
com/test/web/user/mapper
com/test/web/order/mapper
com/test/web//mapper
<!-- 扫描  -->   
    <bean id="sqlSessionFactory" class="">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation"  value="classpath:spring/"/>  
        <property name="mapperLocations" value="classpath*:com/xxxcompany/xxxproject/*/mapper/*.xml" />
    </bean>

<!-- 扫描 xxxMapper -->
    <bean id="daoScan" class="">
        <property name="basePackage" value=".*.mapper" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
    </bean>

除了上面这种目录结构一致的方式,我们还可以使用下面的配置方式,这个意思也就是说只要是符合mapper* 这样的命名规则的包都会被扫描到

<bean id="sqlSessionFactory" class="">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation"  value="classpath:"/>  
        <property name="mapperLocations" value="classpath*:com/dada/**/*"/> 
    </bean>

    <!-- 配置扫描器 -->
    <bean class="">
        <property name="basePackage" value="*"/>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
    </bean>