Spring's solution to integrating mybatis multiple data sources

Today, I tried to separate the log database management, so I used two data sources in one project. After searching for information on the Internet, I finally succeeded. Record it.

First configure jdbc.properties (all the following are basic configurations)

db.driverClass=com.mysql.jdbc.Driver
db1.url=jdbc:mysql://localhost:3306/auge_new?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true
db2.url=jdbc:mysql://localhost:3306/auge_log?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true
db.username=root
db.password=123456

Then configure datasource-config.xml (you can configure it directly in spring-mybatis.xml, here I extracted it)

<!-- database configuration file location-->
    <context:property-placeholder location="classpath:jdbc.properties"/>
<!-- data source 1-->
    <bean id="dataSource1" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
        <!-- Basic attributes url, user, password -->
        <property name="driverClassName" value="${db.driverClass}"/>
        <property name="url" value="${db1.url}"/>
        <property name="username" value="${db.username}"/>
        <property name="password" value="${db.password}"/>
    </bean>
    <!-- data source 2 -->
    <bean id="dataSource2" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
        <!-- Basic attributes url, user, password -->
        <property name="driverClassName" value="${db.driverClass}"/>
        <property name="url" value="${db2.url}"/>
        <property name="username" value="${db.username}"/>
        <property name="password" value="${db.password}"/>
    </bean>
    <bean id="dynamicDataSource" class="com.xiaodou.park.interceptors.DynamicDataSource">
        <property name="targetDataSources">
            <map key-type="java.lang.String">
                <!--Specify lookupKey and its corresponding data source-->
                <entry key="dataSource1" value-ref="dataSource1"></entry>
                <entry key="dataSource2" value-ref="dataSource2"></entry>
            </map>
        </property>
        <!--The default data source can be specified here-->
        <property name="defaultTargetDataSource" ref="dataSource1"/>
    </bean>

Below is the DynamicDataSource class and the DynamicDataSourceHolder class.

public class DynamicDataSource extends AbstractRoutingDataSource {
    @Override
    protected Object determineCurrentLookupKey() {
        // Get the data source ID from the custom location
        return DynamicDataSourceHolder.getDataSource();
    }
}
public class DynamicDataSourceHolder {
    private static final ThreadLocal <String> THREAD_DATA_SOURCE = new ThreadLocal <String>();
    public static String getDataSource(){
        return THREAD_DATA_SOURCE.get();
    }
    public static void setDataSource(String dataSource){
        THREAD_DATA_SOURCE.set(dataSource);
    }
    public static void clearDataSource(){
        THREAD_DATA_SOURCE.remove();
    }
}

Then configure the factory class and transaction processing in spring-mybatis.xml.

    <import resource="datasource-config.xml" />

    <!-- Data source session factory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="configLocation" value="classpath:mybatis-config.xml" />
        <property name="dataSource" ref="dynamicDataSource" />
    </bean>


    <!-- Data Source Mapping-->
    <!--<context:component-scan base-package="com.wechat.xiaodou.mapper"/>-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
        <!--available, or ;split multiple scanned packets-->
        <property name="basePackage" value="com.xiaodou.park.mapper"/>
    </bean>

    <bean id="transactionManager"
          class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource">
            <ref bean="dynamicDataSource"/>
        </property>
    </bean>

After configuration, the dataSource1 data source is used by default. If you want to use the dataSource2 data source, the following code is shown:

DynamicDataSourceHolder.setDataSource("dataSource2");
errorLogService.insertSelective(errorLog);
That's it for the time being, and it will be recorded after subsequent code development.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325856118&siteId=291194637
Recommended