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 ...
随机推荐
- linux使用vi浏览python源码
一.背景 2018/8/15,这一天要分析一个python项目,因此需要浏览代码,而我使用的是ubuntu 16.04,于是作此文 二.步骤 2.1 获取生成tags文件的脚本 http://svn. ...
- CodeForces 828C String Reconstruction(并查集思想)
题意:给你n个串,给你每个串在总串中开始的每个位置,问你最小字典序总串. 思路:显然这道题有很多重复填涂的地方,那么这里的时间花费就会特别高. 我们维护一个并查集fa,用fa[i]记录从第i位置开始第 ...
- 【Python】远离 Python 最差实践,避免挖坑
原文链接:http://blog.guoyb.com/2016/12/03/bad-py-style/ 最近在看一些陈年老系统,其中有一些不好的代码习惯遗留下来的坑:加上最近自己也写了一段烂代码导致服 ...
- Bert学习资料
首先是Bert的论文和 attention is all you need的论文 然后是:将nlp预训练 迁移学习的发展从word2vec 到elmo bert https://mp.weixin.q ...
- 最好的移动触摸滑动插件:Swiper
最近在使用的一个移动触摸滑动插件Swiper,功能很强大,跟之前的那个swipe.JS相比,同时支持在PC端滑动,支持上下方向滑动,支持多张图片滑动,支持多屏滑动,凡是你想得到的几乎都可以实现.最近作 ...
- js 面试题总结
面试题解析 window.number = 1; var obj = { number: 4, dbl: (function(){ console.log(obj.number); this.numb ...
- 雷林鹏分享:Ruby XML, XSLT 和 XPath 教程
Ruby XML, XSLT 和 XPath 教程 什么是 XML ? XML 指可扩展标记语言(eXtensible Markup Language). 可扩展标记语言,标准通用标记语言的子集,一种 ...
- ExtJS 6 如何引入Dashboard模版
最近很多人问我在ext js 6+的版本中怎么引入官方的dashboard模版,正好我好久没写博客了,这里我写一篇博客来说明一下. 在这里以ext js 6.2.1版本为例(注:需要安装Sencha ...
- PHP:第五章——字符串转换与比较
<?php header("Content-Type:text/html;charset=utf-8"); //字符串的转换与比较 //1.ord——返回首字符的ASCLL: ...
- 【MVC】ASP.NET MVC 4项目模板的结构简介
引言 在VS2012新建一个窗体验证的MVC 4项目后,可以看到微软已经帮我们做了很多了,项目里面该有的都有了,完全可以看成一个简单网站.作为开发,能理解里面文件结构和作用,也算是半只脚踏进M ...