MyBatis 傳入參數的問題 (基本數據類型、復雜數據類型)
一、單個參數
1、基本數據類型
(1) 直接使用 #{} 中的參數名與方法中的參數名一致
List<ChargeRuleDO> tests(long id);
<select resultType="com.xxx.bean.ChargeRuleDO">
select * from t_charge_rule t where t.id = #{id}
</select>
(2) 使用注解 #{} 中的參數名與方法中的 @Param() 里的參數名一致
List<ChargeRuleDO> tests(@Param("aid") long bid);
<select resultType="com.xxx.bean.ChargeRuleDO">
select * from t_charge_rule t where t.id = #{aid}
</select>
2、復雜數據類型 (這里主要是指 Java 實體類)
(1) 直接使用 #{} 中的參數名與方法中的參數的屬性名一致
List<ChargeRuleDO> tests(TestQO testQO);
<select resultType="com.xxx.bean.ChargeRuleDO">
select * from t_charge_rule t where t.id = #{id} and t.rule_type=#{ruleType}
</select>
(2) 使用注解 #{} 中的參數名與方法中的 @Param() 里的參數對應的屬性名一致, 而且必須寫成 "#{atestQO.id}" 的格式, 不能簡寫成 "#{id}".
List<ChargeRuleDO> tests(@Param("atestQO") TestQO btestQO);
<select resultType="com.xxx.bean.ChargeRuleDO">
select * from t_charge_rule t where t.id = #{atestQO.id} and t.rule_type=#{atestQO.ruleType}
</select>
二、多個參數
1、基本數據類型
(1) 直接使用 這里改用 #{index}, 其中,#{0} 代表接收的是 dao 層中的第一個參數,#{1} 代表 dao 層中第二參數, 更多參數一致往后加即可。
List<ChargeRuleDO> tests(long id,String ruleType);
<select resultType="com.xxx.bean.ChargeRuleDO">
select * from t_charge_rule t where t.id = #{0} and t.rule_type=#{1}
</select>
(2) 使用注解 #{} 中的參數名與方法中的 @Param() 里的參數名一致
List<ChargeRuleDO> tests(@Param("id") long id,@Param("ruleType") String ruleType);
<select resultType="com.xxx.bean.ChargeRuleDO">
select * from t_charge_rule t where t.id = #{id} and t.rule_type=#{ruleType}
</select>
2、復雜數據類型
(1) 直接使用
嘗試了兩種取參數的方法都不行
(2) 使用注解 #{} 中的參數名與方法中的 @Param() 里的參數對應的屬性名一致, 而且必須寫成 "#{testQO.id}" 的格式, 不能簡寫成 "#{id}".
List<ChargeRuleDO> tests(@Param("testQO")TestQO testQO,@Param("testQO2")TestQO2 testQO2);
<select resultType="com.xxx.bean.ChargeRuleDO">
select * from t_charge_rule t where t.id = #{testQO.id} and t.rule_type=#{testQO.ruleType} and t.user_id=#{testQO2.uesrId}
</select>
3、基本數據類型與復雜數據類型混合使用
因為在多參數的情況下, 復雜數據類型不能直接使用, 所以這里主要嘗試基本數據類型直接使用並且復雜數據類型使用注解這一種情況
List<ChargeRuleDO> tests(long id,String ruleType,@Param("testQO2")TestQO2 testQO2);
<select resultType="com.xxx.bean.ChargeRuleDO">
select * from t_charge_rule t where t.id = #{0} and t.rule_type=#{1} and t.user_id=#{testQO2.uesrId}
</select>
三、List 參數
List<ChargeRuleDO> tests(List<long> list);
<select resultType="com.xxx.bean.ChargeRuleDO">
select * from t_charge_rule t where t.id in
<foreach item="item" index="index" collection="list" open="(" separator="," close=")">
#{item}
</foreach>
</select>
Map 參數
法一:
collection="inventoryMap" 表示的是 dao 中對應的 map 的 @param 的參數名稱, index="key" 中的 key 表示的是 map 的 key 值, item="value" 表示的 map 的 key 所對應的 value 值 , 故直接 #{key} 和 #{value} 進行取值即可
longupdateInventoryBatch(@Param("inventoryMap") HashMap<String, Integer> inventoryMap);
<update parameterType="java.util.Map">
<foreach item="value" index="key" collection="inventoryMap" separator=";" >
UPDATE yanxuan_inventory_transfer
SET
inventory = #{value},
is_inventory='1',
create_time=sysdate()
where
sku_id=#{key}
</foreach>
</update>
法二: 先遍歷 map 的 key, 得到所有的 key 值, 然后根據 key 獲取對應的 value 值
collection="inventoryMap.keys" 表示的遍歷 map 的 key, 同理 collection="inventoryMap.values" 表示的是遍歷的 map 的 value, item="key" 表示的是 map 的 key, #{inventoryMap[${key}]} 取出的是 inventoryMap key 所對應的 value,
注意: ${key} 必須為使用 ${} 取值, 不能使用 #{}, 因為 #{} 會自動加上 "" 這樣是獲取不到 value 值的.
<update parameterType="java.util.Map">
<foreach item="key" collection="inventoryMap.keys" separator=";" >
UPDATE yanxuan_inventory_transfer
SET
inventory = #{inventoryMap[${key}]},
is_inventory='1',
create_time=sysdate()
where
sku_id=#{key}
</foreach>
</update>