栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

细数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即可,加上时区

 

第二坑

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

不然会报绑定异常 



    4.0.0

    com.kgc2
    MybatisTest0603
    pom
    1.0-SNAPSHOT
    
        MybatisTest0604
    
    
        
            mysql
            mysql-connector-java
            8.0.23
        
        
            org.mybatis
            mybatis
            3.5.6
        
        
            junit
            junit
            4.12
        
    

    
        
            
                src/main/resources
                
                    ***.xml
                    ***.properties
                    ***.tld
                
                true
            
        
    

第三坑

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

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

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

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




    
        
            
            
                
                
                
                
            
        
    
    
        
    

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

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

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

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

下面是正确的




    
        
            
            
                
                
                
                
            
        
    
    
        
    

第四坑

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 是接口里的方法 

我记得。。。。。。




    

第6坑

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

 

第七坑 

不要用中文注释 


 

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

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

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/781042.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号