Spring系统访问多数据库

Spring连接多数据库

在比较大的项目中,特别偏报表类的项目,一般会连接好多数据库,有时候需要给不同的service配置不同的数据库。

spring配置文件

applicationContext.xml

  

   <bean id="c3p0DataSource" abstract="true">

     

      <property name="initialPoolSize" value="10" />

     

      <property name="minPoolSize" value="3" />

     

      <property name="maxPoolSize" value="50" />

     

      <property name="maxIdleTime" value="60" />

     

      <property name="acquireIncrement" value="5" />

     

      <property name="idleConnectionTestPeriod" value="60" />

   bean>

  

  

   <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close" parent="c3p0DataSource">

        <property name="driverClass" value="oracle.jdbc.driver.OracleDriver" />

        <property name="jdbcUrl" value="jdbc:oracle:thin:user/password@111:1521:F1" />

        <property name="user" value="test" />

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

    bean>

  

  

   <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">

      <property name="dataSource" ref="dataSource" />

      <property name="hibernateProperties">

         <props>

            <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialectprop>

            <prop key="hibernate.show_sql">trueprop>

            <prop key="hibernate.format_sql">trueprop>

           

            <prop key="hibernate.hbm2ddl.auto">noneprop>

            <prop key="show_sql">trueprop>

         props>

      property>

   bean>

  

  

   <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" abstract="false"

      lazy-init="false" autowire="default">

      <property name="dataSource" ref="dataSource" />

   bean>

  

  

   <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">

      <property name="sessionFactory">

         <ref bean="sessionFactory" />

      property>

   bean>

数据库配置文件

这里连接两种数据库

applicationContextDB.xml

<bean id="jdbcTemplate1" class="org.springframework.jdbc.core.JdbcTemplate">

      <property name="dataSource">

         <bean class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close" parent="c3p0DataSource">

            <property name="driverClass" value="oracle.jdbc.driver.OracleDriver" />

              <property name="jdbcUrl" value="jdbc:oracle:thin:user/password@111:1521:C1" />

              <property name="user" value="app" />

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

         bean>

      property>

   bean>

   <bean id="jdbcTemplate2" class="org.springframework.jdbc.core.JdbcTemplate">

      <property name="dataSource">

         <bean class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close" parent="c3p0DataSource">

            <property name="driverClass" value="oracle.jdbc.driver.OracleDriver" />

              <property name="jdbcUrl" value="jdbc:oracle:thin:user/password@111:1521:a1" />

              <property name="user" value="test" />

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

         bean>

      property>

   bean>

配置Service文件

这里一个service连接一个数据库

applicationContextService.xml

   <bean id="getDataService" class="com.cloud.impl.GetDataImple">

      <property name="jdbcTemplate" ref="jdbcTemplate" />

      <property name="hibernateTemplate" ref="hibernateTemplate" />

   bean>

  

   <bean id="getMenuService" class="com.cloud.impl.GetMenuImpl">

      <property name="jdbcTemplate1" ref="jdbcTemplate1" />

   bean>

  

   <bean id="getUserService" class="com.cloud.impl.GetUserImpl">

      <property name="jdbcTemplate2" ref="jdbcTemplate2" />

   bean>

配置定时器

这里定时器的执行规则是定点执行,秒、分、时

quart.xml

 

    <bean id="getDataClock" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">

       <property name="targetObject" ref="getDataService" />

       <property name="targetMethod" value="getData" />

       <property name="concurrent" value="false" />

    bean>

    <bean id="getDataTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">

       <property name="jobDetail" ref="getDataClock" />

       <property name="cronExpression">

          <value>0 40 17 ? * *value>

       property>

    bean>

   

    <bean id="getMenuClock" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">

       <property name="targetObject" ref="getMenuService" />

       <property name="targetMethod" value="getMenu" />

       <property name="concurrent" value="false" />

    bean>

    <bean id="getMenuTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">

       <property name="jobDetail" ref="getMenuClock" />

       <property name="cronExpression">

          <value>0 39 17 ? * *value>

       property>

    bean>

   

    <bean id="getUserClock" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">

       <property name="targetObject" ref="getUserService" />

       <property name="targetMethod" value="getUser" />

       <property name="concurrent" value="false" />

    bean>

    <bean id="getUserTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">

       <property name="jobDetail" ref="getUserClock" />

       <property name="cronExpression">

          <value>0 41 17 ? * *value>

       property>

    bean>

   

    <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">

       <property name="triggers">

          <list>

             <ref bean="getDataTrigger"/>

             <ref bean="getMenuTrigger"/>

             <ref bean="getUserTrigger"/>

          list>

       property>

    bean>

beans>

连接数据库封装

package com.cloud.Common;

import org.springframework.jdbc.core.JdbcTemplate;

import org.springframework.orm.hibernate3.HibernateTemplate;

public class BaseJdbcSupport {

   private HibernateTemplate hibernateTemplate;

   private JdbcTemplate jdbcTemplate;

   public HibernateTemplate getHibernateTemplate() {

      return hibernateTemplate;

   }

   public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {

      this.hibernateTemplate = hibernateTemplate;

   }

   public JdbcTemplate getJdbcTemplate() {

      return jdbcTemplate;

   }

   public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {

      this.jdbcTemplate = jdbcTemplate;

   }

}

定时器执行代码

package com.cloud.impl;

import java.util.List;

import com.cloud.Common.BaseJdbcSupport;

public class GetDataImple extends BaseJdbcSupport{

   @SuppressWarnings("rawtypes")

   public void getData(){

      List list = this.getJdbcTemplate().queryForList("select t.*, t.rowid from AP_USER t where t.user_code='B11302246'");

      System.out.println("查询数据库一:"+list);

   }

}

package com.cloud.impl;

import java.util.List;

import org.springframework.jdbc.core.JdbcTemplate;

public class GetMenuImpl {

   private JdbcTemplate jdbcTemplate1;

   public JdbcTemplate getJdbcTemplate1() {

      return jdbcTemplate1;

   }

   public void setJdbcTemplate1(JdbcTemplate jdbcTemplate1) {

      this.jdbcTemplate1 = jdbcTemplate1;

   }

   @SuppressWarnings("rawtypes")

   public void getMenu(){

      List list = this.getJdbcTemplate1().queryForList("select t.*, t.rowid from AP_MENU t where t.menu_name='揽收报表'");

      System.out.println("查询数据库二:"+list);

   }

}

package com.cloud.impl;

import org.springframework.jdbc.core.JdbcTemplate;

public class GetUserImpl {

   private JdbcTemplate jdbcTemplate2;

   public JdbcTemplate getJdbcTemplate2() {

      return jdbcTemplate2;

   }

   public void setJdbcTemplate2(JdbcTemplate jdbcTemplate2) {

      this.jdbcTemplate2 = jdbcTemplate2;

   }

   public void getUser(){

      int count = this.jdbcTemplate2.queryForInt("select count(*) from D_USER");

      System.out.println("查询数据库三:"+count);

   }

}

你可能感兴趣的:([---Spring---])