使用spring + mybatis时,java.lang.ClassNotFoundException:com.mysql.cj.jdbc.Drive发生

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用spring + mybatis时,java.lang.ClassNotFoundException:com.mysql.cj.jdbc.Drive发生相关的知识,希望对你有一定的参考价值。

我使用macos,idea,maven,spring,mybatis,druid,mysql8.0.20进行SQL测试,但发生了java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Drive。pom.xml

         <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.20</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.6</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <dependency>
            <groupId>c3p0</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.1.2</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.8</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.2.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>4.2.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>4.1.3.RELEASE</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.1.3.RELEASE</version>
        </dependency>

错误信息:

    at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:80)
    at org.mybatis.spring.transaction.SpringManagedTransaction.openConnection(SpringManagedTransaction.java:81)
    at org.mybatis.spring.transaction.SpringManagedTransaction.getConnection(SpringManagedTransaction.java:67)
    at org.apache.ibatis.executor.BaseExecutor.getConnection(BaseExecutor.java:338)
    at org.apache.ibatis.executor.SimpleExecutor.prepareStatement(SimpleExecutor.java:84)
    at org.apache.ibatis.executor.SimpleExecutor.doQuery(SimpleExecutor.java:62)
    at org.apache.ibatis.executor.BaseExecutor.queryFromDatabase(BaseExecutor.java:326)
    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:83)
    at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:148)
    ... 41 more
Caused by: java.sql.SQLException: com.mysql.cj.jdbc.Drive
    at com.alibaba.druid.util.JdbcUtils.createDriver(JdbcUtils.java:596)
    at com.alibaba.druid.pool.DruidDataSource.init(DruidDataSource.java:815)
    at com.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:1222)
    at com.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:1218)
    at com.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:90)
    at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:111)
    at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:77)
    ... 51 more
Caused by: java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Drive
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:264)
    at com.alibaba.druid.util.JdbcUtils.createDriver(JdbcUtils.java:594)
    ... 57 more

spring xml:

<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-4.0.xsd
   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
   http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
   http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
    <context:property-placeholder location="classpath:properties/*.properties"/>
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
          destroy-method="close">
        <property name="driverClassName" value="$jdbc.driver"/>
        <property name="url"
                  value="jdbc:mysql://$jdbc.host:3306/$jdbc.database?serverTimezone=UTC&amp;useUnicode=true&amp;characterEncoding=utf-8&amp;zeroDateTimeBehavior=convertToNull"/>
        <property name="username" value="$jdbc.userName"/>
        <property name="password" value="$jdbc.passWord"/>
        <property name="initialSize" value="$jdbc.initialSize"></property>
        <property name="maxActive" value="$jdbc.maxActive"/>
        <property name="minIdle" value="$jdbc.minIdle"></property>
        <property name="maxWait" value="$jdbc.maxWait"></property>
    </bean>
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="mapperLocations" value="classpath:mappers/*.xml"/>
        <!--<property name="configLocation" value="classpath:mybatis-config.xml" />-->
    </bean>

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.wlw.mybatis.mapper"/>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    </bean>

    <bean id="transactionManager"
          class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
</beans>

测试代码:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring/applicationContext*.xml")
public class OrderMapperSpringTest 
    @Autowired
    private OrderMapper orderMapper;

    @Test
    public void queryOrderByOrderId() throws Exception 
        Class.forName("com.mysql.cj.jdbc.Driver"); // this is ok
        Order re = this.orderMapper.queryOrderByOrderId("1"); //error occurs here!!!
        System.out.printf("orderId:%s,userId:%s,user.id:%s,userName:%s,userYear:%s,detail:%s",re.getId(),re.getUserId(),re.getUser().getId(),re.getUser().getName(),re.getUser().getYear(),re.getDetail());
    

    @Test
    public void queryOrderDetailByOrderId() throws Exception 
    


我尝试了很多次,更改pom.xml中的每个软件包版本,甚至将durid更改为c3p0时也出现相同的错误,我确保mysql连接器已成功导入,甚至替换了pom中的mysql连接器代码.xml,方法是将mysql-connector-java-8.0.20.jar复制到资源文件夹,然后通过idea module setting添加到项目。

答案

属性$jdbc.driver的值拼写错误。检查您的属性文件并验证其值。

以上是关于使用spring + mybatis时,java.lang.ClassNotFoundException:com.mysql.cj.jdbc.Drive发生的主要内容,如果未能解决你的问题,请参考以下文章

整合SSM时报错:java.lang.AbstractMethodError: org.mybatis.spring.transaction.SpringManagedTransaction.getT

Mybatis与Spring集成时都做了什么?

关于java-MyBatis-Spring学后笔记

Java面试考点解析-- 数据库篇Spring+MyBatis整合

Spring集成MyBatis框架

Java--Spring和MyBatis集成