细数Mybatis中常见问题 报错Cannot find class: ${com.mysql.cj.jdbc.Driver

 今天高高兴兴的写了个mybatis 啪 就很突然 欺负我这个老人 就报各种异常

网上有很多这种错误的解决办法,笔者试了很多没有效果,这个问题出现的原因也很多,这里我只介绍下我的解决办法,提供一种思路

配置mybatis后,运行报错:

### The error may exist in com/dao/dao.xml
### The error may involve com.dao.mapper.getuser
### The error occurred while executing a query
### Cause: java.sql.SQLException: Error setting driver on UnpooledDataSource. Cause: java.lang.ClassNotFoundException: Cannot find class: ${com.mysql.cj.jdbc.Driver

	at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
	at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:149)
	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.invoke(MapperProxy.java:57)
	at com.sun.proxy.$Proxy4.getuser(Unknown Source)
	at test.cc(test.java:14)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:497)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
	at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
	at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
	at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)
Caused by: java.sql.SQLException: Error setting driver on UnpooledDataSource. Cause: java.lang.ClassNotFoundException: Cannot find class: ${com.mysql.cj.jdbc.Driver
	at org.apache.ibatis.datasource.unpooled.UnpooledDataSource.initializeDriver(UnpooledDataSource.java:241)
	at org.apache.ibatis.datasource.unpooled.UnpooledDataSource.doGetConnection(UnpooledDataSource.java:220)

细数mybtis中的坑

第一坑

昨天做的Mybatis的一个demo,pom文件中jar包是5.1.46,第二天再打开就报这个错误,

解决办法:将pom文件中jar包改为8.0.17即可,加上时区

 <property name="url" value="jdbc:mysql://localhost:3306/mybatis?serverTimezone=GMT%2B8"/>

第二坑

在pom文件中追加 build    因为maven中约定大约配置 所以要手动添加

不然会报绑定异常 

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.kgc2</groupId>
    <artifactId>MybatisTest0603</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>MybatisTest0604</module>
    </modules>
    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.23</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.6</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
    </dependencies>

    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                    <include>**/*.tld</include>
                </includes>
                <filtering>true</filtering>
            </resource>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                    <include>**/*.tld</include>
                </includes>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>
</project>

第三坑

mybatis 核心配置文件中 mapper resource中必须用/不能用

必须配置 接口实现类的xml 不配置mapper会报错 绑定异常

必须配置 不然会报int什么初始化异常

还有 com.mysql.cj.jdbc.Driver在mysql 比较高的版本 需要加上 cj

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="${com.mysql.cj.jdbc.Driver"/>
                <property name="url" value="${jdbc:mysql://localhost:3306/mybatis?useSSL=true&amp;useUnicode=true&amp;characterEncoding=utf-8}"/>
                <property name="username" value="${root}"/>
                <property name="password" value="${123qwe}"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="com/dao/dao.xml"/>
    </mappers>
</configuration>

上面也是错的 问题是什么呢?? 这个bug我找了2天终于发现了 引用 mybatis文档的一句话要是世界总是这么简单就好了! 

之前习惯写propties 配置 所以都是${driver} 

"${com.mysql.cj.jdbc.Driver"所以这么写是错的 多了${符号

就会报错 com.mysql.cj.jdbc.Driver

下面是正确的

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true &amp; useUnicode=true &amp; characterEncoding=utf-8"/>
                <property name="username" value="root"/>
                <property name="password" value="123qwe"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="com/dao/dao.xml"/>
    </mappers>
</configuration>

第四坑

mybatis有了核心配置 我们需要创建工具包

包最上面需要我们 导核心配置的xml

package com.util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.*;

import java.io.*;

public class util{
        private static SqlSessionFactory sqlSessionFactory=null;
    static{
        try{
        String resource="core.xml";
        InputStream inputStream= Resources.getResourceAsStream(resource);
        sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
        public static SqlSession getSqlSession(){
            return sqlSessionFactory.openSession();
            }
}

第5坑

接口实现的xml中 namespace要使用全路径名 到mapper接口

id 是接口里的方法 

我记得。。。。。。

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.dao.mapper">
    <select id="getuser" resultType="com.pojo.user">
            select * from mybatis.user
    </select>
</mapper>

第6坑

以上都问题记得刷新maven mysql 保证连接 或者重启idea

 

第七坑 

不要用中文注释 

第八坑

使用mapper resource xml如果没有放在resources目录下 需要补全java下的全路径 用/分割

不然会报异常

Exception in thread "main" java.lang.ExceptionInInitializerError

 如果放在resources下只需要补全resources下全路径 


第九坑 

url路径问题(可能是版本原因)

 

 以上就是暂时遇到的各种报错 。。。。。。。。。。。。

以后遇见更多的报错会继续添加

祝愿各位大佬学习顺利 !!!

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

kjshuan

点个赞就好啦!!!

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

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

打赏作者

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

抵扣说明:

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

余额充值