Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用弹簧时+;mybatis,java.lang.ClassNotFoundException:com.mysql.cj.jdbc.Drive发生_Java_Mysql_Spring_Maven_Mybatis - Fatal编程技术网

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

使用弹簧时+;mybatis,java.lang.ClassNotFoundException:com.mysql.cj.jdbc.Drive发生,java,mysql,spring,maven,mybatis,Java,Mysql,Spring,Maven,Mybatis,我使用macos、idea、maven、spring、mybatis、druid、mysql8.0.20进行SQL测试,但出现了java.lang.ClassNotFoundException:com.mysql.cj.jdbc.Drive。 pom.xml spring xml: <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframewo

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

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>

我尝试了很多次,更改pom.xml中的每个包版本,甚至将
durid
更改为
c3p0
同样的错误,我确保mysql连接器成功导入,甚至将pom.xml中的mysql连接器代码复制到
mysql-connector-java-8.0.20.jar
资源文件夹中,并通过
idea模块设置添加到项目中

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


它当前的值为
com.mysql.cj.jdbc.Drive
,因此它在末尾缺少一个
r

错误显示,您使用的是
com.mysql.cj.jdbc.Drive
,它应该是
com.mysql.cj.jdbc.Driver
。投票结束是一个打字错误。
    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
<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 {
    }

}