解决方案 »

  1.   

    想要不重复这么写,可以搞两个baseDao  继承来用
      

  2.   

    AbstractRoutingDataSource 配合aop 是实现多数据源切换.可以最大程度降低代码的耦合, 不管是service,dao本身都无需关注数据源问题. 不同的数据源仅仅是切点特征不同而以, 比如:service方法名.  mysqlXXX 查询mysql数据源,  oracleXXX查询oracle数据源
      

  3.   


    @Repository("quartsPlanDao")
    public class QuartsPlanDaoImpl implements QuartsPlanDao{
        @Resource
        SessionFactory sqlSessionFactoryMysql;
    }@Repository("sysIssuePlanDao")
    public class SysIssuePlanDaoImpl implements SysIssuePlanDao{
        @Resource
        SessionFactory sqlSessionFactoryOracle;
    }
    Spring config xml:<context:component-scan base-package="com.hpb.quarts"/>
    不会这么简单吧,我理解错了?
      

  4.   


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

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


    <!-- 使用annotation 自动注册bean, 并保证@Required、@Autowired的属性被注入 -->

    <context:component-scan base-package="com.jfpal" >
    </context:component-scan>

    <!-- 数据源 -->
    <bean id="dataSourceOne" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="driverClass" value="${jdbc.driver}"></property>
    <property name="jdbcUrl" value="${jdbc.url}"></property>
    <property name="user" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>
    <property name="minPoolSize" value="1" />
    <property name="maxPoolSize" value="12" />
    <property name="maxIdleTime" value="1800" />
    <property name="acquireIncrement" value="2" />
    <property name="maxStatements" value="10" />
    <property name="initialPoolSize" value="2" />
    <property name="idleConnectionTestPeriod" value="1800" />
    <property name="acquireRetryAttempts" value="30" />
    <property name="acquireRetryDelay" value="100" />
    <property name="breakAfterAcquireFailure" value="false" />
    <property name="testConnectionOnCheckout" value="false" />
    </bean>
      
      
      <!-- 数据源2 -->
    <bean id="dataSourceTwo" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="driverClass" value="${jdbc.driver2}"></property>
    <property name="jdbcUrl" value="${jdbc.url2}"></property>
    <property name="user" value="${jdbc.username2}"/>
    <property name="password" value="${jdbc.password2}"/>
    <property name="minPoolSize" value="1" />
    <property name="maxPoolSize" value="12" />
    <property name="maxIdleTime" value="1800" />
    <property name="acquireIncrement" value="2" />
    <property name="maxStatements" value="10" />
    <property name="initialPoolSize" value="2" />
    <property name="idleConnectionTestPeriod" value="1800" />
    <property name="acquireRetryAttempts" value="30" />
    <property name="acquireRetryDelay" value="100" />
    <property name="breakAfterAcquireFailure" value="false" />
    <property name="testConnectionOnCheckout" value="false" />
    </bean>

    <bean id="dynamicDataSource" class="com.jfpal.framework.dao.DynamicDataSource" >  
        <!-- 通过key-value的形式来关联数据源 -->  
        <property name="targetDataSources">  
            <map>  
                <entry value-ref="dataSourceOne" key="dataSourceOne"></entry>  
                <entry value-ref="dataSourceTwo" key="dataSourceTwo"></entry>  
            </map>  
        </property>  
        <property name="defaultTargetDataSource" ref="dataSourceOne" />  
    </bean> 
    <bean id="dataSourceInterceptor" class="com.jfpal.framework.interceptor.DataSourceInterceptor">
    </bean>
    <aop:config>
    <aop:aspect id="dataSourceAspect" ref="dataSourceInterceptor">  
                <aop:pointcut id="switch" expression="execution(* com.jfpal.pmout.payterminal.service.*.*.*(..))" /> 
                <!-- 进入方法前设置为数据源2 -->
                <aop:before pointcut-ref="switch" method="setdataSourceTwo" />
                <!-- 退出方法,设置回数据源1 -->  
                <aop:after pointcut-ref="switch" method="setdataSourceOne"/>
            </aop:aspect>
            
    </aop:config>

    <!-- SqlSessionFactory配置 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dynamicDataSource" />
    <!-- 自动扫描entity目录, 省掉Configuration.xml里的手工配置 -->
    <property name="typeAliasesPackage" value="com.jfpal" />
    <!-- 显式指定Mapper文件位置 -->
    <property name="mapperLocations" value="classpath*:mybatis/**/*Mapper.xml" />
    <property name="configurationProperties">
    <props>
    <prop key="dialect">oracle</prop>
    </props>
    </property>
    </bean> <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
    <constructor-arg index="0" ref="sqlSessionFactory" />
    </bean>

    <!-- ================================事务相关控制=================================================    -->
      <bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">     
              <property name="dataSource" ref="dynamicDataSource"></property>    </bean>     
      
      <tx:advice id="userTxAdvice" transaction-manager="transactionManager">
        <tx:attributes>
          <tx:method name="insert*" propagation="REQUIRED" read-only="false" 
                                rollback-for="java.lang.Exception" no-rollback-for="java.lang.RuntimeException"/>
          <tx:method name="select*" propagation="REQUIRED" read-only="false" 
                                rollback-for="java.lang.Exception" no-rollback-for="java.lang.RuntimeException"/>
          <tx:method name="find*" propagation="REQUIRED" read-only="false" 
                                rollback-for="java.lang.Exception" no-rollback-for="java.lang.RuntimeException"/>
          <tx:method name="get*" propagation="REQUIRED" read-only="false" 
                                rollback-for="java.lang.Exception" no-rollback-for="java.lang.RuntimeException"/>
          <tx:method name="save*" propagation="REQUIRED" read-only="false" 
                                rollback-for="java.lang.Exception" no-rollback-for="java.lang.RuntimeException"/>
          <tx:method name="delete*" propagation="REQUIRED" read-only="false" 
                                rollback-for="java.lang.Exception" no-rollback-for="java.lang.RuntimeException"/>
                                               
        </tx:attributes>
      </tx:advice>
      </beans>
    全部配置在这里了,具体数据库配置就不用了吧?
      

  5.   

    多套SessionFactory必须是可以的;你用Spring3.x来做的话直接用注解把SessionFactory注入到各种Dao中即可。
      

  6.   


    现在用的就是继承 .但是,父类是注入进去的.
    子类,出来的时候,非spring对象.
      

  7.   


    是的.事务的部分是这样配置 的.
    <aop:pointcut id="appService" expression="execution(* com.hpb..*.*MService*(..))" />
    因为他支持表达式.
      

  8.   


    你的思路应该是对的..
    开始的时候,我也是这样配置..但是,不知道为什么注入不进去.而且,sqlsession  不是一个属性.
    它只是那个support 的一个SET方法 .我不理解,为什么就注入不进去了.就是想用这样的扫描方式但是我没试成功