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 ...
随机推荐
- android的hook方面知识点
android hook分为另种: native层hook---理解ELF文件 java层---虚拟机特性和Java上的反射的作用 注入代码: 存放在哪? 用mmap函数分配临时内存来完成代码存放,对 ...
- 20145326《Java程序设计》实验二Java面向对象程序设计实验报告
20145326<Java程序设计>实验二Java面向对象程序设计实验报告 实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S.O. ...
- Android 开机Process xxx (pid xxxx) has died问题分析
系统中有一个监听BOOT_COMPLETED广播的自启应用,概率性出现启动后被kill掉的现象.Log如下: - :: I ActivityManager: Process com.test.xxx ...
- shell 输入不显示在监视器上
#!/bin/bash read -s -p "Enter your password:" pass echo "your password is $pass" ...
- 在多节点上运行分布式Intel Caffe
一般有2种并行模式:数据并行(Data parallelism)和模型并行(model parallelism). 在模型并行化( model parallelism )方法里,分布式系统中的不同机器 ...
- [sql]java.sql.Types的具体对应值(jdbcType)
public final static int BIT = -7; public final static int TINYINT = -6; public final static int ...
- 手把手教你如何加入到github的开源世界
我曾经一直想加入到开源项目中,但是因为没有人指导流程,网上看了很多,基本都是说了个大概,如果你也是一个初出茅庐的人,那么,我将以自己提交的一次开源代码为例,教会你步入开源的世界. 1,首先登陆到htt ...
- Java线程状态分析
Java线程的生命周期中,存在几种状态.在Thread类里有一个枚举类型State,定义了线程的几种状态,分别有: NEW: 线程创建之后,但是还没有启动(not yet started).这时候它的 ...
- 10046event vs 10053event做了哪些操作
10046 event 告诉我们oracle做了哪些事情,做的怎么样.10053 event 告诉我们oracle优化器怎么想的,再给出成本最低的最终的优化方案,考虑了哪些执行计划拿了哪些统计信息
- C++轮子队
团队Github地址:https://github.com/Pryriat/2048.git 团队展示: 队名:C++轮子队 队员组成: 黄家承(队长) 学号:3116005 ...