【已解决】ERROR [main] - {dataSource-1} init errorjava.sql.SQLException: ${jdbc.driver}

报错如下

ERROR [main] - {dataSource-1} init error
java.sql.SQLException: ${jdbc.driver}
	at com.alibaba.druid.util.JdbcUtils.createDriver(JdbcUtils.java:689)
	at com.alibaba.druid.pool.DruidDataSource.resolveDriver(DruidDataSource.java:1219)
	at com.alibaba.druid.pool.DruidDataSource.init(DruidDataSource.java:888)
	at com.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:1403)
	at com.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:1399)
	at com.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:100)
	at org.springframework.jdbc.datasource.DataSourceUtils.fetchConnection(DataSourceUtils.java:157)
	at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:115)
	at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:78)
	at org.mybatis.spring.transaction.SpringManagedTransaction.openConnection(SpringManagedTransaction.java:80)
	at org.mybatis.spring.transaction.SpringManagedTransaction.getConnection(SpringManagedTransaction.java:67)
	at org.apache.ibatis.executor.BaseExecutor.getConnection(BaseExecutor.java:337)
	at org.apache.ibatis.executor.SimpleExecutor.prepareStatement(SimpleExecutor.java:86)
	at org.apache.ibatis.executor.SimpleExecutor.doQuery(SimpleExecutor.java:62)
	at org.apache.ibatis.executor.BaseExecutor.queryFromDatabase(BaseExecutor.java:325)
	at org.apache.ibatis.executor.BaseExecutor.query(BaseExecutor.java:156)
	at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:109)
	at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:89)
	at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:147)
	at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:140)
	at org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:147)
	at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:80)
	at org.apache.ibatis.binding.MapperProxy$PlainMethodInvoker.invoke(MapperProxy.java:152)
	at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:85)
	at com.sun.proxy.$Proxy10.queryStudents(Unknown Source)
	at test.main(test.java:19)

1. database.properties没有问题

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssmnew?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT
jdbc.username=root
jdbc.password=123456
jdbc.init=1
jdbc.minIdle=1
jdbc.maxActive=20

里面的username的root后面不要有空格!!! 这也可能导致报错,以前遇到过

还有最好加上时区

2. 考虑兼容问题:把数据库版本提高了,再次运行还是报错

<dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
   <version>8.0.27</version>
</dependency

3.  把applicationContext.xml配置文件重新写了一遍运行成功了

发现只有一个id不一样,离谱啊

 所以SqlSessionFactoryBean的id不能是sqlSessionFactory吗?

反正id改为sessionFactory以后运行确实不报错了

这里怀疑是会和spring自带的配置发生冲突

 修改如下:

运行成功:

以下是完整代码

<?xml version="1.0" encoding="utf8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/p
                           http://www.springframework.org/schema/p/spring-p.xsd
                           http://www.springframework.org/schema/aop
                           http://www.springframework.org/schema/aop/spring-aop.xsd
                           http://www.springframework.org/schema/tx
                           http://www.springframework.org/schema/tx/spring-tx.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context.xsd "
>
    <context:property-placeholder location="classpath:database.properties"/>
    <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>
    <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="mapperLocations">
            <value>classpath:com/lyx/mapper/*.xml</value>
        </property>
        <property name="typeAliasesPackage" value="com.lyx.entity"/>
    </bean>
    <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.lyx.mapper"/>
        <property name="sqlSessionFactoryBeanName" value="sessionFactory"/>
    </bean>
    <bean id="studentService" class="com.lyx.service.impl.StudentServiceImpl">
        <property name="studentMapper" ref="studentMapper"/>
    </bean>

</beans>
    

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
根据提供的引用内容,您遇到的问题是`DruidDataSource:646 - dataSource init error java.sql.SQLException: com.mysql.cj.jdbc.Driver`。这个错误通常是由于数据库驱动版本不匹配引起的。 根据引用\[1\]和引用\[2\],您使用的是MySQL数据库连接器的不同版本。原始版本是5.1.9,而更高版本是8.0.28。这两个版本的数据库连接器使用的是不同的驱动类。 在您的错误信息中,`java.sql.SQLException: com.mysql.cj.jdbc.Driver`表示无法找到或加载MySQL数据库驱动类。这可能是因为您的项目配置中指定的驱动类与实际使用的驱动版本不匹配。 要解决这个问题,您可以尝试以下几个步骤: 1. 确保您的项目依赖配置中使用的是正确的数据库连接器版本。根据您提供的引用内容,如果您使用的是更高版本的数据库连接器(8.0.28),请确保您的项目依赖配置中使用的是相应的版本。 2. 检查您的项目中是否存在多个版本的数据库连接器。如果存在多个版本的数据库连接器,可能会导致冲突和错误。请确保只使用一个版本的数据库连接器,并将其正确配置在项目中。 3. 检查您的项目中是否存在其他与数据库连接相关的配置错误。例如,检查数据库连接URL、用户名和密码是否正确配置。 通过检查和调整您的项目配置,确保使用正确的数据库连接器版本,并正确配置数据库连接相关的信息,您应该能够解决这个错误。 #### 引用[.reference_title] - *1* *2* [解决error {dataSource-1} init error java.sql.SQLException: com.mysql.cj.jdbc.Driver](https://blog.csdn.net/qq_61604164/article/details/124837231)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

素心如月桠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值