求解:spring结合mybatis配置多数据源报错-问答-阿里云开发者社区-阿里云

开发者社区> 问答> 正文
阿里云
为了无法计算的价值
打开APP
阿里云APP内打开

求解:spring结合mybatis配置多数据源报错

项目中要操作多个数据库的表,因此要配置多个数据源,配置如下:

<context:property-placeholder location="classpath:config/jdbc.properties" />


    <!-- DataSource数据源,使用c3p0连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClassName}"></property>
<property name="jdbcUrl"     value="${jdbc.url}"></property>
<property name="user"        value="${jdbc.username}"></property>
<property name="password"    value="${jdbc.password}"></property>
<property name="minPoolSize" value="20"></property>
<property name="maxPoolSize" value="40"></property>
<property name="initialPoolSize" value="20"></property> 
<property name="maxIdleTime" value="10000"></property>
<property name="acquireIncrement" value="10"></property>
<property name="maxStatements" value="0"></property>
<property name="idleConnectionTestPeriod" value="60"></property>
</bean>

<!-- pojo 映射 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="classpath:mybatis/mybatis-config-service.xml"/>
        <!--define entity package  -->
        <property name="typeAliasesPackage" value="com.app.anilys.dc.domain.*" />
    </bean>
    
    <!-- 数据源2配置 start -->
<bean id="activityDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClassName}"></property>
<property name="jdbcUrl"     value="${jdbc.activity.url}"></property>
<property name="user"        value="${jdbc.username}"></property>
<property name="password"    value="${jdbc.password}"></property>
<property name="minPoolSize" value="20"></property>
<property name="maxPoolSize" value="40"></property>
<property name="initialPoolSize" value="20"></property> 
<property name="maxIdleTime" value="10000"></property>
<property name="acquireIncrement" value="10"></property>
<property name="maxStatements" value="0"></property>
<property name="idleConnectionTestPeriod" value="60"></property>
</bean>
<bean id="activitySqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="activityDataSource" />
        <property name="configLocation" value="classpath:mybatis/mybatis-config-activity.xml"/>
        <property name="typeAliasesPackage" value="com.app.anilys.dc.domain.*" />
    </bean>
<!-- 数据源2配置 end -->
    
     <!-- 自动扫描组件,这里要把controler下面的 controller去除,他们是在spring3-servlet.xml中配置的,如果不去除会影响事务管理的。   -->
<context:component-scan base-package="com.app.anilys.dc">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /> 
    </context:component-scan>
   
    <!-- @AspectJ的支持,从而可以实现通过注解方式将通知编织到非公共方法中。 -->
    <aop:aspectj-autoproxy proxy-target-class="true"/>
   
    <!-- 扫描mapper -->
    <bean name="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
      <property name="basePackage" value="com.app.anilys.dc.dao.*" />
      <property name="sqlSessionFactory" ref="sqlSessionFactory" />
    </bean>
    
    <bean name="activityMapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
      <property name="basePackage" value="com.app.anilys.dc.dao.*" />
      <property name="sqlSessionFactory" ref="activitySqlSessionFactory" />
    </bean>

<!-- dao 事务 -->
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
    <!-- 数据源2事务管理器配置 start -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="activityDataSource" />
    </bean>
    <!-- 数据源2事务管理器配置 end -->
    
    <!-- spring 声明事务管理 -->
<aop:config>
<aop:pointcut id="fooServiceMethods" expression="execution(* com.app.anilys.dc.serviceimpl.*.*.*(..))"/> 
        <aop:advisor advice-ref="txAdvice" pointcut-ref="fooServiceMethods"/> 
</aop:config>
<aop:config>
<aop:pointcut id="controllerMethods" expression="execution(* com.app.anilys.dc.controller.*.*.*(..))"/> 
        <aop:advisor advice-ref="txAdvice" pointcut-ref="controllerMethods"/> 
</aop:config>

<!-- 数据源2事务通知配置 start -->
<aop:config>
<aop:pointcut id="activityFooServiceMethods" expression="execution(* com.app.anilys.dc.serviceimpl.*.*.*(..))"/> 
        <aop:advisor advice-ref="activityTxAdvice" pointcut-ref="activityFooServiceMethods"/> 
</aop:config>
<aop:config>
<aop:pointcut id="activityControllerMethods" expression="execution(* com.app.anilys.dc.controller.*.*.*(..))"/> 
        <aop:advisor advice-ref="activityTxAdvice" pointcut-ref="activityControllerMethods"/> 
</aop:config>
<!-- 数据源2事务通知配置 end -->

<!-- 事务管理配置 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="select*" read-only="true" propagation="REQUIRED" />
            <tx:method name="find*"   read-only="true" propagation="REQUIRED" />
            <tx:method name="get*"   read-only="true" propagation="REQUIRED" />
            <tx:method name="save*" propagation="REQUIRED" isolation="REPEATABLE_READ" />
            <tx:method name="update*" propagation="REQUIRED" isolation="REPEATABLE_READ" />
            <tx:method name="add*" propagation="REQUIRED" isolation="REPEATABLE_READ" />
            <tx:method name="delete*" propagation="REQUIRED" isolation="REPEATABLE_READ" />
            <tx:method name="start*" propagation="REQUIRED" isolation="REPEATABLE_READ" />
            <tx:method name="*" rollback-for="V20Exception"/>
</tx:attributes>
</tx:advice>
<!-- 数据源2事务通知配置 start -->
<tx:advice id="activityTxAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="select*" read-only="true" propagation="REQUIRED" />
            <tx:method name="find*"   read-only="true" propagation="REQUIRED" />
            <tx:method name="get*"   read-only="true" propagation="REQUIRED" />
            <tx:method name="save*" propagation="REQUIRED" isolation="REPEATABLE_READ" />
            <tx:method name="update*" propagation="REQUIRED" isolation="REPEATABLE_READ" />
            <tx:method name="add*" propagation="REQUIRED" isolation="REPEATABLE_READ" />
            <tx:method name="delete*" propagation="REQUIRED" isolation="REPEATABLE_READ" />
            <tx:method name="start*" propagation="REQUIRED" isolation="REPEATABLE_READ" />
            <tx:method name="*" rollback-for="V20Exception"/>
</tx:attributes>
</tx:advice>


启动tomcat,访问工程应用,报如下错误:

org.springframework.jdbc.BadSqlGrammarException:

Error querying database. Cause: com.microsoft.sqlserver.jdbc.SQLServerException: 对象名 'ActivityUser' 无效。

但是ActivityUser在其中上述配置文件数据源为 activityDataSource的库中存在的,说明这个数据源没有起作用,运行的时候还是在数据源为 dataSource的库中去找表,所以报如上错误,烦请各位帮我看看为何数据源 activityDataSource没起作用,多个数据源该如何配置和实现呢?

展开
收起
a123456678 2016-03-17 10:31:14 3242 0
1 条回答
写回答
取消 提交回答
  • a123456678
    <!--流程引擎属性配置-->
        <bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">
            <property name="dataSource" ref="dataSource"/>
            <property name="idGenerator" ref="midGenerator"/>
            <property name="transactionManager" ref="transactionManager"/>
            <property name="databaseSchemaUpdate" value="true"/>
            <!-- 允许Activit更新数据表-->
            <property name="activityFontName" value="宋体"/>
            <property name="labelFontName" value="宋体"/>
            <property name="jobExecutorActivate" value="false"/>
            <property name="history" value="full"/>
            <property name="processDefinitionCacheLimit" value="10"/>
        </bean>
    2019-07-17 19:04:41
    赞同 展开评论 打赏
问答排行榜
最热
最新
相关电子书
更多
Java Spring Boot开发实战系列课程【第6讲】:Spring Boot 2.0实战MyBatis与优化(Java面试题)
立即下载
Spring Boot2.0实战Redis分布式缓存
立即下载
Java Spring Boot开发实战系列课程【第15讲】:Spring Boot 2.0 API与Spring REST Docs实战
立即下载