这里只讨论向sql语句中传的参数为原始类型时,例如String ,int 等,而不是map,对象等
传多个参数时
需要在dao层使用@Param 注解来标识每个参数,如下
int deleteAbilityDetail(@Param(value = "userId")String userId, @Param(value = "abilityId") String abilityId);
括号中的value值为向sql语句中传入时的名字,这里在sql语句中有两种写法
1. 指定参数名字
<delete id="deleteAbilityDetail">
delete from t_ability
where user_id=#{userId} and id=#{abilityId}
</delete>
- 根据参数索引
<delete id="deleteAbilityDetail">
delete from t_ability
where user_id=#{0} and id=#{1}
</delete>
传递单个参数时
- 当sql语句中,没有对传入的参数进行if判断时,dao层可以不使用@Param注解,直接在sql语句中使用参数名字或参数索引即可
- 当sql语句中,对传入的参数进行了if判断,dao层需使用@Param注解,在sql语句中使用参数名字或参数索引即可