我在做分页查询时报错,场景是这样的:
- mapper.xml 中sql语句如下
<select id="selectTotalCount" resultMap="brandResultMap">
select count(*) from tb_brand;
</select> - dao层接口如下
int selectTotalCount();
- Service层
public PageBean<Brand> SelectByPage(int currentPage, int pageSize) {
SqlSession sqlSession = factory.openSession();
BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
int begin = (currentPage-1)*pageSize;
int size = pageSize;
List<Brand> brands = brandMapper.selectByPage(begin, size);
int totalCount = brandMapper.selectTotalCount();
PageBean<Brand> pg = new PageBean<>();
pg.setRows(brands);
pg.setTotalCount(totalCount);
sqlSession.close();
return pg;
}报错如下
Caused by: org.apache.ibatis.binding.BindingException: Mapper method 'com.itheima.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 com.sun.proxy.$Proxy28.selectTotalCount(Unknown Source)
at com.itheima.service_20211015_114634.impl.BrandServiceImpl.SelectByPage(BrandServiceImpl.java:50)
at com.itheima.web.servlet.BrandServlet.selectByPage(BrandServlet.java:57)
... 25 more网上查询后发现必须将dao层的int数值类型改成Integer类型,修改后还是报错,后来又查到要把mapper.java和mapper.xml文件中的所有int类型都要改成Integer,全都修改后,终于运行成功