天天看点

spring+hibernate多数据源的应用

我有两个数据库test,和test1,两个库里都有一张表TEST_ONE

applicationContext.xml的配置如下

//数据库test配置

<bean id="test"

            class="org.springframework.jdbc.datasource.DriverManagerDataSource">

            <property name="driverClassName"value="com.mysql.jdbc.Driver">

            </property>

            <property name="url"value="jdbc:mysql://localhost:3306/test"></property>

            <property name="username"value="root"></property>

            <property name="password"value="root"></property>

      </bean>

//数据库test1配置

    <beanid="test1"

            class="org.springframework.jdbc.datasource.DriverManagerDataSource">

            <property name="driverClassName"value="com.mysql.jdbc.Driver">

            </property>

            <property name="url"value="jdbc:mysql://localhost:3306/test1"></property>

            <property name="username"value="root"></property>

            <property name="password"value="root"></property>

      </bean>

  //整合两个数据源,指定数据源管理类

    <beanid="dataSource"class="com.keith.dataSource.DynamicDataSource">

            <propertyname="targetDataSources">

                  <mapkey-type="java.lang.String">

                        <entry key="test" value-ref="test"/>

                        <entry key="test1" value-ref="test1"/>

                  </map>

            </property>

//设置默认数据源配置

            <property name="defaultTargetDataSource" ref="test"/>

      </bean>

  <bean id="sessionFactory"

            class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

            <property name="dataSource">

                  <ref bean="dataSource" />

            </property>

            <propertyname="hibernateProperties">

                  <props>

                        <prop key="hibernate.dialect">

                              org.hibernate.dialect.MySQLDialect

                        </prop>

                  </props>

            </property>

            <propertyname="mappingResources">

            <list>

            <value>com/keith/vo/TestOne.hbm.xml</value></list>

            </property>

      </bean >

数据源切换的工具类:

public class DBContextHolder {

    //数据库test

    public static final StringDB_TEST="test";

    //数据库test1

    public static final StringDB_TEST_1="test1";

    private static finalThreadLocalCON_LOCAL=new ThreadLocal();

    public static voidsetDB(String type){

          CON_LOCAL.set(type);

    }

    public static StringgetDB(){

          return(String)CON_LOCAL.get();

    }

    public static voidclearDB(){

          CON_LOCAL.remove();

    }

}

//这个类是用来管理数据源的,配置文件中

public class DynamicDataSource extendsAbstractRoutingDataSourc e{

      @Override

      protectedObject determineCurrentLookupKe y() {

            // TODOAuto-generated method stub

              return DBContextHolder.getDB();

      }

}

//编写测试类

public class Test {

      publicstatic void main(String[] args) {

            ApplicationContext context=newClassPathXmlApplicationC ontext("applicationContext.xml");

            //动态设置数据源

            DBContextHolder.setDB(DBContextHolder.DB_TEST_1);

            SessionFactory sf=(SessionFactory)context.getBean("sessionFactory");

            Session s=sf.openSession();

            Transactiontr=s.getTransaction();

            tr.begin();

            TestOneone=new TestOne();

            one.setName("aaa");

            s.save(one);

            tr.commit();

            sf.close();

      }

}