MySQL分段统计SQL写法 与 Mybatis 异常 java.math.BigDecimal cannot be cast to java.lang.Integer
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 < 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
</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的更多相关文章
- 初学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 ...
- mysql连接报java.math.BigInteger cannot be cast to java.lang.Long异常
使用hibernate出现以下错误 java.sql.SQLException: java.lang.ClassCastException: java.math.BigInteger cannot b ...
- 【问题解决:连接异常】 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 ...
- 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 ...
- 【添加最新版本的mysql的jdbc连接jar包】java.math.BigInteger cannot be cast to java.lang.Long异常
[问题描述] 从我的电脑把项目拷贝到guo小中的win8电脑,but出现了那个错误,估计他的mysql是最新版本的. [如何下载连接jar包] 链接:https://pan.baidu.com/s/1 ...
- 连接Mysql时出现java.math.BigInteger cannot be cast to java.lang.Long问题
今天遇见这样一个坑.在连接数据库进行查询数据时,大家可能会遇见这样一个问题:java.math.BigInteger cannot be cast to java.lang.Long,然后去检查代码中 ...
- Mysql 分段统计
今天遇到个小问题觉得挺有意思,与大家分享. 需求是这样的,对数据库中的一张表做按时间的分段统计,结果只要每个区间的数量. select YEAR(create_time) as nian,MONTH( ...
- mysql 分段统计数据
一个简单的分段统计的问题:student 表{id,name,score} 字段,统计各个分数段的人数.规则:60以下不及格,60-80良,80-100优. SELECT sum(CASE when ...
- MYSQL分段统计
产品表 CREATE TABLE `product` ( `product_id` int(11) NOT NULL AUTO_INCREMENT, `product_model` varchar(2 ...
随机推荐
- 20155201 2016-2017-2 《Java程序设计》第二周学习总结
20155201 2016-2017-2 <Java程序设计>第二周学习总结 教材学习内容总结 编译运行P55各种类型可储存的数值范围代码,截图: 常用格式控制符: 符号 说明 %% 表示 ...
- 20145204Android开发基础
实验四 20145204Android开发基础 实验名称 Android开发基础 实验内容 基于Android Studio开发简单的Android应用并部署测试; 了解Android组件.布局管理器 ...
- LA 7278 Game of Cards(SG函数)
https://vjudge.net/problem/UVALive-7278 题意: 两个人玩游戏,现在有n堆牌,轮到自己时,先在牌堆中选一堆牌,先在牌堆中选择拿走0~k张牌(至少得剩下一张),然后 ...
- JAVA-随机生成四则运算
代码如下: package jiajianchengchu; import java.util.*; public class jisuan { public static String yuefen ...
- Android之获取输入用户名与密码发送短信
打算在过两三天的时间我就要准备出发去浙江了,所以把之前的资料来个总结. 这都是在课堂上做过的作业. 好了,废话少说,直接上代码. 步骤: 1.设置单击事件2.获取输入的QQ号码与密码3.判断输入获取的 ...
- AtomicLong可以被原子地读取和写入的底层long值的操作
java.util.concurrent.atomic.AtomicLong类提供了可以被原子地读取和写入的底层long值的操作,并且还包含高级原子操作. AtomicLong支持基础long类型变量 ...
- hdu4965矩阵快速幂
这题不能直接按常规做啊,因为数组根本就开不下,转换思维A(B*A)^(n*n-1)B 这样的话数组B*A就是10*10了,然后快速幂就行了 刚开始数组都开小了,tle,还找了半天bug...还有就是定 ...
- 外汇EA(LRY_FX_Robot_V5)
EA介绍 EA类型是马丁+策略,EA主要功能有风控设置(预付款.浮亏.加仓层数等达到多少进行操作).移动止损(包括隐藏移动止损).帮我操作手动单子(如果你开了首仓不会操作这个功能可参帮你加仓平仓移动止 ...
- Java实现冒泡排序,选择排序,插入排序
冒泡排序: 思想: 冒泡排序重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来.走访数列的工作是重复地进行直到没有再需要交换,也就是说排序完成 特点:比较稳定,排序数较小是 ...
- Unable to connect to any of the specified MySQL hosts.
c#连接Mysql数据建立连接时提示:Unable to connect to any of the specified MySQL hosts. 出现此错误的原因是Server(数据库服务器IP地址 ...