spring-hibernate配置多个mysql连接池

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:beans="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:task="http://www.springframework.org/schema/task"
        xmlns:mongo="http://www.springframework.org/schema/data/mongo"

        xsi:schemaLocation="
         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
            http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd
            http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.8.xsd">

    <context:component-scan base-package="dao,service,task"/>

    <beans:bean id="property" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <beans:property name="locations">
            <beans:list>
                <beans:value>classpath:jdbc.properties</beans:value>
                <beans:value>classpath:jdbc-local.properties</beans:value>
            </beans:list>
        </beans:property>
    </beans:bean>

    <!-- Default bean name is 'mongo' -->
    <mongo:mongo host="192.168.*.*" port="27017" id="mongo">

    </mongo:mongo>

    <!-- Offers convenience methods and automatic mapping between MongoDB JSON documents and your domain classes. -->
    <beans:bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
        <beans:constructor-arg ref="mongo"/>
        <beans:constructor-arg name="databaseName" value="internal"/>
    </beans:bean>

    <!-- local bean name is 'local' -->
    <!--<mongo:mongo host="127.0.0.1" port="27017" id="local">-->

    <!--</mongo:mongo>-->
    <!--<beans:bean id="localMongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">-->
    <!--<beans:constructor-arg ref="local"/>-->
    <!--<beans:constructor-arg name="databaseName" value="internal"/>-->
    <!--</beans:bean>-->

    <beans:bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <beans:property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <beans:property name="url" value="${jdbc.url}"/>
        <beans:property name="username" value="${jdbc.username}"/>
        <beans:property name="password" value="${jdbc.password}"/>
    </beans:bean>

    <!-- 本地mysql连接配置-->
    <beans:bean id="localDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <beans:property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <beans:property name="url" value="${local-jdbc.url}"/>
        <beans:property name="username" value="${local-jdbc.username}"/>
        <beans:property name="password" value="${local-jdbc.password}"/>
    </beans:bean>

    <!-- 配置hibernate相关信息 -->
    <beans:bean id="sessionFactory" name="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <beans:property name="dataSource" ref="dataSource"/>
        <beans:property name="hibernateProperties">
            <beans:props>

                <beans:prop key="connection.useUnicode">true</beans:prop>
                <beans:prop key="connection.characterEncoding">UTF-8</beans:prop>

                <beans:prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</beans:prop>
                <beans:prop key="hibernate.show_sql">true</beans:prop>
                <beans:prop key="hibernate.format_sql">true</beans:prop>

                <beans:prop key="hibernate.hbm2ddl.auto">update</beans:prop>

                <beans:prop key="hibernate.connection.autocommit">true</beans:prop>
                <beans:prop key="hibernate.transaction.manager_lookup_class">
                    org.springframework.orm.hibernate4.SpringSessionContext
                </beans:prop>
            </beans:props>
        </beans:property>

        <beans:property name="packagesToScan" value="model.*"/>
    </beans:bean>

    <!-- 本地localSessionFactory 注意name属性-->
    <beans:bean id="localSessionFactory" name="localSessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <beans:property name="dataSource" ref="localDataSource"/>
        <beans:property name="hibernateProperties">
            <beans:props>

                <beans:prop key="connection.useUnicode">true</beans:prop>
                <beans:prop key="connection.characterEncoding">UTF-8</beans:prop>

                <beans:prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</beans:prop>
                <beans:prop key="hibernate.show_sql">true</beans:prop>
                <beans:prop key="hibernate.format_sql">true</beans:prop>

                <beans:prop key="hibernate.hbm2ddl.auto">update</beans:prop>

                <beans:prop key="hibernate.connection.autocommit">true</beans:prop>
                <beans:prop key="hibernate.transaction.manager_lookup_class">
                    org.springframework.orm.hibernate4.SpringSessionContext
                </beans:prop>
            </beans:props>
        </beans:property>

        <beans:property name="packagesToScan" value="dao.impl.*,model.*"/>
    </beans:bean>

    <!-- 事务管理器-->
    <beans:bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <beans:property name="sessionFactory" ref="sessionFactory"/>
    </beans:bean>

    <!-- 使用annotation定义事务 -->
    <tx:annotation-driven transaction-manager="transactionManager"/>


    <!--定时任务-->
    <task:annotation-driven/>

</beans:beans>

spring配置多个mysql连接池



你可能感兴趣的:(spring,Hibernate,mys)