MyBatis可能出现的所有异常

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里面&要写成&amp;

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.空指针异常:

解决检查是否重复声明工厂对象:

  • 6
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值