一个项目中连接了两种数据库,mysql和oracle 技术用的是mybatis和jdbc报了错

图片说明

4个回答

你这个原因应该是缺少数据库连接的驱动包

sqlserver_driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
mysql_driverClassName=com.mysql.jdbc.Driver

.....
 <property value="${sqlserver_driverClassName}" name="driverClassName"/>
 .....
 <property value="${mysql_driverClassName}" name="driverClassName"/>

 -<dependency>

<groupId>mysql</groupId>

<artifactId>mysql-connector-java</artifactId>

<version>5.1.31</version>

</dependency>

-<dependency>

<groupId>com.hynnet</groupId>

<artifactId>sqljdbc4-chs</artifactId>

<version>4.0.2206.100</version>

</dependency>

不同库 需要不同包

Dblshine
Dblshine 回复qq_29962235: 没事, 我解决了。
3 年多之前 回复
qq_29962235
qq_29962235 回复Dblshine: 哥们,不要误会啊,只是看到了而已,oracle驱动包ojdbc6.jar,mysql驱动包mysql-connector-java-5.1.39-bin.jar
3 年多之前 回复
Dblshine
Dblshine 有详细的吗?
3 年多之前 回复

多数据源的话要看你的DataSource和事务的配置
在调用mybatis的方法时要指定数据源,要不然他怎么知道你具体要持久化哪个

 <?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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.2.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
    http://www.springframework.org/schema/util 
    http://www.springframework.org/schema/util/spring-util-4.2.xsd
    http://www.springframework.org/schema/task  
    http://www.springframework.org/schema/task/spring-task-4.2.xsd">



    <context:annotation-config/>  
    <context:component-scan base-package="com.w.task"/>  
    <task:annotation-driven scheduler="qbScheduler" mode="proxy"/>  
    <task:scheduler id="qbScheduler" pool-size="10"/>    

    <!-- 引入jdbc配置文件 -->
    <context:property-placeholder location="classpath:conf/jdbc.properties"/>

    <!--本示例采用DBCP连接池。 连接池配置如下 -->
    <bean id="dataSourceSqlServer" class="org.apache.commons.dbcp2.BasicDataSource">
        <property name="driverClassName" value="${sqlserver_driverClassName}" />
        <property name="url" value="${sqlserver_url}" />
        <property name="username" value="${sqlserver_username}" />
        <property name="password" value="${sqlserver_password}" />
        <!--initialSize: 初始化连接-->  
        <property name="initialSize" value="5"/>  
        <!--maxIdle: 最大空闲连接-->  
        <property name="maxIdle" value="10"/>  
        <!--minIdle: 最小空闲连接-->  
        <property name="minIdle" value="5"/>          
    </bean>    
    <!-- mybatis文件配置,扫描所有mapper文件 -->
    <bean id="sqlServerSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"
        p:dataSource-ref="dataSourceSqlServer" p:configLocation="classpath:conf/mybatis-config-sqlserver.xml"
        p:mapperLocations="classpath:mapperSqlServer/*.xml" /><!-- configLocation为mybatis属性 
        mapperLocations为所有mapper -->

    <!-- spring与mybatis整合配置,扫描所有dao ,生成与DAO类相同名字的bean(除了首字母小写)-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"
        p:basePackage="com.w.sqlserver.dao" p:sqlSessionFactoryBeanName="sqlServerSessionFactory" />

    <!-- 对数据源进行事务管理 -->
    <bean id="transactionManagerSqlServer" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
        p:dataSource-ref="dataSourceSqlServer" />
    <tx:annotation-driven mode="proxy" transaction-manager="transactionManagerSqlServer" />
    <tx:advice id="txAdviceSqlServer" transaction-manager="transactionManagerSqlServer">
        <tx:attributes>
            <tx:method name="add*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception" />
            <tx:method name="insert*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception" />
            <tx:method name="save*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception" />
            <tx:method name="del*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception" />       
           <tx:method name="modify*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception" />
            <tx:method name="update*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception" />
             <tx:method name="edit*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception" />
           <tx:method name="find*" read-only="true" propagation="SUPPORTS"/>
           <tx:method name="get*" read-only="true" propagation="SUPPORTS"/>
           <tx:method name="select*" read-only="true" propagation="SUPPORTS"/>
        </tx:attributes>
   </tx:advice>
   <aop:config>    
       <aop:pointcut id="allMethodSqlServer" expression="execution(* com.w.sqlserver.service..*.*(..))" /> 
       <aop:advisor pointcut-ref="allMethodSqlServer" advice-ref="txAdviceSqlServer" />
    </aop:config>



    <!-- JDBC2 -->    
    <!--本示例采用DBCP连接池。 连接池配置如下 -->
    <bean id="dataSourceMySQL" class="org.apache.commons.dbcp2.BasicDataSource">
        <property name="driverClassName" value="${mysql_driverClassName}" />
        <property name="url" value="${mysql_url}" />
        <property name="username" value="${mysql_username}" />
        <property name="password" value="${mysql_password}" />
        <!--initialSize: 初始化连接-->  
        <property name="initialSize" value="5"/>  
        <!--maxIdle: 最大空闲连接-->  
        <property name="maxIdle" value="10"/>  
        <!--minIdle: 最小空闲连接-->  
        <property name="minIdle" value="5"/>          
    </bean>    
    <!-- mybatis文件配置,扫描所有mapper文件 -->
    <bean id="mySQLSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"
        p:dataSource-ref="dataSourceMySQL" p:configLocation="classpath:conf/mybatis-config-mysql.xml"
        p:mapperLocations="classpath:mapperMySQL/*.xml" /><!-- configLocation为mybatis属性 
        mapperLocations为所有mapper -->

    <!-- spring与mybatis整合配置,扫描所有dao ,生成与DAO类相同名字的bean(除了首字母小写)-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"
        p:basePackage="com.w.mysql.dao" p:sqlSessionFactoryBeanName="mySQLSessionFactory" />

    <!-- 对数据源进行事务管理 -->
    <bean id="transactionManagerMySQL" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
        p:dataSource-ref="dataSourceMySQL" />
    <tx:annotation-driven mode="proxy" transaction-manager="transactionManagerMySQL" />
    <tx:advice id="txAdviceMySQL" transaction-manager="transactionManagerMySQL">
        <tx:attributes>
            <tx:method name="add*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception" />
            <tx:method name="insert*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception" />
            <tx:method name="save*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception" />
            <tx:method name="del*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception" />       
           <tx:method name="modify*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception" />
            <tx:method name="update*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception" />
             <tx:method name="edit*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception" />
           <tx:method name="find*" read-only="true" propagation="SUPPORTS"/>
           <tx:method name="get*" read-only="true" propagation="SUPPORTS"/>
           <tx:method name="select*" read-only="true" propagation="SUPPORTS"/>
        </tx:attributes>
   </tx:advice>
   <aop:config>    
       <aop:pointcut id="allMethodMySQL" expression="execution(* com.w.mysql.service..*.*(..))" /> 
       <aop:advisor pointcut-ref="allMethodMySQL" advice-ref="txAdviceMySQL" />
    </aop:config>
</beans>

文件名spring-mybatis.xml
我是凭兴趣自己配置的,项目有点复杂 这个是双数据库的配置,参考用 mysql 和 sqlserver 的 也是SSM的。哪里不足大家也可以指出来

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐