方法一:忽略parameterType,加@param("xxx")注解 在mapper接口中加上@param("xxx")注解,则在配置文件中直接用即可 List<Map<String, Object>> getDataByTime(@Param("startTime") String startTime, @Param("endTime") String endTime, @Param("platformId") Long platformId); <select resultType="java.util.Map"> SELECT t.seller_id as sellerId, sum(t.payment_price) as total, FROM trade_orders t WHERE AND t.platform_id = #{platformId} <if test="startTime != null and startTime != ''"> AND <![CDATA[t.order_time >= #{startTime}]]> </if> <if test="endTime != null and endTime != ''"> AND <![CDATA[t.order_time <= #{endTime}]]> </if> GROUP BY t.seller_id </select> 方法二:忽略parameterType,不加@param("xxx")注解 用#{index},是第几个就用第几个的索引,索引从0开始 List<Map<String, Object>> getDataByTime(String startTime, String endTime, Long platformId); <select resultType="java.util.Map"> SELECT t.seller_id as sellerId, sum(t.payment_price) as total, FROM trade_orders t WHERE AND t.platform_id = #{3} <if test="startTime != null and startTime != ''"> AND <![CDATA[t.order_time >= #{0}]]> </if> <if test="endTime != null and endTime != ''"> AND <![CDATA[t.order_time <= #{1}]]> </if> GROUP BY t.seller_id </select> 方法三:使用Map封装参数,parameterType=“hashmap” 封装好后,直接在配置文件引用#{key}即可 List<Map<String, Object>> getDataByTime(HashMap map); <select parameterType="hashmap" resultType="java.util.Map"> SELECT t.seller_id as sellerId, sum(t.payment_price) as total, FROM trade_orders t WHERE AND t.platform_id = #{platformId} <if test="startTime != null and startTime != ''"> AND <![CDATA[t.order_time >= #{startTime}]]> </if> <if test="endTime != null and endTime != ''"> AND <![CDATA[t.order_time <= #{endTime}]]> </if> GROUP BY t.seller_id </select> 方法四:使用List封装参数
mapper配置文件使用循环list
List<Map<String, Object>> getDataByTime(List<String> list); <select resultType="java.util.Map"> select XX from trade_orders where id in <foreach item="item" index="index" collection="list" open="(" separator="," close=")"> #{item} </foreach> </select>