String Mybatis 多数据源配置

时间:2022-06-03 12:55:18

做项目用到了多数据源的配置,有时候不用就忘记了,自己做下备份以防止忘记。

在配置文件中:

<bean id="ds1" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value=""/>
        <property name="username" value=""/>
        <property name="password" value=""/>
    </bean>
    
    <bean id="ds2" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value=""/>
        <property name="username" value=""/>
        <property name="password" value=""/>
    </bean>

<bean id="jdbcTemplate1" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="ds2"/>
    </bean>
    <bean id="jdbcTemplate2" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="authDataSource"/>
    </bean>

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

把数据源映射不同的Mapper

<bean id="sqlSessionFactory" class="javacommon.SqlSessionFactoryFactoryBean">
        <property name="configLocation" value="classpath:configuration.xml"/>
        <property name="mapperLocations" value="classpath*:/com/**/model/**/*Mapper.xml"/>
        <property name="dataSource" ref="ds1"/>
    </bean>
    
    <bean id="sqlSessionFactory1" class="javacommon.SqlSessionFactoryFactoryBean">
        <property name="configLocation" value="classpath:configuration.xml"/>
        <property name="mapperLocations" value="classpath*:/com/**/model/**/uc/*Mapper.xml"/>
        <property name="dataSource" ref="ds2"/>
    </bean>

<tx:annotation-driven transaction-manager="transactionManager1" proxy-target-class="true"/>
    <tx:annotation-driven transaction-manager="transactionManager2" proxy-target-class="true"/>

最后最重要的:

public abstract class BaseIbatisOauth3Dao<E,PK extends Serializable> extends BaseIbatis3Dao {

public void setSqlSessionFactory2(SqlSessionFactory sqlSessionFactory) {
        super.setSqlSessionFactory(sqlSessionFactory);
        
    }

需要增加这个类,你的特殊的dao需要继承这个类