3.参数是Map类型
这种情况下可以直接跟据map的key进行取值。
public interface EmployeeMapper {
public Employee getEmp(Map<String,Object> map);
}
接口
<mapper namespace="com.xj.mapper.EmployeeMapper">
<select id="getEmp" resultType="com.xj.domain.Employee">
SELECT * FROM employee WHERE id = #{id} AND lastName = #{lastName}
</select>
</mapper>
映射文件
4.参数是Conlection、数组类型的
方式一:使用mybatis默认提供的方式
这种情况下mybatis也会帮我们对参数进行特殊处理,mybtis同样把Conlection、数组类型的参数封装为Map,如果是Collection类型则Map的key为collection,如果是Array类型则Map的key为array。特殊的如List,他的key为list,但是由于List也属于Collection,所以key也可以用collection。
public interface EmployeeMapper {
public Employee getEmpById(List<Integer> ids);
}
接口
<mapper namespace="com.xj.mapper.EmployeeMapper">
<select id="getEmpById" resultType="com.xj.domain.Employee">
select * from employee where id = #{list[0]}
</select>
</mapper>
映射文件
方式二:使用@Param注解
我们可以在接口的参数上标注@Param注解,指定参数的名称,然后映射文件中就可以直接通过#{注解指定的参数名}进行取值
public interface EmployeeMapper {
public Employee getEmpById(@Param("idList") List<Integer> ids);
}
接口
<mapper namespace="com.xj.mapper.EmployeeMapper">
<select id="getEmpById" resultType="com.xj.domain.Employee">
select * from employee where id = #{idList[0]}
</select>
</mapper>
映射文件
二、接口中有两个及两个以上参数
1.多个参数都是基本类型or基本类型的包装类orString类型
方式一:使用mybatis默认提供的方式
有多个参数时,mybatis会把这多个参数封装为Map类型,这个Map中的key为param1,param2,....,paramN。所以我们要取出第一个参数就可以用#{param1}取出,要取第N个参数就用#{paramN}。
public interface EmployeeMapper {
public Employee getEmp(String lastName,Integer id);
}
接口
<mapper namespace="com.xj.mapper.EmployeeMapper">
<select id="getEmp" resultType="com.xj.domain.Employee">
SELECT * FROM employee WHERE id = #{param2} AND lastName = #{param1}
</select>
</mapper>
映射文件
方式二:使用@Param注解
我们可以在接口的参数上标注@Param注解,指定参数的名称,然后映射文件中就可以直接通过#{注解指定的参数名}进行取值
public interface EmployeeMapper {
public Employee getEmp(@Param("name") String lastName,@Param("id") Integer id);
}
接口
<mapper namespace="com.xj.mapper.EmployeeMapper">
<select id="getEmp" resultType="com.xj.domain.Employee">
SELECT * FROM employee WHERE id = #{id} AND lastName = #{name}
</select>
</mapper>
映射文件
2.多个参数中既有对象类型,又有参数是基本类型or基本类型的包装类or字符串类型
public interface EmployeeMapper {
public Employee getEmp(@Param("id") Integer id,@Param("emp") Employee employee);
}
接口
<mapper namespace="com.xj.mapper.EmployeeMapper">
<select id="getEmp" resultType="com.xj.domain.Employee">
SELECT * FROM employee WHERE id = #{id} AND lastName = #{emp.lastName}
</select>
</mapper>
映射文件