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. QT+qtablewidget自定义表头【合并单元格】

    1.把下列文件放在工程中[已上传到我的文件中] 2.代码 auto *headview = new HHeadViewClass(Qt::Horizontal, ui.tableWidget); he ...

  2. 如何生成ssh密钥对

    答:执行以下命令即可,生成的密钥对在~/.ssh下,会生成两个文件,一个id_rsa和id_rsa.pub,前者是私钥,后者是公钥 ssh-keygen -t rsa -C "your_em ...

  3. luogu p3366 最小生成树模板

    倒腾了一个小时  自己也没去看网上的 总算自己能写出来模板了 kruskal //最小生成树 每次找最短的边 #include<bits/stdc++.h> using namespace ...

  4. 如何用js创建表格?

    1.用js创建表格 <script> function createTable(){ //创建表格 //创建对象 //window下面的属性方法可以把window去掉或者写上 var ta ...

  5. MyBatis基本工作原理

    Application是程序员开发的Java代码,蓝色为MyBatis框架. API是MyBatis提供的增删改查等功能接口. 老式SQL写法我们在Dao中写SQL: SELECT * FROM us ...

  6. G_M_C_美食节

    美食节 题解:学习了动态加边,可以说是进一步理解了网络流.具体思路就是考虑每一道菜,如果这是该位厨师最后一次做,那么等待时间就是做这道菜的时间,如果是倒数第二次做,就要两倍时间(目前做了一次,后面还有 ...

  7. P4行为模型BMV2安装

    前提:依赖关系请移步上篇博客.P4行为模型BMV2依赖关系安装:thrift nanomsg nnpy安装 以及,要把下面这些东西装好. On Ubuntu 14.04, the following ...

  8. Codeforces Round #417 (Div. 2) B. Sagheer, the Hausmeister

    http://codeforces.com/contest/812/problem/B 题意: 有n层楼,每层楼有m个房间,1表示灯开着,0表示灯关了.最两侧的是楼梯. 现在每从一个房间移动到另一个房 ...

  9. Tinkoff Challenge - Elimination Round D. Presents in Bankopolis(区间DP)

    http://codeforces.com/contest/793/problem/D 题意:给出一些点和他们之间的距离,是有向的,这些点从1~n顺序排列,现在选出k个点组成一条路径,使他们之间的距离 ...

  10. Question: Should I use reads with good quality but failed-vendor flag?--biostart for vendor quality

    https://www.biostars.org/p/198405/ Quick question is: I have some mapped reads in bam file which hav ...