最近开始学习框架,实现Mybatis的入门案例,看视频弹幕里面有好几个人说自己调了一天,结果自己真就也调了一天(吐血),开始什么关于时区和编码字符的问题很快就解决了,最后一直报下面这个错误:
Exception in thread "main" org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: java.sql.SQLException: Error setting driver on UnpooledDataSource. Cause: java.lang.ClassNotFoundException: Cannot find class: com.mysql.cj.jdbc.Driver
### The error may exist in dao/UserDao.xml
### The error may involve dao.UserDao.findAll
### 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:150)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:141)
at org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:137)
at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:75)
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:59)
at com.sun.proxy.$Proxy3.findAll(Unknown Source)
at test.MybatisTest.main(MybatisTest.java:27)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at com.intellij.rt.execution.application.AppMainV2.main(AppMainV2.java:131)
最终找到原因是自己的mysql版本与依赖版本不一致,利用cmd找到自己的mysql版本为8.0.19,而自己加的依赖mysql版本是5.1.32,正是版本不匹配导致的错误。
于是我开始了艰辛的调试之旅,中间试了很多方法:
1.将mysql版本在pom.xml文件中手动改为8.0.19,然后通过IDEA连接官网下载依赖jar包,失败。
2.更改maven的settings.xml配置文件连接私服自动下载,失败。
<!--私服的地址-->
<mirror>
<id>public</id>
<name>internal nexus repository</name>
<url> http://192.168.1.120:8081/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
3.在maven官网手动下载8.0.19相关jar包复制到maven的本地仓库中直接加载依赖,失败。
maven官网下载依赖网址:https://mvnrepository.com/ 直接进入搜索mysql,找到对应版本下载相应jar包即可。
4.jar包不能简单复制,需要通过cmd中mvn命令安装,成功!!!
以下为cmd中的操作及显示结果:
C:\Users\lenovo>mvn install:install-file -DgroupId=mysql -DartifactId=mysql-connector-java -Dversion=8.0.19 -Dpackaging=jar -Dfile=C:\Users\lenovo\Desktop\mysql-connector-java-8.0.19.jar
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-install-plugin:2.4:install-file (default-cli) @ standalone-pom ---
[INFO] Installing C:\Users\lenovo\Desktop\mysql-connector-java-8.0.19.jar to E:\maven_repository\mysql\mysql-connector-java\8.0.19\mysql-connector-java-8.0.19.jar
[INFO] Installing C:\Users\lenovo\AppData\Local\Temp\mvninstall15651534075899359776.pom to E:\maven_repository\mysql\mysql-connector-java\8.0.19\mysql-connector-java-8.0.19.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.702 s
[INFO] Finished at: 2020-08-15T20:25:43+08:00
[INFO] Final Memory: 6M/30M
[INFO] ------------------------------------------------------------------------
相关参数设置参考以下pom.xml的相关依赖程序:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.19</version>
</dependency>
说明:需要先把在官网上下载好的8.0.19版本mysql的jar包放到其他目录,输入命令后程序会自动将相关jar包文件放在你的maven本地仓库中。
这点破东西我鼓捣了小半天,我真是醉了,我也是巨菜(吐血)。