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>