java.lang.IllegalStateException: Cannot load driver class: com.mysql.cj.jdbc.Driver

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘userController’: Unsatisfied dependency expressed through field ‘userService’; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘userServiceImpl’: Unsatisfied dependency expressed through field ‘userMapper’; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘userMapper’ defined in file [C:\Users\Administrator\Desktop\tdkj\zljd\target\classes\com\tdkj\mapper\UserMapper.class]: Unsatisfied dependency expressed through bean property ‘sqlSessionFactory’; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘sqlSessionFactory’ defined in class path resource [org/mybatis/spring/boot/autoconfigure/MybatisAutoConfiguration.class]: Unsatisfied dependency expressed through method ‘sqlSessionFactory’ parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘dataSource’ defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Generic.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]: Factory method ‘dataSource’ threw exception; nested exception is java.lang.IllegalStateException: Cannot load driver class: com.mysql.cj.jdbc.Driver

以上为原本报错信息,大意为 创建userService 是找到不到 名为userServiceImpl的依赖项 找不到名为UserMapper 的Bean 找不到dataSource 等 之后开始在 是否注入Bean方向开始找 发现 @Server @Mapper 等注解都没有问题,然后开始看最后一句 nested exception is java.lang.IllegalStateException: Cannot load driver class: com.mysql.cj.jdbc.Driver 发现了问题 说是驱动有问题,
之后查看pom文件
java.lang.IllegalStateException: Cannot load driver class: com.mysql.cj.jdbc.Driver
看到用的版本是5.1.25 在上篇文章的配置文件中写到过 mysql 6以上的才需要讲驱动配置改为com.mysql.cj.jdbc.Driver 6以下直接用 com.mysql.jdbc.Driver 即可
java.lang.IllegalStateException: Cannot load driver class: com.mysql.cj.jdbc.Driver
修改过后,Run起来啦java.lang.IllegalStateException: Cannot load driver class: com.mysql.cj.jdbc.Driver

我会不定时记录一些日常工作遇到的问题,希望对你有所帮助