mybatis中mapper接口的參數設置幾種方法


方法一:忽略parameterType,加@param("xxx")注解

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

List<Map<String, Object>> getDataByTime(@Param("startTime") String startTime, @Param("endTime") String endTime, @Param("platformId") Long platformId);
<select id="getDataByTime" 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 id="getDataByTime" 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 id="getDataByTime" 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配置文件使用foreach標簽循環list

List<Map<String, Object>> getDataByTime(List<String> list);  
<select id="getXXXBeanList" 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> 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM