方法一:使用map接口传递参数
map是一个键值对应的集合,要通过知道它的键,才能明了其作用,使用map不能限定其传递的数据类型。
publicList findRolesByMap(Map<String, Object> parameterMap);
<select id="findRolesByMap" parameterType="map" resultType="role">
select id, role_name as roleName, note from t_role where role_name like concat('%', #{roleName}, '%') and note like concat('%', #{note}, '%')
</select>
方法二:使用注解传递多个参数
注解@Param(org.apache.ibatis.annotations.Param),可以通过它去定义映射器的参数名称,使用它可以得到更好的可读性。
public List findRolesByAnnotation(@Param(“roleName”) String rolename, @Param(“note”) String note);
<select id="findRolesByAnnotation" resultType="role">
select id, role_name as roleName, note from t_role where role_name like concat('%', #{roleName}, '%') and note like concat('%', #{note}, '%')
</select>
方法三:通过Java Bean传递多个参数
public List findRolesByBean(RoleParams roleParam);
select id, role_name as roleName, note from t_role where role_name like concat(’%’, #{roleName}, ‘%’) and note like concat(’%’, #{note}, ‘%’)
方法四:混合使用
在一些情况下需要混合使用几种方法来传递参数。
public List findByMix(@Param(“params”) RoleParams roleParams, @Param(“page”) PageParam PageParam);
select id, role_name as roleName, note from t_role
where role_name like concat(’%’, #{params.roleName}, ‘%’) and note like concat(’%’, #{params.note}, ‘%’) limit #{page.start}, #{page.limit}