如何在同一个类中使用多个数据源?

时间:2018-05-22 13:06:34

标签: java mysql spring-mvc datasource jdbctemplate

如何在我的控制器Class1中使bean.xml中配置的所有数据源都可用。

bean.xml:

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

    <bean id="xxxbc"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
            <property name="username" value="xxxbc" />
            <property name="url" value="jdbc:mysql://localhost:3306/xxxbc" />
            <property name="password" value="xxxbc" />
    </bean>

    <bean id="xxxbb"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
            <property name="username" value="xxxbb" />
            <property name="url" value="jdbc:mysql://localhost:3306/xxxbb" />
            <property name="password" value="xxxbb" />
    </bean>

    <bean id="yyybc"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
            <property name="username" value="yyybc" />
            <property name="url" value="jdbc:mysql://localhost:3306/yyybc" />
            <property name="password" value="yyybc" />
    </bean>

    <bean id="yyybb"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
            <property name="username" value="yyybb" />
            <property name="url" value="jdbc:mysql://localhost:3306/yyybb" />
            <property name="password" value="yyybb" />
    </bean>




    <bean id="xxxcctemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="xxxcc"></property>
    </bean>
    <bean id="xxxbbtemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="xxxbb"></property>
    </bean>

    <bean id="yyybctemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="yyybc"></property>
    </bean>
    <bean id="yyybbtemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="yyybb"></property>
    </bean>




    <bean id="class1" class="com.controller.Class1">
        <property name="jdbcTemplate" ref="xxxcctemplate"></property>
    </bean>

    <bean id="class2" class="com.controller.Class1">
        <property name="jdbcTemplate" ref="xxxbbtemplate"></property>
    </bean>

    <bean id="class3" class="com.controller.Class1">
        <property name="jdbcTemplate" ref="yyybctemplate"></property>
    </bean>

    <bean id="class4" class="com.controller.Class1">
        <property name="jdbcTemplate" ref="yyybbtemplate"></property>
    </bean>

</beans>

0 个答案:

没有答案
相关问题