1.Type interface com.yd.dao.UserMapper is not known to the MapperRegistry.
出现这个错误是因为没有在Mybatis的核心文件中注册接口对应的xml文件。
需要在Mybatis的核心配置文件中,将所有的xml文件都进行注册。
<mappers>
<!--每一个mybatis的xml都需要在核心配置文件中进行注册-->
<mapper resource="com/yd/dao/UserMapper.xml"></mapper>
</mappers>
2.java.lang.ExceptionInInitializerError
maven工程的资源过滤问题,maven项目默认不会将xml文件都编译。
手动配置资源过滤即可。
<!--在build中配置resources,来防止我们资源导出失败的问题-->
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
3.Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<!--导入DJBC驱动-->
<property name="driver" value="com.mysql.jdbc.Driver"/>
<!--连接数据库URL-->
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&useUnicode=true&characterEncoding=UTF-8"/>
<!--配置用户名-->
<property name="username" value="root"/>
<!--配置密码-->
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
注意配置文件中的 数据库连接URL 最好别瞎写配置参数。
修改成如下代码即可。
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<!--导入DJBC驱动-->
<property name="driver" value="com.mysql.jdbc.Driver"/>
<!--连接数据库URL-->
<property name="url" value="jdbc:mysql://localhost:3306/mybatis"/>
<!--配置用户名-->
<property name="username" value="root"/>
<!--配置密码-->
<property name="password" value="root"/>
</dataSource>
</environment>
4.Caused by: java.io.IOException: Could not find resource com/yd/dao/UserMapper.xml
出现这个错误直接断定为maven配置中忘记配置忽略文件。
<build>
<!--配置maven对resources文件过滤-->
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
5.Caused by: java.sql.SQLException: Error setting driver on UnpooledDataSource. Cause: java.lang.ClassNotFoundException: Cannot find class: #{driver}
配置文件中获取db.properties文件中的参数问题,应用${}获取而不采用#{}。
<dataSource type="POOLED">
<!--导入DJBC驱动-->
<property name="driver" value="${driver}"/>
<!--连接数据库URL-->
<property name="url" value="${url}"/>
<!--配置用户名-->
<property name="username" value="${username}"/>
<!--配置密码-->
<property name="password" value="${password}"/>
</dataSource>
6. org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.yd.mapper.UserMapper.getUser
因为mapper接口中的方法名字没有和配置文件中的id相互对应。
public interface UserMapper {
public List<User> getUser();
}
<?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.yd.mapper.UserMapper">
<select id="getUser" resultType="com.yd.pojo.User">
select * from user
</select>
</mapper>
因为是通过xml文件中的id去接口所以需要mapper接口中的方法名和xml配置文件中的id名一致,才能找得到。