问题描述
学习黑马课程时,在idea
中运行tomcat
时查不到数据,错误信息如下:
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 P l a i n M e t h o d I n v o k e r . i n v o k e ( M a p p e r P r o x y . j a v a : 152 ) a t o r g . a p a c h e . i b a t i s . b i n d i n g . M a p p e r P r o x y . i n v o k e ( M a p p e r P r o x y . j a v a : 85 ) a t c o m . s u n . p r o x y . PlainMethodInvoker.invoke(MapperProxy.java:152) at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:85) at com.sun.proxy. PlainMethodInvoker.invoke(MapperProxy.java:152)atorg.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:85)atcom.sun.proxy.Proxy32.selectTotalCount(Unknown Source)
at com.itheima.service.impl.BrandServiceImpl.selectByPage(BrandServiceImpl.java:70)
at com.itheima.web.servlet.BrandServlet.selectByPage(BrandServlet.java:71)
… 25 more
原因分析
出错原因是方法selectTotalCount
的返回值类型为int
,执行方法时却试图返回null
,而int
的默认值为0
,与null
不兼容,需要把int
类型换成其包装类型Integer
。
解决方案
- 修改
pojo
包下的PageBean
类,将totalCount
的类型改为Integer
,getter
与setter
方法的类型也要改:
- 修改
dao层
的BrandMapper
类,将方法selectTotalCount
的返回值类型改为Integer
:
- 修改
service层
的BrandServiceImpl
类,将方法selectByPage
中totalCount
的类型改为Integer
: