转于: https://www.cnblogs.com/xiancheng/p/5543014.html
在茫茫博海中,找到一篇符合口味的博客不容易,感谢博主!
一、单个参数
1、基本数据类型
(1)直接使用 #{}中的参数名与方法中的参数名一致
List<ChargeRuleDO> tests(long id);
<select id="tests" 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 id="tests" 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 id="tests" 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 id="tests" 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 id="tests" 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 id="tests" 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 id="tests" 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 id="tests" 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 id="tests" 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 id="updateInventoryBatch" 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 id="updateInventoryBatch" 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>