mabatis返回map结果集

1,如果你返回的数据只有一条

xml中:
<select id="searchncomedateByInvestID" resultMap="java.util.HashMap">
    select
    t1.invest_id                      ,
    cast(t1.modify_time AS DATE) modify_time
    from t_c_wh_redeeminfo t1
    where 1=1
    and t1.invest_id =#{investId}
</select>
dao中:
Map<String,Object> searchncomedateByInvestID(investId);

2,如果插入多条

<resultMap id="getAllSetDaysResult"   type="HashMap">
        <result property="investid" column="invest_id" jdbcType="VARCHAR" />
        <result property="modifytime" column="modify_time" jdbcType="DATE"/>

</resultMap>
<select id="searchncomedateByInvestID" parameterType="java.util.List" resultMap="getAllSetDaysResult">
    select
    t1.invest_id                      ,
    cast(t1.modify_time AS DATE) modify_time
    from t_c_wh_redeeminfo t1
    where 1=1
    and t1.invest_id in
    <foreach collection="list" item="investId" index="index"  open="(" close=")" separator=",">
        #{investId}
    </foreach>
</select>
dao中:
List<Map<String, Object>> searchncomedateByInvestID(List<String> preinvestList);

主要是参考大神的笔记:https://www.cnblogs.com/jwdd/p/10046270.html

 

 

 

使用mybatis返回map集合类型的数据所遇到的报错。

1:

 

问题描述:从数据库取count、sum等函数的值需要转化成Integer的时候出现 
java.math.BigDecimal cannot be cast to java.lang.Integer的报错 

<select id="selectHeaderConfirmCount" resultType="java.util.Map">
        select
            TENANT_ID tenantId,COUNT(1) confirmCount
        from
            atl_spuc_demand_forecast_header t
        where
            t.status = #{status}
        GROUP BY
            TENANT_ID
    </select>

其中 tenantId,还有confirmCount都是数字,所以mybatis框架进行返回的时候,返回的都是BigDecimal类型的数据,你在上层进行接收的时候,如果使用Integer类型进行接收的时候,就会报这个错误。

解决方案:使用BigDecimal进行接收就行了。(自己测试过)

或者参考以下过程(没有测试过)

Object object = map.get("id");
Integer.parseInt(String.valueOf(object ));

 

posted @ 2020-12-21 09:19  czzh  阅读(300)  评论(0)    收藏  举报