Spring与MyBatis集成,使用的数据源是c3p0,为什么需要引入spring-jdbc这个jar包?(如下面的代码)

下面的错误应该是与数据源的注入相关吧!!!但是为什么引入spring-jdbc这个jar包就能解决呢?

如果不引入spring-jdbc这个jar包:将报如下的错(关键信息):
警告: Exception encountered during context initialization - cancelling refresh attempt: 
org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'sqlSessionFactory' defined in file 
[C:\Users\123\IdeaProjects\SpringDemo\target\seckill\WEB-INF\classes\spring\spring-dao.xml]:
 Error setting property values;
 nested exception is org.springframework.beans.PropertyBatchUpdateException; 
nested PropertyAccessExceptions (1) are:

PropertyAccessException 1: org.springframework.beans.MethodInvocationException:
 Property 'dataSource' threw exception; 
nested exception is java.lang.NoClassDefFoundError: 
org/springframework/jdbc/datasource/TransactionAwareDataSourceProxy

下面再附上spring-dao.xml中有关数据源的配置:

<!--2.数据库连接池-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <!--配置连接池属性-->
    <property name="driverClass" value="${driver}" />

    <!-- 基本属性 url、user、password -->
    <property name="jdbcUrl" value="${url}" />
    <property name="user" value="${jdbc.username}" />
    <property name="password" value="${password}" />

    <!--c3p0私有属性-->
    <property name="maxPoolSize" value="30"/>
    <property name="minPoolSize" value="10"/>
    <!--关闭连接后不自动commit-->
    <property name="autoCommitOnClose" value="false"/>

    <!--获取连接超时时间-->
    <property name="checkoutTimeout" value="1000"/>
    <!--当获取连接失败重试次数-->
    <property name="acquireRetryAttempts" value="2"/>
</bean>



<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <!--扫描sql配置文件:mapper需要的xml文件-->
    <property name="mapperLocations" value="classpath:com/imooc/mappers/*.xml"/>
</bean>

阅读 3.6k
1 个回答

和c3p0没有关系,是因为org.mybatis.spring.SqlSessionFactoryBean用到了spring-jdbc。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题