MyBatis 错误:Mapper 方法试图返回 null,解决方法
报错原因是 Mapper 方法 'org.example.dao.AdminDao.deletById' 试图从一个带有原始返回类型(int)的方法返回 null。在 MyBatis 中,对于原始返回类型(如 int,boolean 等),不能返回 null 值。
要解决这个问题,可以在 Mapper 接口的方法中将返回类型改为包装类型(如 Integer,Boolean 等),以允许返回 null 值。如果不希望返回 null 值,可以在方法中添加逻辑来处理 null 情况。
在这种情况下,可以将 AdminDao 接口中的 deletById 方法的返回类型改为 Integer,并在方法中添加逻辑来处理 null 情况。例如:
public Integer deletById(Integer id) {
// 添加逻辑来处理 null 情况
if (id == null) {
return 0; // 或者抛出异常
}
// 其他处理逻辑
// ...
}
将返回类型改为 Integer 后,调用该方法时,如果返回 null,可以根据需要进行处理。例如,可以将 null 视为 0 或抛出异常,具体取决于业务需求。
原文地址: https://www.cveoy.top/t/topic/o7dT 著作权归作者所有。请勿转载和采集!