当前位置:   article > 正文

mybatis配置mysql时报错问题Cannot find class: com.mysql.cj.jdbc.Driver_mybatis cannot find class

mybatis cannot find class

配置mybatis后,运行报错:

  1. org.apache.ibatis.exceptions.PersistenceException:
  2. ### Error querying database. Cause: java.sql.SQLException: Error setting driver on UnpooledDataSource. Cause: java.lang.ClassNotFoundException: Cannot find class: com.mysql.cj.jdbc.Driver
  3. ### The error may exist in com/study/Dao/UserMapper.xml
  4. ### The error may involve com.study.Dao.UserDao.getUserList
  5. ### The error occurred while executing a query
  6. ### Cause: java.sql.SQLException: Error setting driver on UnpooledDataSource. Cause: java.lang.ClassNotFoundException: Cannot find class: com.mysql.cj.jdbc.Driver
  7. at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
  8. at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:149)
  9. at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:140)
  10. at org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:147)
  11. at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:80)
  12. at org.apache.ibatis.binding.MapperProxy$PlainMethodInvoker.invoke(MapperProxy.java:152)
  13. at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:85)
  14. at com.sun.proxy.$Proxy6.getUserList(Unknown Source)
  15. at com.study.Dao.TestDao.test(TestDao.java:17)
  16. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  17. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  18. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  19. at java.lang.reflect.Method.invoke(Method.java:498)
  20. at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
  21. at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
  22. at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
  23. at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
  24. at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
  25. at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
  26. at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
  27. at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
  28. at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
  29. at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
  30. at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
  31. at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
  32. at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
  33. at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
  34. at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
  35. at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
  36. at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
  37. at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)
  38. Caused by: java.sql.SQLException: Error setting driver on UnpooledDataSource. Cause: java.lang.ClassNotFoundException: Cannot find class: com.mysql.cj.jdbc.Driver
  39. at org.apache.ibatis.datasource.unpooled.UnpooledDataSource.initializeDriver(UnpooledDataSource.java:244)
  40. at org.apache.ibatis.datasource.unpooled.UnpooledDataSource.doGetConnection(UnpooledDataSource.java:223)
  41. at org.apache.ibatis.datasource.unpooled.UnpooledDataSource.doGetConnection(UnpooledDataSource.java:219)
  42. at org.apache.ibatis.datasource.unpooled.UnpooledDataSource.getConnection(UnpooledDataSource.java:95)
  43. at org.apache.ibatis.datasource.pooled.PooledDataSource.popConnection(PooledDataSource.java:432)
  44. at org.apache.ibatis.datasource.pooled.PooledDataSource.getConnection(PooledDataSource.java:89)
  45. at org.apache.ibatis.transaction.jdbc.JdbcTransaction.openConnection(JdbcTransaction.java:139)
  46. at org.apache.ibatis.transaction.jdbc.JdbcTransaction.getConnection(JdbcTransaction.java:61)
  47. at org.apache.ibatis.executor.BaseExecutor.getConnection(BaseExecutor.java:337)
  48. at org.apache.ibatis.executor.SimpleExecutor.prepareStatement(SimpleExecutor.java:86)
  49. at org.apache.ibatis.executor.SimpleExecutor.doQuery(SimpleExecutor.java:62)
  50. at org.apache.ibatis.executor.BaseExecutor.queryFromDatabase(BaseExecutor.java:325)
  51. at org.apache.ibatis.executor.BaseExecutor.query(BaseExecutor.java:156)
  52. at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:109)
  53. at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:89)
  54. at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:147)
  55. ... 29 more

这很明显嘛,就是没找到mysql链接数据库的包,可是都配过了,父工程的配置依赖:

  1. <dependency>
  2. <groupId>mysql</groupId>
  3. <artifactId>mysql-connector-java</artifactId>
  4. <version>8.0.16</version>
  5. </dependency>
  6. <!-- https://mvnrepository.com/artifact/junit/junit -->
  7. <dependency>
  8. <groupId>junit</groupId>
  9. <artifactId>junit</artifactId>
  10. <version>4.12</version>
  11. </dependency>
  12. <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
  13. <dependency>
  14. <groupId>org.mybatis</groupId>
  15. <artifactId>mybatis</artifactId>
  16. <version>3.5.2</version>
  17. </dependency>

没问题呀,而且另一个子工程能成功连接到数据库:

也就说明包没导入错误,又看了眼mybatis-config.xml,肯定也没错啊:

  1. <configuration>
  2. <environments default="development">
  3. <environment id="development">
  4. <transactionManager type="JDBC"/>
  5. <dataSource type="POOLED">
  6. <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
  7. <property name="url" value="jdbc:mysql://localhost:3306/mybatis?userSSL=true&amp;useUnicode=true&amp;characterEncoding=UTF-8&amp;serverTimezone=UTC"/>
  8. <property name="username" value="root"/>
  9. <property name="password" value="root"/>
  10. </dataSource>
  11. </environment>
  12. </environments>
  13. <mappers>
  14. <mapper resource="com/study/UserMapper.xml"/>
  15. </mappers>
  16. </configuration>

在父工程的pom.xml中也导入了java目录下的resouces文件的导出配置:

  1. <build>
  2. <resources>
  3. <resource>
  4. <directory>src/main/java</directory>
  5. <includes>
  6. <include>**/*.xml</include>
  7. <include>**/*.properties</include>
  8. </includes>
  9. <filtering>true</filtering>
  10. </resource>
  11. <resource>
  12. <directory>src/main/resources</directory>
  13. <includes>
  14. <include>**/*.xml</include>
  15. <include>**/*.properties</include>
  16. </includes>
  17. <filtering>true</filtering>
  18. </resource>
  19. </resources>
  20. </build>

还是不好使,上网查了快1个小时,咋tm都不对,什么硬往里塞jar包的方法都试过了,都不好使。

最后气的新建了个工程,把写好的文件都导入进去,竟然连上了,好使了,啥错都不报了,简直神了

我又回到这个工程,把这个子工程删掉,重新建一个子工程,导入文件,运行,Bingo~~~~

为什么为什么为什么。。。。。。。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/618459
推荐阅读
相关标签
  

闽ICP备14008679号