mybatis resultType=java.util.Map 时注意返回null

时间:2021-08-27 16:21:27

利用mybatis 动态查询一条记录,而返回是一个map(resultType=”java.util.Map”,键是字段名,值是字段值),比如操作数据库是通过存储过程或者是直接传一个String sql="select * from emp where e_id=1"字符串来返回一个map。


1、如果某一列的值为空。查询这条记录时就不显示这列了。《===map中无此对应的键


2、如果这个列是number类型,不能给默认值(比如 ‘0’)。只能为空,这样也会显示不出来《===map中无此对应的键


解决办法:可以利用Oracle的nvl()函数,比如nvl(字段,’0’)表示如果字段为空,则用0字符替代


<select id="queryLogonData" parameterType="java.util.Map" resultType="java.util.Map">
select nvl(aa.DEVICE_CNT,0) DEVICE_CNT, nvl(aa.EVENT_CNT,0) EVENT_CNT, nvl(aa.USER_CNT,0) USER_CNT,
bb.CAL_NUMBER from
(select 0 as DEVICE_CNT,sum(a.event_cnt) as EVENT_CNT,count(distinct(a.user_identifier)) as
USER_CNT,a.CAL_NUMBER
from razor_dw_user_logon_log_day a
where 1 = 1
<if test="version != 'all' and '' != version">
<![CDATA[
AND VERSION =#{version}
]]>
</if>
AND a.APP_KEY IN (SELECT PRODUCTKEY FROM RAZOR_CHANNEL_PRODUCT WHERE PRODUCT_ID = #{productId})
AND a.cal_number between #{startTime} and #{endTime}
group by a.cal_number
order by a.cal_number ASC) aa
right join dim_calendar bb
on aa.cal_number = bb.cal_number
where bb.cal_number between #{startTime} and #{endTime}
order by bb.cal_number ASC
</select>