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

发布于:2022-12-23 ⋅ 阅读:(440) ⋅ 点赞:(0)

在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>

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

本文含有隐藏内容,请 开通VIP 后查看

网站公告

今日签到

点亮在社区的每一天
去签到