关于java:mysql的-int-类型刨析返回类型为BigDicemal-类型的奇怪现象

10次阅读

共计 1626 个字符,预计需要花费 5 分钟才能阅读完成。

通过实测:mybatis 中的 sql 语句 int 类型 java 接管的几种状况。

1. 用 resultType=”int”(返回类型用 int)

1.1 如果 resultType=”int”,id 没做运算,

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

显示后果,

java 能够用 int 接管。

1.2 如果 resultType=”int”,id 做运算,

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

显示后果:

   java 能够用 int 接管


1.3 id 做聚合运算

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

运算后果:

  java 能够用 int 接管

2. 用 resultType=”map”(返回类型用 Map<String,Object>)

2.1 id 没做运算

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

运算后果:

java 能够用 int 接管

2.2 id 做运算

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

运算后果:

java 要用 long 类型接管


2.3 id 做聚合函数运算

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

运算后果:

java 用 BigDecimal 接管

3.resultType=”map” (返回类型用 list<Map<String,Object>>)

3.1 id 没做运算

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

运算后果:

java 能够用 int 接管

3.2 id 做运算

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

运算后果

java 能够用 long 接管


3.3 id 做聚合运算

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

运算后果:

java 能够用 BigDecimal 接管

总结

类型 \int 值 没做运算 做了运算 做了聚合运算
int int int int
Map<String,Object> int long BigDecimal
List<String,Object> int long BigDecimal

为什么会呈现 BigDecimal 类型呢,
通过下面的测试:
能够得出

  1. 用的是Map<String,Object>,List<Map<String,Object>> 接管,
  2. sql 语句中 int 类型的值做了 聚合运算

满足上述两个条件。
java 中去取 value 的值就会变成 BigDecimal 类型

正文完
 0