暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

mybatis mysql int 作为返参接受值 返回结果为null 会报错么

全栈的程序员 2022-04-05
843

喜欢就价格关注吧,持续分享

前言:

mybatis和mysql 是平时在java项目中经常用到的。今天带大家看一个平时不留意的错误。如果在 mapper中用 int 来接收 数据统计的sql。查不到返回null 会报错么?


不知道的请看下文


答案是会报错的。


错误信息

    org.apache.ibatis.binding.BindingException: 
    Mapper method 'com.*******Mapper.countByExample
    attempted to return null from a method
    with a primitive return type (int).

    报错原因

    mapper.xml文件中配置了select返回值为int,但实际的sql查询结果为null (问题出在mapper.xml的情况)

    解决方案

    mapper.java中接口方法的返回值由 int 改为 Integer。 Integer 为int的包装类。可以接收为 null 的返回值。

    文章转载自全栈的程序员,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

    评论