attempted to return null from a methodwith a primitive return type (int).及count(*)返回null的解决

在java的学习过程中会遇到各种各样的问题,以下问题就是我遇到的有关mybatis相关的问题

首先是控制台报错:

Caused by: org.apache.ibatis.binding.BindingException: Mapper method 'com.xxx.mapper.BrandMapper.selectTotalCount attempted to return null from a method with a primitive return type (int).
    at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:102)
    at org.apache.ibatis.binding.MapperProxy$PlainMethodInvoker.invoke(MapperProxy.java:152)
    at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:85)
    at jdk.proxy4/jdk.proxy4.$Proxy28.selectTotalCount(Unknown Source)
    at com.itheima.service.impl.BrandServiceImpl.selectByPage(BrandServiceImpl.java:63)
    at com.itheima.web.BrandServlet.selectByPage(BrandServlet.java:61)
    ... 25 more

    <select id="selectTotalCount" resultMap="brandResultMap">
        select count(*) from tb_brand;
    </select>

首先是报出 Mapper method 'com.xxx.mapper.BrandMapper.selectTotalCount attempted to return null from a method with a primitive return type (int). 的错误,在CSDN上搜索相关的内容,发现是将mapper.xml和mapper.java中的变量设置为int,后改为Interger后控制台不会报出上述错误,但是selectTotalCount返回的值始终是null

 @Override
    public PageBean<Brand> selectByPage(int currentPage, int pageSize) {
        SqlSession sqlSession = factory.openSession();
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        /*
        * (currentPage - 1)*pageSize
        * pageSize
        * */
        int begin=(currentPage - 1)*pageSize;
        int size=pageSize;
        Integer totalCount = mapper.selectTotalCount();
        List<Brand> brands = mapper.selectByPage(begin, size);
        PageBean<Brand> pageBean = new PageBean<>();
        pageBean.setTotalCount(totalCount);
        pageBean.setRows(brands);
        System.out.println("Impl"+totalCount);
        sqlSession.close();
        return pageBean;
    }

于是乎查找各种资料,但始终没能够解决问题,知道后面才发现mapper.xml中

select count(*) from tb_brand;

不能设置为resultmap应该为resultType="Interger",修改后运行成功 

    <select id="selectTotalCount" resultType="integer">
        select count(*) from tb_brand;
    </select>

此错误虽然简单,但是浪费了我大概两个小时查找问题所在,以后对每个参数都应该仔细认真对待。 

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值