MySQL segmentation statistics SQL notation and Mybatis error: Java.math.BigDecimal cannot be cast to Java.lang.Integer

Source: Internet
Author: User

Mysql> Select     - sum( Case  whenScore< -  Then 1 Else 0 End) as '<60',     - sum( Case  whenScore>= -  andScore<= the  Then 1 Else 0 End) as '60~69',     - sum( Case  whenScore>= -  andScore<= -  Then 1 Else 0 End) as '70~79',     - sum( Case  whenScore>= the  andScore<= the  Then 1 Else 0 End) as '80~89',     - sum( Case  whenScore>- -  Then 1 Else 0 End) as '>=90'     -  fromStudent_course - ;+------+-------+-------+-------+------+| < -  |  -~ the |  -~ - |  the~ the | >= - |+------+-------+-------+-------+------+|    2 |     1 |     1 |     1 |   Ten |+------+-------+-------+-------+------+

With MyBatis, the XML file is configured as follows:

  <SelectID= "Getscorestatistics"Resulttype= "Map">Select sum ( case when score&lt;1 Else 0 end) as '&lt;sum ( case when score&gt;= Score&lt;= 1 Else 0 end) as ' 60~69 ', sum ( case when score&gt;= Score and&lt;= 1 Else 0 end) as ' 70~79 ', sum ( case when score&gt;= Score&lt;= 1 Else 0 end) as ' 80~89 ', sum ( case when score&gt;= 1 Else 0 end) as '&gt;=90 ' from Student_course</Select>

Mapper Interface:

Map<string, object> getscorestatistics ();

Note that if you use Map<string, integer> as the return value, you will get an error:

Java.math.BigDecimal cannot is cast to Java.lang.Integer

The reason is that the result of sum () is handled as Java.math.BigDecimal, and he cannot be converted directly to Java.lang.Integer, so the error is.

The correct approach is to return map<string, Object>

{"<60": 2, "60~69": 1, "70~79": 1, "80~89": 1, ">=90": 5}

int count1 = Integer.parseint (Resultmap.get ("<60"). toString ());

You can get the correct result by using the toString () method of type object and then Integer.parseint ().

Of course we can also directly return: Map<string, bigdecimal> getscorestatistics ();

Then get the values we need by Bigdecimal.intvalue ():

    Map<string, bigdecimal> getscorestatistics ();
map<string, bigdecimal> resultmap = This.studentCourseMapper.getScoreStatistics ();
int count = Resultmap.get ("<60"). Intvalue ();

Summary :

1) the sum () in SQL returns the return value returned as BigDecimal in MyBatis, so we have two ways to handle it:

1> Returns an Object value and then Integer.parseint (obj.tostring ()) to get the int value;

2> Returns the BigDecimal value and then obtains the desired value through Bigdecimal.intvalue () , it should be said that we recommend using the second method.

2) MySQL Segmentation statistics method:sum (case when score< 1 Else 0 end)

MySQL segmentation statistics SQL notation and Mybatis error: Java.math.BigDecimal cannot be cast to Java.lang.Integer

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.