Mybatis用MapperScannerConfigurer,使用自定义注解实现多数据源配置

借鉴:https://www.cnblogs.com/jasonboren/p/10598083.html

一:MapperScannerConfigurer类在spring配置文件中可以配置以下几个属性:

1.basePackage:用于指定映射接口文件的包路径,当需要扫描多个包时使用逗号或分号隔开。

2.annotationClass:指定了要扫描的注解名称,只有被注解标示的类才会被配置为映射器。

3.markerInterface:指定创建映射器的接口。

4.sqlSessionFactoryBeanName:指定在spring中定义的sqlSessionFactory的bean名称。

5.sqlSessionTemplateBeanName:指定在spring中定义的sqlSessionTemplate的bean名称。如果定义此属性,那么sqlSessionFactoryBeanName将不起作用。

二:datasource.xml文件配置多数据源:

assets-platform-datasource.xml:配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx.xsd">

    <bean id="assetPlatformDatasource"
          class="com.alibaba.druid.pool.DruidDataSource"
          init-method="init"
          destroy-method="close">
        <property name="url" value="${datasource.assets-platform.url}"/>
        <property name="username" value="${datasource.assets-platform.username}"/>
        <property name="password" value="${datasource.assets-platform.password}"/>
        <property name="DriverClassName" value="${jdbc.driver}"/>
        <!-- 配置初始化大小、最小、最大 -->
        <property name="initialSize" value="${jdbc.initialPoolSize.medium}"/>
        <property name="minIdle" value="${jdbc.minPoolSize.medium}"/>
        <property name="maxActive" value="${jdbc.maxPoolSize.medium}"/>
        <!-- 配置获取连接等待超时的时间 -->
        <!--
        <property name="maxWait" value="${jdbc.maxWaitTime}"/>
        -->
        <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
        <property name="timeBetweenEvictionRunsMillis" value="${jdbc.idleConnectionTestPeriod}"/>
        <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
        <property name="minEvictableIdleTimeMillis" value="${jdbc.maxIdleTime}"/>
        <property name="validationQuery" value="SELECT 'x'"/>
        <property name="testWhileIdle" value="true"/>
        <property name="testOnBorrow" value="false"/>
        <property name="testOnReturn" value="false"/>
        <!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
        <property name="poolPreparedStatements" value="false"/>
        <property name="maxPoolPreparedStatementPerConnectionSize" value="20"/>
        <!-- 配置监控统计拦截的filters -->
        <property name="filters" value="stat,log4j"/>
        <property name="timeBetweenLogStatsMillis" value="60000"/>
        <property name="connectionProperties"
                  value="druid.stat.logSlowSql=true;druid.stat.slowSqlMillis=500;druid.stat.mergeSql=true"/>
    </bean>

    <bean id="assetPlatformTxManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="assetPlatformDatasource"/>
    </bean>
    <bean id="assetPlatformTxTemplate" class="org.springframework.transaction.support.TransactionTemplate">
        <constructor-arg name="transactionManager" ref="assetPlatformTxManager"/>
    </bean>
    <tx:annotation-driven transaction-manager="assetPlatformTxManager" proxy-target-class="true"/>
    <bean id="assetPlatformSqlSessionFactory" name="assetPlatformSqlSessionFactory"
          class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="assetPlatformDatasource"/>
        <property name="configLocation" value="classpath:mybatis/mybatis-config.xml"/>
        <property name="mapperLocations" value="classpath*:mapper/*/*.xml"/>
        <property name="transactionFactory">
            <bean class="org.mybatis.spring.transaction.SpringManagedTransactionFactory"/>
        </property>
    </bean>

    <bean name="assetPlatformSqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg name="sqlSessionFactory" ref="assetPlatformSqlSessionFactory"/>
    </bean>

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="annotationClass" value="com.xxxxx.AssetsPlatformMapper"/>
        <property name="basePackage" value="com.xxxxx.assets.dal"/>
        <property name="sqlSessionFactoryBeanName" value="assetPlatformSqlSessionFactory"/>
    </bean>
</beans>

com.xxxxx.AssetsPlatformMapper:配置

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface AssetsPlatformMapper {
}

使用:配置多个datasource.xml和自定义注解AssetsPlatformMapper(Mapper文件不管在什么目录都可以生效,一个注解对应一种数据源)

@AssetsPlatformMapper
public interface AssetCardBaseMapper {
    AssetCardBasePO selectOne(AssetCardBasePO cardBasePO);

    List<AssetCardBasePO> selectList(AssetCardBasePO assetCardBasePO);
    
}

你可能感兴趣的:(mybatis)