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>