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

sirbozc5  于 2021-06-20  发布在  Mysql
关注(0)|答案(0)|浏览(188)

如何使在bean.xml中配置的所有数据源在控制器中可用 Class1 .
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>

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题