在排错时,发现Mysql 5.x版本8.X连接DB的环境配置有很大差别,所以我们要首先匹配版本号,具体连接区别可以参考: https://www.cnblogs.com/jyroy/p/11276152.html
连接好后,我的环境是:
Mysql5.7.19
mysql-connector-java 5.1.47
jdk 1.8
网上搜到的关于Cannot find class: com.mysql.jdbc.Driver问题一般都是:
1,dependcy version不匹配,比如Mysql8.0的用了mysql-connector-java5.1的驱动;
2,mybatis-config.xml里的url字符串配错,比如多了一个空格
3,或者你的XXXMapper.xml的namespace路径错误。
我仔细检查了这三个问题,发现都是正确。配置文件如下: pom.xml
<?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">
<parent>
<artifactId>mybatis-sky</artifactId>
<groupId>com.kuang</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>mybatis-2</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<!--资源引入标签-->
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>A
<filtering>true</filtering>
</resource>
</resources>
</build>
</project>
mybatis-config.xml
<?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.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&useUnicode=true&characterEncoding=utf8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/kuang/mapper/UserMapper.xml"/>
</mappers>
</configuration>
UserMapper.xml
<?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.kuang.mapper.UserMapper">
<select id="selectUser" resultType="com.kuang.pojo.User">
select * from user
</select>
</mapper>
可以发现都是正确的,但是进行测试依然会出现找不到驱动的场面。
我也是偶然发现解决方法,应该是属于IDE的问题,我用的是InteliJ Idea。其实我们进到UserMapper.xml里
image-20210729214151997.png会发现select标签是黄色的,有一个黄色warning,点开小灯泡:
image-20210729214244126.png选configure data source:
image-20210729214353837.png会看到Drivers里面的Mysql5.1,如果这里你的驱动没装,会显示红色,你直接选择安装即可。
等你回去运行,你会发现,之前的driver缺失问题解决了!
接下来就会遇到第二个大坑:
第二个大坑:error parsing sql mapper configuration. cause: java.io.ioexception: could not find resource com/kuang/dao/usermapper.xml
我已经在mybatis-config.xml里配置率mappers,他依然说找不到,其实就是没有加资源,等加入后就能解决了,这串代码也能解决resources文件夹里资源不被识别的问题。
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>A
<filtering>true</filtering>
</resource>
</resources>
</build>
注意一点:filtering标签里面的属性值是false。
网友评论