mysql> select
-> sum(case when score<60 then 1 else 0 end) as '<60',
-> sum(case when score>=60 and score<=69 then 1 else 0 end) as '60~69',
-> sum(case when score>=70 and score<=79 then 1 else 0 end) as '70~79',
-> sum(case when score>=80 and score<=89 then 1 else 0 end) as '80~89',
-> sum(case when score>-90 then 1 else 0 end) as '>=90'
-> from student_course
-> ;
+------+-------+-------+-------+------+
| <60 | 60~69 | 70~79 | 80~89 | >=90 |
+------+-------+-------+-------+------+
| 2 | 1 | 1 | 1 | 10 |
+------+-------+-------+-------+------+

采用mybatis时,xml文件配置如下处理:

  <select id="getScoreStatistics" resultType="map">
select
sum(case when score &lt; 60 then 1 else 0 end) as '&lt;60',
sum(case when score &gt;= 60 and score &lt;= 69 then 1 else 0 end) as '60~69',
sum(case when score &gt;= 70 and score &lt;= 79 then 1 else 0 end) as '70~79',
sum(case when score &gt;= 80 and score &lt;= 89 then 1 else 0 end) as '80~89',
sum(case when score &gt;= 90 then 1 else 0 end) as '&gt;=90'
from student_course
</select>

mapper接口:

Map<String, Object> getScoreStatistics();

注意,这里如果使用 Map<String, Integer> 作为返回值,会报错:

java.math.BigDecimal cannot be cast to java.lang.Integer

原因是,sum() 的结果是作为 java.math.BigDecimal 来处理的, 而他不能直接转换成 java.lang.Integer,所以报错。

正确的处理方法是,返回 Map<String, Object>,然后

{"<60":2,"60~69":1,"70~79":1,"80~89":1,">=90":5}

int count1 = Integer.parseInt(resultMap.get("<60").toString());

通过Object类型的 toString()方法,然后 Integer.parseInt() 这里才能得到正确的结果。

当然我们也可以直接返回:Map<String, BigDecimal> getScoreStatistics();

然后通过BigDecimal.intValue() 来获得我们需要的值:

    Map<String, BigDecimal> getScoreStatistics();
Map<String, BigDecimal> resultMap = this.studentCourseMapper.getScoreStatistics();
int count = resultMap.get("<60").intValue();

 总结

1)sql中的 sum() 返回返回值在mybatis中是作为BigDecimal来返回的,所以我们有两种方法来处理:

1> 返回 Object 值,然后通过 Integer.parseInt(obj.toString()); 来得到int值;

2> 返回 BigDecimal 值,然后通过 BigDecimal.intValue()得到需要的值,应该说我们推荐使用第二种方法。

2)mysql分段统计方法:sum(case when score<60 then 1 else 0 end)

MySQL分段统计SQL写法 与 Mybatis 异常 java.math.BigDecimal cannot be cast to java.lang.Integer的更多相关文章

  1. 初学MyBatis(踩坑)Error querying database. Cause: java.sql.SQLException: java.lang.ClassCastException: java.math.BigInteger cannot be cast to java.lang.Long

    最近在学习Mybatis,代码全部根据教程写好了,一运行结果报了一个错误,主要错误内容: Caused by: org.apache.ibatis.exceptions.PersistenceExce ...

  2. mysql连接报java.math.BigInteger cannot be cast to java.lang.Long异常

    使用hibernate出现以下错误 java.sql.SQLException: java.lang.ClassCastException: java.math.BigInteger cannot b ...

  3. 【问题解决:连接异常】 java.lang.ClassCastException: java.math.BigInteger cannot be cast to java.lang.Long

    问题描述: MySQL更新到8.0.11之后连接数据库时会报出错误 Your login attempt was not successful, try again.Reason: Could not ...

  4. Spring boot启动时报 java.sql.SQLException: java.lang.ClassCastException: java.math.BigInteger cannot be cast to java.lang.Long错误

    Spring boot启动时报 java.sql.SQLException: java.lang.ClassCastException: java.math.BigInteger cannot be ...

  5. 【添加最新版本的mysql的jdbc连接jar包】java.math.BigInteger cannot be cast to java.lang.Long异常

    [问题描述] 从我的电脑把项目拷贝到guo小中的win8电脑,but出现了那个错误,估计他的mysql是最新版本的. [如何下载连接jar包] 链接:https://pan.baidu.com/s/1 ...

  6. 连接Mysql时出现java.math.BigInteger cannot be cast to java.lang.Long问题

    今天遇见这样一个坑.在连接数据库进行查询数据时,大家可能会遇见这样一个问题:java.math.BigInteger cannot be cast to java.lang.Long,然后去检查代码中 ...

  7. Mysql 分段统计

    今天遇到个小问题觉得挺有意思,与大家分享. 需求是这样的,对数据库中的一张表做按时间的分段统计,结果只要每个区间的数量. select YEAR(create_time) as nian,MONTH( ...

  8. mysql 分段统计数据

    一个简单的分段统计的问题:student 表{id,name,score} 字段,统计各个分数段的人数.规则:60以下不及格,60-80良,80-100优. SELECT sum(CASE when ...

  9. MYSQL分段统计

    产品表 CREATE TABLE `product` ( `product_id` int(11) NOT NULL AUTO_INCREMENT, `product_model` varchar(2 ...

随机推荐

  1. Git提交撤销

    场景:本地修改代码后,执行commit提交,但此时想撤销该提交. 执行命令: git reflog   查看所有commit历史 git reset --hard commitid     将当前远程 ...

  2. SQL server 2012 阻塞分析查询

    最近公司的数据库并发有点大,由于CPU不高.内存不高.硬盘正常.网络也正常等等,但系统还是会卡,所以就怀疑是数据库阻塞导致的,于是去查询资料,看书及经过用以下sql观查,经过几天对数据的分析找到原因并 ...

  3. install ros-indigo-map-server

    sudo apt-get install ros-indigo-map-server

  4. python 字典的合并

    d1 = {, } d2 = {, } d = d1.copy() d.update(d2) print(d)

  5. Visitor(访问者)

    意图: 定义一个操作中的算法的骨架,而将一些步骤延迟到子类中.TemplateMethod 使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤. 适用性: 一次性实现一个算法的不变的部分, ...

  6. oracle RAC的客户端HA配置

    在ORACLE 9i RAC 环境下,为了做到高可用性,需要对客户端的tnsnames.ora这个文件进行配置,在oracle中这样的配置叫做TAF,这个配置不能使用NETCA配置程序生成.其中ORA ...

  7. spring boot: 热部署spring-boot-devtools

    spring boot: 热部署spring-boot-devtools 1引入spring-boot-devtools依赖包 <!-- spring boot devtools 热部署 --& ...

  8. AndroidStudio怎样导入library项目开源库

    AndroidStudio是一款非常强大的android程序开发软件,在里面集成了几乎所有android开发中需要使用的工具,编译.运行.打包.开发.调试等功能一应俱全,可以使用起来非常方便. 今天要 ...

  9. wepy绘制雷达图

    代码如下: <style lang='less'> .radar-canvas2 { width: 690rpx; height: 420rpx; } </style> < ...

  10. IOS-Core Data的使用

    二.Core Data的使用(一) CoreData 1.  常用类和方法   ( 注意需要导入   coredata  框架   ) 表结构:NSEntityDescription 表记录:NSMa ...