Mybatis报错 attempted to return null from a method with a primitive return type (int).

1、报错信息

org.apache.ibatis.binding.BindingException: Mapper method 'xxxMapper.XXX attempted 
to return null from a method with a primitive return type (int).
    at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:93)
    at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:59)
    at com.sun.proxy.$Proxy71.getResourceDataNumsByTitle(Unknown Source)
    at com.haoeasy.news.serviceimpl.ResourceServiceImpl.getResourceDataNumsByTitle(ResourceServiceImpl.java:39)
    at com.haoeasy.news.serviceimpl.ResourceServiceImpl.insertOneResourceData(ResourceServiceImpl.java:23)
    at com.haoeasy.news.news.NewsApplicationTests.getUrlContent(NewsApplicationTests.java:105)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)

 

报错的原因在于sql语句未查询到数据,返回为null。而我们定义的dao层方法是返回为int,就会出现如下这样的提示:
return null from a method with a primitive return type (int).(试图从具有原始返回类型(int)的方法返回null)
Ingeter是int的包装类,int的初值为0,Ingeter的初值为null

2、解决方式:

将dao层的返回类型改为Integer

 

posted @ 2021-08-05 16:19  唏嘘-  阅读(1398)  评论(0编辑  收藏  举报