mybatis使用foreach遍历时同时遍历key和value

要使用UNION ALL合并表的时候发现查询要同时遍历map的key和value来查询之前没写过今天记载一下

mapper.xml如下

	<select id="selectStatisticalDeposit" resultMap="DepositResultResult">
        <foreach collection="map.keys" item="key" separator="UNION ALL">
            SELECT #{key} as 'time',
                count( CASE WHEN order_status != 3 THEN 1 ELSE 0 END ) 'total_money',
                count( CASE WHEN order_type = 1 THEN 1 ELSE 0 END ) AS 'wx_money',
                count( CASE WHEN order_type = 2 THEN 1 ELSE 0 END ) AS 'zfb_money',
                count( CASE WHEN order_status = 1 THEN 1 ELSE 0 END ) AS 'return_money',
                count( CASE WHEN order_status = 2 THEN 1 ELSE 0 END ) AS 'not_return_money'
                FROM
                rental_order
            WHERE
            #{map[${key}]}
        foreach>
    select>

dao接口如下

/**
	 * 押金统计
	 * @param map
	 * @return
	 */
	public List<DepositResult> selectStatisticalDeposit(@Param("map") Map<String,Object> map);

#{map[${key}]}类似map.get(“key”)取当前key对应的value值。由于value是set所以还需要遍历。

你可能感兴趣的:(mybatis需要注意)