美文网首页
mysql的 int 类型,刨析返回类型为BigDicemal

mysql的 int 类型,刨析返回类型为BigDicemal

作者: Java柱柱 | 来源:发表于2020-12-24 14:07 被阅读0次

经过实测:mybatis 中的sql语句int类型 java接收的几种情况。

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

mysql的int类型,返回为BigDicemal的奇怪现象

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

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

显示结果,

java可以用 int 接收。
mysql的int类型,返回为BigDicemal的奇怪现象

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接收
mysql的int类型,返回为BigDicemal的奇怪现象

1.3 id 做聚合运算

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

运算结果:

java可以用int接收
mysql的int类型,返回为BigDicemal的奇怪现象

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

mysql的int类型,返回为BigDicemal的奇怪现象

2.1 id没做运算

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

运算结果:

java可以用int 接收
mysql的int类型,返回为BigDicemal的奇怪现象

2.2 id做运算

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

运算结果:

java要用long类型接收
mysql的int类型,返回为BigDicemal的奇怪现象

2.3 id做聚合函数运算

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

运算结果:

java用BigDecimal接收
mysql的int类型,返回为BigDicemal的奇怪现象

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

mysql的int类型,返回为BigDicemal的奇怪现象

3.1 id没做运算

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

运算结果:

java可以用int接收
mysql的int类型,返回为BigDicemal的奇怪现象

3.2 id做运算

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

运算结果

java可以用long接收
mysql的int类型,返回为BigDicemal的奇怪现象

3.3 id做聚合运算

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

运算结果:

java可以用BigDecimal接收
mysql的int类型,返回为BigDicemal的奇怪现象

总结

类型\int值没做运算做了运算做了聚合运算intintintintMap<String,Object>intlongBigDecimalList<String,Object>intlongBigDecimal

为什么会出现BigDecimal类型呢,

经过上面的测试:

可以得出

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

满足上述两个条件。

java中去取value的值就会变成 BigDecimal 类型

来源:https://www.tuicool.com/articles/q2QRNra

相关文章

  • mysql的 int 类型,刨析返回类型为BigDicemal

    经过实测:mybatis 中的sql语句int类型 java接收的几种情况。 1.用resultType="int...

  • 1.变量

    Python推荐用isinstance()来确定变量的类型,例如isinstance(520,int)返回值类型为...

  • 第一章:开始

    1. 编写一个简单的 C++ 程序 main 函数的返回类型必须为 int,即整数类型。int 类型是一种 内置类...

  • # Android之四大组件UI

    Service onStartCommand返回参数说明 方法onStartCommand()的返回值为int类型...

  • c语言指针

    int (*func_p)();//指向int类型函数的指针 int *f();//返回指向int类型指针的函数 ...

  • C++ 复杂声明

    1、方法也是有类型的,方法的类型由返回类型和形参表决定。比如int F (int)的类型就是去掉方法名,int (...

  • Block

    Block的定义 第一个int为返回值类型,myBlock为block名,第二个int为参数类型,num为传入的形...

  • 函数指针

    定义 返回值类型 (* 指针名称)();返回值类型 (* 指针名称)(参数类型1, ...); 声明 int (*...

  • MySQL数据类型

    MySQL学习笔记(2) mysql支持的数据类型 mysql数值类型 关于每个类型的详细信息可以通过 ? int...

  • 《Cpp primer 5》笔记(1)

    第一章 开始 1.1 编写一个简单的程序 main()函数的返回类型必须为int,即整数类型。int是一种内置类型...

网友评论

      本文标题:mysql的 int 类型,刨析返回类型为BigDicemal

      本文链接:https://www.haomeiwen.com/subject/vfdmnktx.html