mybatis中mapper接口的参数设置几种方法

注解

在mapper接口中加上@param("xxx")注解,则在配置文件中直接用即可

List<Map<String, Object>> getDataByTime(@Param("startTime") String startTime, @Param("endTime") String endTime, @Param("platformId") Long platformId);
<select >
      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>

注解

用#{index},是第几个就用第几个的索引,索引从0开始

List<Map<String, Object>> getDataByTime(String startTime, String endTime, Long platformId);
<select >
      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>

parameterType=“hashmap”

封装好后,直接在配置文件引用#{key}即可

List<Map<String, Object>> getDataByTime(HashMap map);
<select >
      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配置文件使用foreach标签循环list

List<Map<String, Object>> getDataByTime(List<String> list);  
<select >
  select XX from trade_orders where id in
  <foreach item="item" index="index" collection="list" open="(" separator="," close=")">  
    #{item}  
  </foreach>  
</select>