mysql数据库在进行统计时候,返回的count()是个long型,sum()返回的是bigDecimal类型,前段需要的是int型故而需要进行转换。
1 <select id="getDeviceRentNum" parameterType="map" resultType="map"> 2 SELECT SUM(IF(f.`rent`=1,1,0)) rentNum, COUNT(d.`id`) deviceTotal 3 FROM d_device d INNER JOIN d_device_fields f ON d.`id`=f.`device_id` 4 WHERE d.`del_flag`=0 AND f.`del_flag`=0 AND d.`company_id`=#{companyId} 5 <if test="productId!=null and productId!=''">AND d.`product_id`=#{productId}</if> 6 </select> 7 <select id="getDeviceSoldNum" parameterType="map" resultType="Integer"> 8 SELECT COUNT(d.id) soldNum FROM d_device_fields f INNER JOIN d_device d ON f.device_id=d.id INNER JOIN d_device_user_bind dub ON d.id=dub.device_id 9 WHERE d.del_flag=0 AND dub.del_flag=0 AND f.rent!=1 AND d.`company_id`=#{companyId} 10 <if test="productId!=null and productId!=''">AND d.`product_id`=#{productId}</if>
Service 层转换
1 if(sign==2){ 2 Map<String, Object> resultMap = new HashMap<String,Object>(); 3 Map<String, Object> rentMap = deviceMapper.getDeviceRentNum(paramMap); 4 Integer soldNum = deviceMapper.getDeviceSoldNum(paramMap); 5 Integer rentNum = ((BigDecimal)rentMap.get("rentNum")).intValue(); 6 String dTotal = ((Long)rentMap.get("deviceTotal")).toString(); 7 Integer deviceTotal = Integer.valueOf(dTotal); 8 Integer forSaleNum = (deviceTotal-soldNum-rentNum); 9 resultMap.put("soldNum",soldNum ); 10 resultMap.put("rentNum", rentNum); 11 resultMap.put("forSaleNum", forSaleNum); 12 resultMap.put("deviceTotal",deviceTotal); 13 return resultMap; 14 }
经过如此转换方可以,否则会报错的,报错如下:
java.lang.ClassCastException: java.lang.Long cannot be cast to java.math.BigDecimal 之类的