编程知识 cdmana.com

The int type of MySQL, analyze the strange phenomenon that the return type is bigdicemal

Through the measured :mybatis Medium sql sentence int type java Several cases of receiving .

1. use resultType="int" ( Return type with int)

image.png

1.1 If resultType="int", id No calculations ,

<select id="getTest" resultType="int">
    select id from ting_cmdb_physical_equipment where id=2897;
</select>

Show results ,

java It can be used  int  receive .

image.png

1.2 If resultType="int", id Do calculations ,

<select id="getTest" resultType="int">
    select id-1 as id 
    from ting_cmdb_physical_equipment where id=2897;
</select>

Show results :

   java It can be used int receive 

image.png
1.3 id Do aggregate operations

<select id="getTest" resultType="int">
    select sum(id) as id 
    from ting_cmdb_physical_equipment where id=2897;
</select>

Calculation results :

  java It can be used int receive 

image.png

2. use resultType="map"( Return type with Map<String,Object>)

image.png

2.1 id No calculations

<select id="getTest" resultType="map">
    select id from ting_cmdb_physical_equipment where id=2897;
</select>

Calculation results :

java It can be used int  receive 

image.png

2.2 id Do calculations

<select id="getTest" resultType="map">
    select id-1 as id 
    from ting_cmdb_physical_equipment where id=2897;
</select>

Calculation results :

java Use long Type reception 

image.png
2.3 id Do aggregate function operations

<select id="getTest" resultType="map">
    select sum(id) as id 
    from ting_cmdb_physical_equipment where id=2897;
</select>

Calculation results :

java use BigDecimal receive 

image.png

3.resultType="map" ( Return type with list<Map<String,Object>>)

image.png

3.1 id No calculations

<select id="getTest" resultType="map">
    select id as id
    from ting_cmdb_physical_equipment where id=2897;
</select>

Calculation results :

java It can be used int receive 

image.png

3.2 id Do calculations

<select id="getTest" resultType="map">
    select id-1 as id 
    from ting_cmdb_physical_equipment where id=2897;
</select>

Calculation results

java It can be used long receive 

image.png
3.3 id Do aggregate operations

<select id="getTest" resultType="map">
    select sum(id) as id 
    from ting_cmdb_physical_equipment where id=2897;
</select>

Calculation results :

java It can be used BigDecimal receive 

image.png

summary

type \int value No calculations Did the operation Did the aggregation operation
int int int int
Map<String,Object> int long BigDecimal
List<String,Object> int long BigDecimal

Why does it show up BigDecimal Type? ,
After the above test :
We can draw

  1. It's using Map<String,Object>,List<Map<String,Object>> receive ,
  2. sql In the sentence int The value of the type does Aggregate operations ,

The above two conditions are satisfied .
java To get in value The value of will become BigDecimal type

版权声明
本文为[.TsXMA]所创,转载请带上原文链接,感谢
https://cdmana.com/2020/12/20201224100315740e.html

Scroll to Top