错误信息
org.apache.ibatis.binding.BindingException: Mapper method 'com.*******Mapper.countByExample attempted to return null from a method with a primitive return type (int).
对于这个问题,一般情况下在百度找到的答案可参考以下
http://blog.youkuaiyun.com/just_lion/article/details/9700793
http://houseyoung.cn/?p=73
原因大多为
- mapper.xml文件中配置了select返回值为int,但实际的sql查询结果为null (问题出在mapper.xml的情况)
- mapper.xml中对应select语句的返回值为Integer,而mapper.java中接口方法的返回值为int,所以查不到值时,Integer对应的返回值为null无法转换成int (问题出在mapper.java的情况)
总的来说解决方案有两个
- 修改mapper.xml文件,对select语句或者返回值进行修改
- 修改mapper.java源码,修改出错语句对应的接口方法返回值为可接收null的类型,再于service代码中进行处理
mybatis与pagehelper的组合使用中出现以上异常
参考信息
里面有说到
//获取第1页,10条内容,默认查询总数count
PageHelper.startPage(1, 10);
//紧跟着的第一个select方法会被分页
List list = countryMapper.selectIf(1);
于是乎调用了
PageHelper.startPage(1, 10);
语句后,下一条select语句将不会时执行mapper.xml中原本的语句,而是被修改后的select语句
于是,若一位粗心的程序员使用了
PageHelper.startPage(1, 10);
这句分页代码,接下来却以这样的习惯编码,而没有使用PageHelper的page对象
//分页
PageHelper.startPage(page, rows);
//获取结果总数
Integer total = countByExample(example);
//获取当前页结果
List rows = selectByExample(example);
就会导致分页代码对countByExample进行了分页,而没有对selectByExample分页,这样导致的后果就是若总数为零时会报错,同时将无法实现分页功能