新手使用DruidDataSource时最可能出现的错误

最近在学SSM在学到spring整合mybatis时遇到了一个错误困扰了一下午

<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
    <property name="driver" value="${jdbc.driver}"/>
    <property name="url" value="${jdbc.url}"/>
    <property name="username" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>
</bean>

这是开始的配置,因为是从mybatis-config.xml中复杂的所以没发现

<property name="driver" value="${jdbc.driver}"/>中要使用

driverClassName导致出现了一系列奇怪的错误

三月 03, 2023 4:19:40 下午 org.springframework.test.context.support.AbstractTestContextBootstrapper getDefaultTestExecutionListenerClassNames
信息: Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener, org.springframework.test.context.event.EventPublishingTestExecutionListener]
三月 03, 2023 4:19:40 下午 org.springframework.test.context.support.AbstractTestContextBootstrapper getTestExecutionListeners
信息: Using TestExecutionListeners: [org.springframework.test.context.web.ServletTestExecutionListener@6ba0ac, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@1fa3a9d, org.springframework.test.context.support.DependencyInjectionTestExecutionListener@ebc39e, org.springframework.test.context.support.DirtiesContextTestExecutionListener@4bee09, org.springframework.test.context.transaction.TransactionalTestExecutionListener@16c48ac, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener@1deb50e, org.springframework.test.context.event.EventPublishingTestExecutionListener@19963d8]
三月 03, 2023 4:19:41 下午 org.springframework.context.support.AbstractApplicationContext refresh
警告: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'bookServiceImpl': Unsatisfied dependency expressed through field 'bookMapper'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'bookMapper' defined in file [F:\idea_yuanma\chapter14\target\classes\com\sjb\dao\BookMapper.class]: Cannot resolve reference to bean 'sqlSessionFactory' while setting bean property 'sqlSessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [applicationContext.xml]: Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [applicationContext.xml]: Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'java.lang.String' to required type 'java.sql.Driver' for property 'driver'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'java.sql.Driver' for property 'driver': no matching editors or conversion strategy found
三月 03, 2023 4:19:41 下午 org.springframework.test.context.TestContextManager prepareTestInstance
严重: Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@ebc39e] to prepare test instance [com.sjb.service.BookServiceTest@1bbc4e]
java.lang.IllegalStateException: Failed to load ApplicationContext

当把dataSource改成下面这样时就OK了

<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
    <property name="driverClassName" value="${jdbc.driver}"/>
    <property name="url" value="${jdbc.url}"/>
    <property name="username" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>
</bean>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
DruidDataSource 是阿里巴巴开源的一个数据库连接池组件,用于管理和提供数据库连接。在使用 DruidDataSource 之前,需要先引入 Druid 的依赖包。 首先,在项目的 pom.xml 文件中添加以下依赖: ```xml <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>{druid_version}</version> </dependency> ``` 其中,`{druid_version}` 是 Druid 的版本号,可以根据需要进行替换。 接下来,在 Spring Boot 的配置文件(application.properties 或 application.yml)中配置 DruidDataSource 的相关属性,例如: ```properties spring.datasource.url=jdbc:mysql://localhost:3306/db_example spring.datasource.username=db_username spring.datasource.password=db_password spring.datasource.driver-class-name=com.mysql.jdbc.Driver # DruidDataSource 相关配置 spring.datasource.druid.initial-size=5 spring.datasource.druid.min-idle=5 spring.datasource.druid.max-active=20 spring.datasource.druid.max-wait=60000 spring.datasource.druid.time-between-eviction-runs-millis=60000 spring.datasource.druid.min-evictable-idle-time-millis=300000 spring.datasource.druid.validation-query=SELECT 1 spring.datasource.druid.test-while-idle=true spring.datasource.druid.test-on-borrow=false spring.datasource.druid.test-on-return=false spring.datasource.druid.pool-prepared-statements=true spring.datasource.druid.max-pool-prepared-statement-per-connection-size=20 spring.datasource.druid.filters=stat,wall,log4j ``` 以上是一些常见的配置项,你可以根据实际需求进行调整。 最后,在需要使用数据库连接的地方,注入 DruidDataSource 对象即可进行数据库操作。例如,在 Spring Boot 的 Service 类中使用: ```java @Service public class UserService { @Autowired private DruidDataSource dataSource; public void doSomething() { // 使用 dataSource 进行数据库操作 } } ``` 这样就完成了 DruidDataSource使用。通过配置文件中的属性,可以调整连接池的大小、最大等待间、连接验证等设置,从而满足具体项目的需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值