Mybatis 提示java.math.BigDecimal cannot be cast to java.lang.Integer

  • Post author:
  • Post category:java


今天在编写统计模块时,出现如下错误信息:

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

核心代码:

<select id="getSum" parameterType="map" resultType="java.util.HashMap">
			select
				sum(draw_num) as drawNum ,  
				sum(base_num) as baseNum,  
				sum(photo_num) as photoNum, 
				sum(negative_num) as negativeNum  
			from ucas_file_info where 1 = 1 
			<include refid="condition"></include>
	</select>

接口定义

List<Map> getSum(Map<String, Object> paramter);

service 实现:

      List<Map> sum = fileMapper.getSum(paramter);
			sum.stream().forEach(item ->{
				
				if(item.get("drawNum") != null){
					BigDecimal drawNum = (BigDecimal) item.get("drawNums");
					result.put("drawNum", drawNum.longValue());
				}
				if(item.get("baseNum") != null){
					BigDecimal baseNum = (BigDecimal) item.get("baseNums");
					result.put("baseNum", baseNum.longValue());
				}
				if(item.get("photoNum") != null){
					BigDecimal photoNum =(BigDecimal) item.get("photoNums");
					result.put("photoNum", photoNum.longValue());
				}
				if(item.get("negativeNum") != null){
					BigDecimal negativeNum = (BigDecimal) item.get("negativeNums");
					result.put("negativeNum", negativeNum.longValue());
				}
			});

错误原因:

mysql sum() 函数的结果映射为 java.math.BigDecimal ,不能直接转换为java.lang.Integer 或者是java.lang.Long类型



版权声明:本文为zhouzhiwengang原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。