1. Mapper注册问题:
UserMapper is not known to the MapperRegistry.
原因:每一个Mapper.xml都需要在mybatis核心配置文件(mybatis-config.xml)中注册
解决:在mybatis-config.xml中加:注意路径正确且配置文件不能有多余的字符
<mappers> <mapperresource="com/zhou/dao/UserMapper.xml"/> </mappers>
注意此处路径是com/zhou/dao/UserMapper.xml而非com.zhou.dao.UserMapper.xml,写成点就会报错
2. 找不到映射文件:
Could not find resource com/zhou/dao/UserMapper.xml
解决:
<!--在build中配置resources,来防止我们资源导出失败的问题-->
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
3. mapper接口跟mapper.xml文件没有映射问题:
Invalid bound statement (not found):
可能原因:(大部分可能是某些地方敲错了)
①.mapper.xml中的namespace和实际的mapper文件不一致
②.mapper接口中的方法名和mapper.xml中的id标签不一致
③.接口名和xml名字不一样(这个一定要仔细检查,题主就是这个错误,改掉就好了)。
4. mysql版本问题:
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server.
原因:Maven驱动版本与实际连接数据库版本不一致
解决:将驱动改成自己mysql的版本号(在依赖中修改)
5. mysql版本问题:
Caused by: java.sql.SQLException: The server time zone value '�й���ʱ��' is unrecognized or represen
原因:版本与mybatis配置文件中URL规定不匹配
解决:修改URL
①是降低mysql的版本号
②修改URL:
将url=jdbc:mysql://localhost:3306/db1?characterEncoding=utf-8
改成:url=jdbc:mysql://localhost:3306/db1?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
(注意在mybatis-config.xml里面&要写成&)
6. 配置文件的编码问题:
“2 字节的 UTF-8 序列的字节 2 无效”异常处理方法
解决:在pom.xml中加入
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
7.驱动问题:
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary
解决:mybatis配置文件修改加cj
8.问题:打印对象出来一堆地址:没有重写toString
解决:pojo给实体类加上toString
9.空指针异常:
解决:检查是否重复声明工厂对象:
文章评论